Skip to content

Commit

Permalink
Fix copies test
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaidong committed Jun 24, 2024
1 parent 7711827 commit cfd0ada
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 293 deletions.
25 changes: 13 additions & 12 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ import {
isString,
} from "./utils/detection.ts";

export const clone = (val: any): any => {
export type AnyObject = { [key: string]: any };

export const clone = (val: AnyObject): AnyObject => {
return structuredClone(val);
};

export const copies = (
source: object,
dest: object,
export function copies(
source: AnyObject,
dest: AnyObject,
matched: boolean = false,
excepts: string[] = [],
): object => {
const xdest = clone(dest)
): AnyObject {
for (const k in source) {
if (excepts.length > 0 && excepts.includes(k)) {
continue;
}
if (!matched || (matched && hasProperty(dest, k))) {
const oa: any = source[k as keyof typeof source];
const ob: any = dest[k as keyof typeof dest];
const oa = source[k];
const ob = dest[k];
if ((isObject(ob) && isObject(oa)) || (isArray(ob) && isArray(oa))) {
xdest[k] = copies(oa, dest[k as keyof typeof dest], matched, excepts);
dest[k] = copies(oa, dest[k], matched, excepts);
} else {
xdest[k] = clone(oa);
dest[k] = clone(oa);
}
}
}
return xdest;
};
return dest;
}

export const unique = (arr: any[] = []): any[] => {
return [...new Set(arr)];
Expand Down
74 changes: 37 additions & 37 deletions tests/date_test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import { assertEquals } from "assert";

import {
formatDateString,
formatTimeAgo
} from "../utils/date.ts";
import { formatDateString, formatTimeAgo } from "../utils/date.ts";

Deno.test("check if .formatDateString() works correctly", async (t) => {
const d = new Date();

await t.step(' check .formatDateString() with default options', () => {
const result = formatDateString(d)
const reg = /^\w+\s\d+,\s+\d{4},\s\d+:\d+:\d+\s(AM|PM)\s(GMT)\+\d+$/
assertEquals(result.match(reg) !== null, true)
await t.step(" check .formatDateString() with default options", () => {
const result = formatDateString(d);
const reg = /^\w+\s\d+,\s+\d{4},\s\d+:\d+:\d+\s(AM|PM)\s(GMT)\+\d+$/;
assertEquals(result.match(reg) !== null, true);
});

await t.step(' check .formatDateString() with custom options', () => {
await t.step(" check .formatDateString() with custom options", () => {
const result = formatDateString(d, {
dateStyle: 'full',
timeStyle: 'medium',
dateStyle: "full",
timeStyle: "medium",
hour12: true,
})
const reg = /^\w+,\s\w+\s\d+,\s+\d{4}\sat\s\d+:\d+:\d+\s(AM|PM)$/
assertEquals(result.match(reg) !== null, true)
});
const reg = /^\w+,\s\w+\s\d+,\s+\d{4}\sat\s\d+:\d+:\d+\s(AM|PM)$/;
assertEquals(result.match(reg) !== null, true);
});

await t.step(' check .formatDateString() with custom language and options', () => {
const result = formatDateString(d, 'en', {
dateStyle: 'full',
timeStyle: 'medium',
hour12: true,
})
const reg = /^\w+,\s\w+\s\d+,\s+\d{4}\sat\s\d+:\d+:\d+\s(AM|PM)$/
assertEquals(result.match(reg) !== null, true)
});
await t.step(
" check .formatDateString() with custom language and options",
() => {
const result = formatDateString(d, "en", {
dateStyle: "full",
timeStyle: "medium",
hour12: true,
});
const reg = /^\w+,\s\w+\s\d+,\s+\d{4}\sat\s\d+:\d+:\d+\s(AM|PM)$/;
assertEquals(result.match(reg) !== null, true);
},
);
});

Deno.test("check if .formatTimeAgo() works correctly", async (t) => {
const d = new Date();

await t.step(' check if .formatTimeAgo() return "just now"', () => {
const result = formatTimeAgo(d)
assertEquals(result, 'just now')
const justnowCustomMessage = formatTimeAgo(d, 'vi', 'vừa mới xong')
assertEquals(justnowCustomMessage, 'vừa mới xong')
const result = formatTimeAgo(d);
assertEquals(result, "just now");
const justnowCustomMessage = formatTimeAgo(d, "vi", "vừa mới xong");
assertEquals(justnowCustomMessage, "vừa mới xong");
});

await t.step(' check if .formatTimeAgo() after 5s', () => {
const t = d.getSeconds()
d.setSeconds(t - 5)
const result = formatTimeAgo(d)
assertEquals(result, '5 seconds ago')
await t.step(" check if .formatTimeAgo() after 5s", () => {
const t = d.getSeconds();
d.setSeconds(t - 5);
const result = formatTimeAgo(d);
assertEquals(result, "5 seconds ago");
});

await t.step(' check if .formatTimeAgo() after 2 days', () => {
const t = d.getDate()
d.setDate(t - 2)
const result = formatTimeAgo(d)
assertEquals(result, '2 days ago')
await t.step(" check if .formatTimeAgo() after 2 days", () => {
const t = d.getDate();
d.setDate(t - 2);
const result = formatTimeAgo(d);
assertEquals(result, "2 days ago");
});
});
Loading

0 comments on commit cfd0ada

Please sign in to comment.