-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from dojyorin/dev
feat: object operation.
- Loading branch information
Showing
11 changed files
with
77 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export {assertEquals} from "https://deno.land/std@0.192.0/testing/asserts.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.192.0/path/mod.ts"; | ||
export {serve} from "https://deno.land/std@0.192.0/http/mod.ts"; | ||
export {exists} from "https://deno.land/std@0.192.0/fs/mod.ts"; | ||
export {assertEquals} from "https://deno.land/std@0.194.0/testing/asserts.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.194.0/path/mod.ts"; | ||
export {serve} from "https://deno.land/std@0.194.0/http/mod.ts"; | ||
export {exists} from "https://deno.land/std@0.194.0/fs/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.192.0/path/mod.ts"; | ||
export {Logger} from "https://deno.land/std@0.192.0/log/mod.ts"; | ||
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.192.0/log/handlers.ts"; | ||
export {format as formatDate} from "https://deno.land/std@0.192.0/datetime/mod.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.194.0/path/mod.ts"; | ||
export {Logger} from "https://deno.land/std@0.194.0/log/mod.ts"; | ||
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.194.0/log/handlers.ts"; | ||
export {format as formatDate} from "https://deno.land/std@0.194.0/datetime/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export type Opt<T> = Partial<Record<keyof T, unknown>>; | ||
|
||
export function deepClone<T extends Opt<T>>(data:T):T{ | ||
return structuredClone(data); | ||
} | ||
|
||
export function deepFreeze<T extends Opt<T>>(data:T):T{ | ||
Object.freeze(data); | ||
|
||
for(const k in data){ | ||
if(Object.hasOwn(data, k) && typeof data[k] === "object" && data[k] !== null && !Object.isFrozen(data[k])){ | ||
deepFreeze(data[k]); | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
export function deepSeal<T extends Opt<T>>(data:T):T{ | ||
Object.seal(data); | ||
|
||
for(const k in data){ | ||
if(Object.hasOwn(data, k) && typeof data[k] === "object" && data[k] !== null && !Object.isSealed(data[k])){ | ||
deepSeal(data[k]); | ||
} | ||
} | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {assertEquals} from "../deps.test.ts"; | ||
import {deepClone, deepFreeze, deepSeal} from "../src/object.ts"; | ||
|
||
const sample = { | ||
aaa: { | ||
bbb: { | ||
ccc: false | ||
} | ||
} | ||
}; | ||
|
||
Deno.test({ | ||
name: "Freeze: DeepFreeze", | ||
fn(){ | ||
const freeze = deepFreeze(deepClone(sample)); | ||
|
||
const desc = Object.getOwnPropertyDescriptor(freeze.aaa.bbb, "ccc"); | ||
assertEquals(desc?.configurable, false); | ||
assertEquals(desc?.writable, false); | ||
} | ||
}); | ||
|
||
Deno.test({ | ||
name: "Freeze: DeepSeal", | ||
fn(){ | ||
const seal = deepSeal(deepClone(sample)); | ||
|
||
const desc = Object.getOwnPropertyDescriptor(seal.aaa.bbb, "ccc"); | ||
assertEquals(desc?.configurable, false); | ||
assertEquals(desc?.writable, true); | ||
} | ||
}); |