From cfd0adaaf3ab719a3510a1d3abce3a6e03dc352f Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Mon, 24 Jun 2024 07:49:36 +0700 Subject: [PATCH] Fix copies test --- mod.ts | 25 ++--- tests/date_test.ts | 74 +++++++-------- tests/detection_test.ts | 200 ++++++++++++++++++++-------------------- tests/mod_test.ts | 184 +++++++++++++++++++----------------- tests/string_test.ts | 70 +++++++------- utils/date.ts | 58 ++++++------ utils/string.ts | 2 +- 7 files changed, 320 insertions(+), 293 deletions(-) diff --git a/mod.ts b/mod.ts index 0a10b0b..ce76340 100644 --- a/mod.ts +++ b/mod.ts @@ -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)]; diff --git a/tests/date_test.ts b/tests/date_test.ts index bb5bf1a..9700772 100644 --- a/tests/date_test.ts +++ b/tests/date_test.ts @@ -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"); }); }); diff --git a/tests/detection_test.ts b/tests/detection_test.ts index 29f8aea..e8f7de0 100644 --- a/tests/detection_test.ts +++ b/tests/detection_test.ts @@ -1,250 +1,250 @@ import { assertEquals } from "assert"; -import { - isNumber, - isInteger, +import { + hasProperty, isArray, - isString, isBoolean, - isNull, - isUndefined, - isNil, - isFunction, - isObject, isDate, isEmail, isEmpty, - hasProperty + isFunction, + isInteger, + isNil, + isNull, + isNumber, + isObject, + isString, + isUndefined, } from "../utils/detection.ts"; Deno.test("check if .isNumber() works correctly", async (t) => { - const positives = [1, 1.5, 0, 9999, -2] + const positives = [1, 1.5, 0, 9999, -2]; for (const val of positives) { await t.step(`test .isNumber(${val}) --> true`, () => { - assertEquals(isNumber(val), true) - }) + assertEquals(isNumber(val), true); + }); } - const negatives = [{}, [], '', null, undefined] + const negatives = [{}, [], "", null, undefined]; for (const val of negatives) { await t.step(`test .isNumber(${val}) --> true`, () => { - assertEquals(isNumber(val), false) - }) + assertEquals(isNumber(val), false); + }); } }); Deno.test("check if .isInteger() works correctly", async (t) => { - const positives = [1, 1000, 9999, 0, -3] + const positives = [1, 1000, 9999, 0, -3]; for (const val of positives) { await t.step(`test .isInteger(${val}) --> true`, () => { - assertEquals(isInteger(val), true) - }) + assertEquals(isInteger(val), true); + }); } - const negatives = [1.5, -3.2, '', undefined] + const negatives = [1.5, -3.2, "", undefined]; for (const val of negatives) { await t.step(`test .isInteger(${val}) --> true`, () => { - assertEquals(isInteger(val), false) - }) + assertEquals(isInteger(val), false); + }); } }); Deno.test("check if .isArray() works correctly", async (t) => { - const positives = [[], [1, 2, 3], new Array(), Array.from(new Set())] + const positives = [[], [1, 2, 3], new Array(), Array.from(new Set())]; for (const val of positives) { await t.step(`test .isArray(${val}) --> true`, () => { - assertEquals(isArray(val), true) - }) + assertEquals(isArray(val), true); + }); } - const negatives = [1.5, '', undefined] + const negatives = [1.5, "", undefined]; for (const val of negatives) { await t.step(`test .isArray(${val}) --> true`, () => { - assertEquals(isArray(val), false) - }) + assertEquals(isArray(val), false); + }); } }); Deno.test("check if .isString() works correctly", async (t) => { - const positives = ['', 'abc xyz', '10000'] + const positives = ["", "abc xyz", "10000"]; for (const val of positives) { await t.step(`test .isString(${val}) --> true`, () => { - assertEquals(isString(val), true) - }) + assertEquals(isString(val), true); + }); } - const negatives = [{}, 30, [], 1.5, null, undefined] + const negatives = [{}, 30, [], 1.5, null, undefined]; for (const val of negatives) { await t.step(`test .isString(${val}) --> true`, () => { - assertEquals(isString(val), false) - }) + assertEquals(isString(val), false); + }); } }); Deno.test("check if .isBoolean() works correctly", async (t) => { - const positives = [true, false] + const positives = [true, false]; for (const val of positives) { await t.step(`test .isBoolean(${val}) --> true`, () => { - assertEquals(isBoolean(val), true) - }) + assertEquals(isBoolean(val), true); + }); } - const negatives = [{}, [], '', 1, 0, null, undefined] + const negatives = [{}, [], "", 1, 0, null, undefined]; for (const val of negatives) { await t.step(`test .isBoolean(${val}) --> true`, () => { - assertEquals(isBoolean(val), false) - }) + assertEquals(isBoolean(val), false); + }); } }); Deno.test("check if .isNull() works correctly", async (t) => { - const positives = [null] + const positives = [null]; for (const val of positives) { await t.step(`test .isNull(${val}) --> true`, () => { - assertEquals(isNull(val), true) - }) + assertEquals(isNull(val), true); + }); } - const negatives = [{}, [], '', 0, undefined] + const negatives = [{}, [], "", 0, undefined]; for (const val of negatives) { await t.step(`test .isNull(${val}) --> true`, () => { - assertEquals(isNull(val), false) - }) + assertEquals(isNull(val), false); + }); } }); Deno.test("check if .isUndefined() works correctly", async (t) => { - let v - const positives = [undefined, v] + let v; + const positives = [undefined, v]; for (const val of positives) { await t.step(`test .isUndefined(${val}) --> true`, () => { - assertEquals(isUndefined(val), true) - }) + assertEquals(isUndefined(val), true); + }); } - const negatives = [{}, [], '', 0, null] + const negatives = [{}, [], "", 0, null]; for (const val of negatives) { await t.step(`test .isUndefined(${val}) --> true`, () => { - assertEquals(isUndefined(val), false) - }) + assertEquals(isUndefined(val), false); + }); } }); Deno.test("check if .isNil() works correctly", async (t) => { - let v - const positives = [undefined, v, null] + let v; + const positives = [undefined, v, null]; for (const val of positives) { await t.step(`test .isNil(${val}) --> true`, () => { - assertEquals(isNil(val), true) - }) + assertEquals(isNil(val), true); + }); } - const negatives = [{}, [], '', 0] + const negatives = [{}, [], "", 0]; for (const val of negatives) { await t.step(`test .isNil(${val}) --> true`, () => { - assertEquals(isNil(val), false) - }) + assertEquals(isNil(val), false); + }); } }); Deno.test("check if .isFunction() works correctly", async (t) => { - const positives = [function () {}, () => {}] + const positives = [function () {}, () => {}]; for (const val of positives) { await t.step(`test .isFunction(${val}) --> true`, () => { - assertEquals(isFunction(val), true) - }) + assertEquals(isFunction(val), true); + }); } - const negatives = [{}, [], '', 0, null] + const negatives = [{}, [], "", 0, null]; for (const val of negatives) { await t.step(`test .isFunction(${val}) --> true`, () => { - assertEquals(isFunction(val), false) - }) + assertEquals(isFunction(val), false); + }); } }); Deno.test("check if .isObject() works correctly", async (t) => { - const ob = new Object() - const positives = [{}, ob, Object.create({})] + const ob = new Object(); + const positives = [{}, ob, Object.create({})]; for (const val of positives) { await t.step(`test .isObject(${val}) --> true`, () => { - assertEquals(isObject(val), true) - }) + assertEquals(isObject(val), true); + }); } - const negatives = [17, [], '', 0, null, () => {}, true] + const negatives = [17, [], "", 0, null, () => {}, true]; for (const val of negatives) { await t.step(`test .isObject(${val}) --> true`, () => { - assertEquals(isObject(val), false) - }) + assertEquals(isObject(val), false); + }); } }); Deno.test("check if .isDate() works correctly", async (t) => { - const dt = new Date() - const positives = [dt] + const dt = new Date(); + const positives = [dt]; for (const val of positives) { await t.step(`test .isDate(${val}) --> true`, () => { - assertEquals(isDate(val), true) - }) + assertEquals(isDate(val), true); + }); } - const negatives = [17, [], '', 0, null, () => {}, true, {}, dt.toUTCString()] + const negatives = [17, [], "", 0, null, () => {}, true, {}, dt.toUTCString()]; for (const val of negatives) { await t.step(`test .isDate(${val}) --> true`, () => { - assertEquals(isDate(val), false) - }) + assertEquals(isDate(val), false); + }); } }); Deno.test("check if .isEmail() works correctly", async (t) => { - const positives = ['admin@pwshub.com', 'abc@qtest.com'] + const positives = ["admin@pwshub.com", "abc@qtest.com"]; for (const val of positives) { await t.step(`test .isEmail(${val}) --> true`, () => { - assertEquals(isEmail(val), true) - }) + assertEquals(isEmail(val), true); + }); } - const negatives = [{}, [], '', 0, undefined, 'a23b@qtest@com'] + const negatives = [{}, [], "", 0, undefined, "a23b@qtest@com"]; for (const val of negatives) { await t.step(`test .isEmail(${val}) --> true`, () => { - assertEquals(isEmail(val), false) - }) + assertEquals(isEmail(val), false); + }); } }); Deno.test("check if .isEmpty() works correctly", async (t) => { - const positives = ['', 0, {}, [], undefined, null] + const positives = ["", 0, {}, [], undefined, null]; for (const val of positives) { await t.step(`test .isEmpty(${val}) --> true`, () => { - assertEquals(isEmpty(val), true) - }) + assertEquals(isEmpty(val), true); + }); } - const negatives = [{ a: 1 }, '12', 9, [7, 1]] + const negatives = [{ a: 1 }, "12", 9, [7, 1]]; for (const val of negatives) { await t.step(`test .isEmpty(${val}) --> true`, () => { - assertEquals(isEmpty(val), false) - }) + assertEquals(isEmpty(val), false); + }); } }); Deno.test("check if .hasProperty() works correctly", async (t) => { const obj = { - name: 'alice', + name: "alice", age: 17, - } - const positives = ['name', 'age'] + }; + const positives = ["name", "age"]; for (const val of positives) { await t.step(`test .hasProperty(${val}) --> true`, () => { - assertEquals(hasProperty(obj, val), true) - }) + assertEquals(hasProperty(obj, val), true); + }); } - const negatives = [{ a: 1 }, 'email', 9, '__proto__'] + const negatives = [{ a: 1 }, "email", 9, "__proto__"]; for (const val of negatives) { await t.step(`test .hasProperty(${val}) --> true`, () => { - assertEquals(hasProperty(obj, val), false) - }) + assertEquals(hasProperty(obj, val), false); + }); } }); diff --git a/tests/mod_test.ts b/tests/mod_test.ts index ef63fd1..0fecba4 100644 --- a/tests/mod_test.ts +++ b/tests/mod_test.ts @@ -1,13 +1,14 @@ import { assertEquals, assertStringIncludes } from "assert"; import { + AnyObject, clone, copies, - unique, + hasProperty, + pick, sort, sortBy, - pick, - hasProperty + unique, } from "../mod.ts"; Deno.test("check if .clone() works correctly", async (t) => { @@ -16,136 +17,153 @@ Deno.test("check if .clone() works correctly", async (t) => { level: 4, IQ: 140, epouse: { - name: 'Alice', + name: "Alice", age: 27, }, birthday: new Date(), a: 0, clone: false, reg: /^\w+@\s([a-z])$/gi, - } - const y = clone(x) + }; + const y = clone(x); Object.keys(x).forEach((k) => { - assertEquals(hasProperty(y, k), true) - }) + assertEquals(hasProperty(y, k), true); + }); Object.keys(x.epouse).forEach((k) => { - assertEquals(hasProperty(y.epouse, k), true) - assertEquals(y.epouse[k], x.epouse[k as keyof typeof x.epouse]) - }) + assertEquals(hasProperty(y.epouse, k), true); + assertEquals(y.epouse[k], x.epouse[k as keyof typeof x.epouse]); + }); // check immutability - y.epouse.age = 25 - assertEquals(y.epouse.age, 25) - assertEquals(x.epouse.age, 27) - }) + y.epouse.age = 25; + assertEquals(y.epouse.age, 25); + assertEquals(x.epouse.age, 27); + }); await t.step(`check if .clone(array) works correctly`, () => { const x: any[] = [ 1, 5, 0, - 'a', + "a", -10, - '-10', - '', + "-10", + "", { a: 1, - b: 'Awesome', + b: "Awesome", }, [ 5, 6, 8, { - name: 'Lys', + name: "Lys", age: 11, }, ], - ] - const y = clone(x) - assertEquals(y.length, x.length) + ]; + const y = clone(x); + assertEquals(y.length, x.length); for (let i = 0; i < x.length; i++) { - assertEquals(x[i], y[i]) + assertEquals(x[i], y[i]); } // check immutability - y[8][3].age = 10 - assertEquals(y[8][3].age, 10) - assertEquals(x[8][3].age, 11) - }) + y[8][3].age = 10; + assertEquals(y[8][3].age, 10); + assertEquals(x[8][3].age, 11); + }); }); Deno.test("check if .copies() works correctly", async (t) => { - // await t.step(`check if .copies(source, dest) works correctly`, () => { - // const str = 'abcdefghijklmnopqrstuvwxyz' - // const arr = str.split('') - // const uniqChar = pick(arr)[0] - // assertStringIncludes(str, uniqChar) - // }) - - await t.step(`check if .copies(source, dest, matched, excepts) works correctly`, () => { - const source = { - name: 'Kiwi', - age: 16, - gender: 'male', - } - const dest = { - name: 'Aline', - age: 20, - } - copies(source, dest, true, ['age']) - assertEquals(hasProperty(dest, 'gender'), false) - assertEquals(dest.name, source.name) - assertEquals(dest.age, source.age) - }) + await t.step(`copies: simple copy`, () => { + const source: AnyObject = { a: 1, b: 2, c: 3 }; + const dest: AnyObject = { a: 10, b: 20, d: 40 }; + const result = copies(source, dest); + const expected: AnyObject = { a: 1, b: 2, d: 40, c: 3 }; + assertEquals(result, expected); + }); + + await t.step(`copies: with matched`, () => { + const source: AnyObject = { a: 1, b: 2, c: 3 }; + const dest: AnyObject = { a: 10, b: 20, d: 40 }; + const result = copies(source, dest, true); + const expected: AnyObject = { a: 1, b: 2, d: 40 }; + assertEquals(result, expected); + }); + + await t.step(`copies: with excepts`, () => { + const source: AnyObject = { a: 1, b: 2, c: 3 }; + const dest: AnyObject = { a: 10, b: 20, d: 40 }; + const result = copies(source, dest, false, ["b", "c"]); + const expected: AnyObject = { a: 1, b: 20, d: 40 }; + assertEquals(result, expected); + }); + + await t.step(`copies: nested objects`, () => { + const source: AnyObject = { a: 1, b: { b1: 2, b2: 3 }, c: 4 }; + const dest: AnyObject = { a: 10, b: { b1: 20, b2: 30 }, d: 50 }; + const result = copies(source, dest); + const expected: AnyObject = { a: 1, b: { b1: 2, b2: 3 }, c: 4, d: 50 }; + assertEquals(result, expected); + }); + + await t.step(`copies: nested arrays`, () => { + const source: AnyObject = { a: 1, b: [2, 3], c: 4 }; + const dest: AnyObject = { a: 10, b: [20, 30], d: 50 }; + const result = copies(source, dest); + const expected: AnyObject = { a: 1, b: [2, 3], c: 4, d: 50 }; + assertEquals(result, expected); + }); }); Deno.test("check if .unique(array) works correctly", () => { - const arr = [1, 1, 2, 2, 3, 4, 5, 5, 6, 3, 5, 4] - const uniqArr = unique(arr) - assertEquals(uniqArr.length, 6) + const arr = [1, 1, 2, 2, 3, 4, 5, 5, 6, 3, 5, 4]; + const uniqArr = unique(arr); + assertEquals(uniqArr.length, 6); }); Deno.test("check if .sort(array) works correctly", () => { - const arr = [6, 4, 8, 2] - const sortedArr = sort(arr) - assertEquals(sortedArr.join(''), '2468') + const arr = [6, 4, 8, 2]; + const sortedArr = sort(arr); + assertEquals(sortedArr.join(""), "2468"); }); Deno.test("check if .sortBy(array) works correctly", () => { const arr = [ - { age: 5, name: 'E' }, - { age: 9, name: 'B' }, - { age: 3, name: 'A' }, - { age: 12, name: 'D' }, - { age: 7, name: 'C' }, - ] + { age: 5, name: "E" }, + { age: 9, name: "B" }, + { age: 3, name: "A" }, + { age: 12, name: "D" }, + { age: 7, name: "C" }, + ]; const sortedByAge = [ - { age: 3, name: 'A' }, - { age: 5, name: 'E' }, - { age: 7, name: 'C' }, - { age: 9, name: 'B' }, - { age: 12, name: 'D' }, - ] - const sortedArr = sortBy(arr, 1, 'age') - assertEquals(JSON.stringify(sortedArr), JSON.stringify(sortedByAge)) - - const sortedByNonExistKey = sortBy(arr, 1, 'balance') - assertEquals(JSON.stringify(sortedByNonExistKey), JSON.stringify(arr)) + { age: 3, name: "A" }, + { age: 5, name: "E" }, + { age: 7, name: "C" }, + { age: 9, name: "B" }, + { age: 12, name: "D" }, + ]; + const sortedArr = sortBy(arr, 1, "age"); + assertEquals(JSON.stringify(sortedArr), JSON.stringify(sortedByAge)); + + const sortedByNonExistKey = sortBy(arr, 1, "balance"); + assertEquals(JSON.stringify(sortedByNonExistKey), JSON.stringify(arr)); }); Deno.test("check if .pick() works correctly", async (t) => { await t.step(`check if .pick(array) works correctly`, () => { - const str = 'abcdefghijklmnopqrstuvwxyz' - const arr = str.split('') - const uniqChar = pick(arr)[0] - assertStringIncludes(str, uniqChar) - }) + const str = "abcdefghijklmnopqrstuvwxyz"; + const arr = str.split(""); + const uniqChar = pick(arr)[0]; + assertStringIncludes(str, uniqChar); + }); await t.step(`check if .pick(array, count) works correctly`, () => { - const str = 'abcdefghijklmnopqrstuvwxyz' - const arr = str.split('') - const picked = pick(arr, 10) - assertEquals(picked.length, 10) - }) + const str = "abcdefghijklmnopqrstuvwxyz"; + const arr = str.split(""); + const picked = pick(arr, 10); + assertEquals(picked.length, 10); + }); }); diff --git a/tests/string_test.ts b/tests/string_test.ts index 123a85e..025888f 100644 --- a/tests/string_test.ts +++ b/tests/string_test.ts @@ -1,77 +1,81 @@ import { assertEquals } from "assert"; -import { - truncate, - stripTags, +import { escapeHTML, - unescapeHTML, + slugify, + stripTags, + truncate, ucfirst, ucwords, - slugify + unescapeHTML, } from "../utils/string.ts"; Deno.test("check if .truncate() works correctly", async (t) => { const inputs = [ { - text: 'If a property is non-configurable, its writable attribute can only be changed to false.', + text: + "If a property is non-configurable, its writable attribute can only be changed to false.", limit: 60, - expectation: 'If a property is non-configurable, its writable attribute...', + expectation: + "If a property is non-configurable, its writable attribute...", }, { - text: 'this string is less than limit', + text: "this string is less than limit", limit: 100, - expectation: 'this string is less than limit', + expectation: "this string is less than limit", }, { - text: 'uyyiyirwqyiyiyrihklhkjhskdjfhkahfiusayiyfiudyiyqwiyriuqyiouroiuyi', + text: "uyyiyirwqyiyiyrihklhkjhskdjfhkahfiusayiyfiudyiyqwiyriuqyiouroiuyi", limit: 20, - expectation: 'uyyiyirwqyiyiyrih...', + expectation: "uyyiyirwqyiyiyrih...", }, - ] + ]; - let k = 1 + let k = 1; for (const input of inputs) { - const { text, limit, expectation } = input + const { text, limit, expectation } = input; await t.step(` check .truncate($${k})`, () => { - const actual = truncate(text, limit) - assertEquals(actual, expectation) - }) - k++ + const actual = truncate(text, limit); + assertEquals(actual, expectation); + }); + k++; } }); Deno.test("check if .stripTags() works correctly", () => { - const actual = stripTags('Hello world') - assertEquals(actual, 'Hello world') + const actual = stripTags("Hello world"); + assertEquals(actual, "Hello world"); }); Deno.test("check if .escapeHTML() works correctly", () => { - const actual = escapeHTML('Hello world') - assertEquals(actual, '<a>Hello <b>world</b></a>') + const actual = escapeHTML("Hello world"); + assertEquals(actual, "<a>Hello <b>world</b></a>"); }); Deno.test("check if .unescapeHTML() works correctly", () => { - const actual = unescapeHTML('<a>Hello <b>world</b></a>') - assertEquals(actual, 'Hello world') + const actual = unescapeHTML( + "<a>Hello <b>world</b></a>", + ); + assertEquals(actual, "Hello world"); }); Deno.test("check if .ucfirst() works correctly", () => { - const actual = ucfirst('HElLo wOrLd') - assertEquals(actual, 'Hello world') + const actual = ucfirst("HElLo wOrLd"); + assertEquals(actual, "Hello world"); }); Deno.test("check if .ucwords() works correctly", () => { - const actual = ucwords('HElLo wOrLd') - assertEquals(actual, 'Hello World') + const actual = ucwords("HElLo wOrLd"); + assertEquals(actual, "Hello World"); }); Deno.test("check if .slugify() works correctly", () => { assertEquals( - slugify('Sur l\'année 2015'), - 'sur-lannee-2015' - ) + slugify("Sur l'année 2015"), + "sur-lannee-2015", + ); assertEquals( slugify('Nghị luận tác phẩm "Đường kách mệnh" của Hồ Chí Minh?!'), - 'nghi-luan-tac-pham-duong-kach-menh-cua-ho-chi-minh' - ) + "nghi-luan-tac-pham-duong-kach-menh-cua-ho-chi-minh", + ); }); diff --git a/utils/date.ts b/utils/date.ts index 1a53cb6..6bebaf5 100755 --- a/utils/date.ts +++ b/utils/date.ts @@ -38,45 +38,49 @@ const getTimeConvers = (): TimeConversions => { const isValidLocal = (hl: string): boolean => { try { - const locale = new Intl.Locale(hl) - return locale.language !== '' + const locale = new Intl.Locale(hl); + return locale.language !== ""; } catch { - return false + return false; } -} +}; export const formatDateString = (...args: any[]): string => { - const input = args[0] - const lang = isValidLocal(args[1]) ? args[1] : 'en' - const dfmt = getDateFormat() + const input = args[0]; + const lang = isValidLocal(args[1]) ? args[1] : "en"; + const dfmt = getDateFormat(); const opt = args.length >= 3 ? args[2] : args.length === 1 - ? dfmt - : isObject(args[1]) - ? args[1] - : dfmt - const dtf = new Intl.DateTimeFormat(lang, opt) - return dtf.format(new Date(input)) -} + ? dfmt + : isObject(args[1]) + ? args[1] + : dfmt; + const dtf = new Intl.DateTimeFormat(lang, opt); + return dtf.format(new Date(input)); +}; -export const formatTimeAgo = (input: any, lang: string = 'en', justnow: string = 'just now'): string => { - const t: number = (new Date(input)).getTime() - let delta: number = Date.now() - t - const tcv = getTimeConvers() +export const formatTimeAgo = ( + input: any, + lang: string = "en", + justnow: string = "just now", +): string => { + const t: number = (new Date(input)).getTime(); + let delta: number = Date.now() - t; + const tcv = getTimeConvers(); if (delta <= tcv.second) { - return justnow + return justnow; } - let unit: any = 'second' + let unit: any = "second"; for (const key in tcv) { if (delta < tcv[key as keyof typeof tcv]) { - break + break; } else { - unit = key - delta /= tcv[key as keyof typeof tcv] + unit = key; + delta /= tcv[key as keyof typeof tcv]; } } - delta = Math.floor(delta) - const rel = new Intl.RelativeTimeFormat(lang) - return rel.format(-delta, unit) -} + delta = Math.floor(delta); + const rel = new Intl.RelativeTimeFormat(lang); + return rel.format(-delta, unit); +}; diff --git a/utils/string.ts b/utils/string.ts index 45178ed..8217012 100755 --- a/utils/string.ts +++ b/utils/string.ts @@ -56,7 +56,7 @@ export const ucwords = (s: string): string => { }; export const replaceAll = (s: string, a: string, b: string): string => { - return toString(s).replaceAll(a, b) + return toString(s).replaceAll(a, b); }; const getCharMap = (): { [key: string]: string } => {