From 64a313480ff4aeb3c84967453fcd940f2805698a Mon Sep 17 00:00:00 2001 From: dojyorin Date: Fri, 14 Jul 2023 19:37:43 +0900 Subject: [PATCH] feat: object operation. --- deps.test.ts | 8 ++++---- deps.ts | 8 ++++---- mod.test.ts | 1 + mod.ts | 1 + mod.universal.ts | 1 + src/crypto.ts | 11 ----------- src/fetch.ts | 2 +- src/log.deno.ts | 6 +++--- src/object.ts | 29 +++++++++++++++++++++++++++++ test/crypto.test.ts | 11 +---------- test/object.test.ts | 32 ++++++++++++++++++++++++++++++++ 11 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 src/object.ts create mode 100644 test/object.test.ts diff --git a/deps.test.ts b/deps.test.ts index a3d0cb9..e0eb600 100644 --- a/deps.test.ts +++ b/deps.test.ts @@ -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"; \ No newline at end of file +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"; \ No newline at end of file diff --git a/deps.ts b/deps.ts index 4b59692..a11260b 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; \ No newline at end of file +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"; \ No newline at end of file diff --git a/mod.test.ts b/mod.test.ts index 3ae27be..2b732f0 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -5,6 +5,7 @@ import "./test/fetch.test.ts"; import "./test/json.deno.test.ts"; import "./test/log.deno.test.ts"; import "./test/minipack.test.ts"; +import "./test/object.test.ts"; import "./test/path.deno.test.ts"; import "./test/platform.deno.test.ts"; import "./test/process.deno.test.ts"; diff --git a/mod.ts b/mod.ts index e807950..068a54e 100644 --- a/mod.ts +++ b/mod.ts @@ -3,6 +3,7 @@ export * from "./src/crypto.ts"; export * from "./src/deflate.ts"; export * from "./src/fetch.ts"; export * from "./src/minipack.ts"; +export * from "./src/object.ts"; export * from "./src/text.ts"; export * from "./src/time.ts"; diff --git a/mod.universal.ts b/mod.universal.ts index 26f5277..815a728 100644 --- a/mod.universal.ts +++ b/mod.universal.ts @@ -3,5 +3,6 @@ export * from "./src/crypto.ts"; export * from "./src/deflate.ts"; export * from "./src/fetch.ts"; export * from "./src/minipack.ts"; +export * from "./src/object.ts"; export * from "./src/text.ts"; export * from "./src/time.ts"; \ No newline at end of file diff --git a/src/crypto.ts b/src/crypto.ts index 2d07d51..914d579 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -19,17 +19,6 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ }, false, ["encrypt", "decrypt"]); } -/** -* Generate UUIDv4 string. -* @example -* ```ts -* const uuid = randomUuid(); -* ``` -*/ -export function randomUuid():string{ - return crypto.randomUUID(); -} - /** * Generate random binary with any number of bytes. * @example diff --git a/src/fetch.ts b/src/fetch.ts index 21deb22..b97495c 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -47,7 +47,7 @@ export async function fetchExtend(path:string, typ referrerPolicy: option?.referrerPolicy ?? "no-referrer", referrer: option?.referrer, signal: option?.signal, - headers: option?.headers, + headers: option?.headers && new Headers(option.headers), body: option?.body }); diff --git a/src/log.deno.ts b/src/log.deno.ts index d74f9f2..e2fce32 100644 --- a/src/log.deno.ts +++ b/src/log.deno.ts @@ -15,10 +15,10 @@ function logRecord(date:Date, level:string, message:string){ * ``` */ export function logEntry(name?:string):Logger{ - const logName = name ?? "operation"; + const title = name ?? "operation"; const level = "INFO"; - const log = new Logger(logName, level, { + const log = new Logger(title, level, { handlers: [ new ConsoleHandler(level, { formatter({datetime, levelName, msg}){ @@ -26,7 +26,7 @@ export function logEntry(name?:string):Logger{ } }), new FileHandler(level, { - filename: `${mainPath()}/${logName}.log`, + filename: `${mainPath()}/${title}.log`, formatter({datetime, levelName, msg}){ return logRecord(datetime, levelName, msg); } diff --git a/src/object.ts b/src/object.ts new file mode 100644 index 0000000..30418f3 --- /dev/null +++ b/src/object.ts @@ -0,0 +1,29 @@ +export type Opt = Partial>; + +export function deepClone>(data:T):T{ + return structuredClone(data); +} + +export function deepFreeze>(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>(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; +} \ No newline at end of file diff --git a/test/crypto.test.ts b/test/crypto.test.ts index 21136b6..4075bda 100644 --- a/test/crypto.test.ts +++ b/test/crypto.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {randomUuid, randomBin, hashValue, pubkeyGen, pubkeyEncrypt, pubkeyDecrypt, pubkeySign, pubkeyVerify} from "../src/crypto.ts"; +import {randomBin, hashValue, pubkeyGen, pubkeyEncrypt, pubkeyDecrypt, pubkeySign, pubkeyVerify} from "../src/crypto.ts"; const sample = new Uint8Array([0x02, 0xF2, 0x5D, 0x1F, 0x1C, 0x34, 0xB9, 0x2F]); @@ -14,15 +14,6 @@ const hashResult = new Uint8Array([ 0x0D, 0xE6, 0x31, 0xDA, 0x4B, 0xBD, 0xD4, 0x58 ]); -Deno.test({ - name: "Crypto: UUID", - fn(){ - const {length} = randomUuid(); - - assertEquals(length, 36); - } -}); - Deno.test({ name: "Crypto: Random", fn(){ diff --git a/test/object.test.ts b/test/object.test.ts new file mode 100644 index 0000000..88f5bd5 --- /dev/null +++ b/test/object.test.ts @@ -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); + } +}); \ No newline at end of file