Skip to content

Commit

Permalink
Merge pull request #62 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat: object operation.
  • Loading branch information
dojyorin authored Jul 14, 2023
2 parents 747d7f1 + 64a3134 commit 775ec69
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 33 deletions.
8 changes: 4 additions & 4 deletions deps.test.ts
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";
8 changes: 4 additions & 4 deletions deps.ts
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";
1 change: 1 addition & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
1 change: 1 addition & 0 deletions mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
11 changes: 0 additions & 11 deletions src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function fetchExtend<T extends keyof ResponseType>(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
});

Expand Down
6 changes: 3 additions & 3 deletions src/log.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ 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}){
return logRecord(datetime, levelName, msg);
}
}),
new FileHandler(level, {
filename: `${mainPath()}/${logName}.log`,
filename: `${mainPath()}/${title}.log`,
formatter({datetime, levelName, msg}){
return logRecord(datetime, levelName, msg);
}
Expand Down
29 changes: 29 additions & 0 deletions src/object.ts
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;
}
11 changes: 1 addition & 10 deletions test/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -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]);

Expand All @@ -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(){
Expand Down
32 changes: 32 additions & 0 deletions test/object.test.ts
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);
}
});

0 comments on commit 775ec69

Please sign in to comment.