From d339dbc5e45b364c263591a90144518eb5c48c7e Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:08:35 +0000 Subject: [PATCH 01/23] chore: improve package.json lint and format scripts --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d565153..9c9ce66 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,10 @@ "test": "jest --coverage --filter \"./jest.filter.cjs\"", "test:perf": "jest --filter \"./jest.perf.filter.cjs\"", "lint": "concurrently \"pnpm:lint:*\"", + "lint:code": "eslint .", + "lint:style": "prettier --check .", "lint:types": "tsc --noEmit", - "lint:js": "eslint src", + "format": "eslint --fix --ext .ts . && prettier --write .", "build": "concurrently \"pnpm:build:*\"", "build:types": "tsc -d --emitDeclarationOnly -p ./tsconfig.build.json", "build:js": "rollup -c rollup.config.mjs && pnpm run minify", From 7b13f34cb39bb431604076b252328abfb8ff0e89 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:10:05 +0000 Subject: [PATCH 02/23] chore(style): lint and format all code files --- src/lz4js.d.ts | 4 ++-- src/parse.ts | 8 ++++---- src/serialize.ts | 4 ++-- src/stringify.ts | 10 +++++----- test/parse.perf.test.ts | 38 ++++++++++++++++++------------------- test/parse.test.ts | 34 ++++++++++++++++----------------- test/stringify.perf.test.ts | 18 +++++++++--------- test/stringify.test.ts | 34 ++++++++++++++++----------------- test/util/timer.ts | 4 ++-- 9 files changed, 77 insertions(+), 77 deletions(-) diff --git a/src/lz4js.d.ts b/src/lz4js.d.ts index 8f45fec..d665fc3 100644 --- a/src/lz4js.d.ts +++ b/src/lz4js.d.ts @@ -1,11 +1,11 @@ declare module 'lz4js' { export function compress( buffer: number[] | Uint8Array, - maxSize?: number + maxSize?: number, ): number[] | Uint8Array; export function decompress( buffer: number[] | Uint8Array, - maxSize?: number + maxSize?: number, ): number[] | Uint8Array; } diff --git a/src/parse.ts b/src/parse.ts index fabaef4..c231eb6 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -73,7 +73,7 @@ export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, options?: ParseOptions, - typeGuard?: ParseTypeGuardFunction + typeGuard?: ParseTypeGuardFunction, ): T { let _reviver: ParseReviverFunction | undefined = undefined; if (typeof reviver === 'function') { @@ -109,7 +109,7 @@ export function parse( try { if (typeGuard?.(result) === false) { throw new Error( - 'Please throw a custom error in the type guard function to track the problems' + 'Please throw a custom error in the type guard function to track the problems', ); } } catch (err: unknown) { @@ -125,7 +125,7 @@ export function parse( function unminifyKeys( obj: any, keyMap?: Record, - level?: number + level?: number, ): any { let _level: number; if (level == null || level === 0) { @@ -165,7 +165,7 @@ function unminifyKeys( export function decompressString(str: string): string { try { return new TextDecoder().decode( - Uint8Array.from(decompress(Base64.toUint8Array(str))) + Uint8Array.from(decompress(Base64.toUint8Array(str))), ); } catch (err: unknown) { // str is not a base64 encoded string of a byte array diff --git a/src/serialize.ts b/src/serialize.ts index 2b616e6..231713b 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -12,7 +12,7 @@ export function serialize(obj: any) { extended: { enable: false }, decompress: { enable: false }, unminify: { enable: false }, - } + }, ); } @@ -27,6 +27,6 @@ export function deserialize(obj: any) { extended: { enable: true, relaxed: true }, decompress: { enable: false }, unminify: { enable: false }, - } + }, ); } diff --git a/src/stringify.ts b/src/stringify.ts index 91075ef..2dd6994 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -11,7 +11,7 @@ export type StringifyReplacerArray = (string | number)[]; export type StringifyReplacerFunction = ( this: any, key: string, - value: any + value: any, ) => any; export interface StringifyOptions { @@ -86,7 +86,7 @@ export function stringify( | StringifyOptions | null, space?: string | number, - options?: StringifyOptions + options?: StringifyOptions, ): string { let _replacer: any = undefined; if (Array.isArray(replacer)) { @@ -199,7 +199,7 @@ function getMinifyKeyMap(obj: T): Record { function findAllJsonKeys( obj: any, - keyCounts: Record = {} + keyCounts: Record = {}, ): Record { if (Array.isArray(obj)) { obj.forEach((o) => { @@ -219,7 +219,7 @@ function findAllJsonKeys( function minifyKeys( obj: any, keyMap: Record, - level?: number + level?: number, ): any { let _level: number; if (level == null || level === 0) { @@ -250,6 +250,6 @@ function minifyKeys( export function compressString(str: string): string { return Base64.fromUint8Array( compress(new TextEncoder().encode(str)) as Uint8Array, - true + true, ); } diff --git a/test/parse.perf.test.ts b/test/parse.perf.test.ts index 1eb39c9..282138c 100644 --- a/test/parse.perf.test.ts +++ b/test/parse.perf.test.ts @@ -5,27 +5,27 @@ import { formatDuration, timer } from './util/index.js'; const test = (data: any): void => { const [original, originalStringifyDuration] = timer(() => - JSON.stringify(data) + JSON.stringify(data), ); const [, originalDuration] = timer(() => JSON.parse(original)); const [basic, basicStringifyDuration] = timer(() => JsonKit.stringify(data)); const [, basicDuration] = timer(() => - JsonKit.parse(basic, { unminify: false, decompress: false }) + JsonKit.parse(basic, { unminify: false, decompress: false }), ); const [minified, minifiedStringifyDuration] = timer(() => - JsonKit.stringify(data, { minify: true }) + JsonKit.stringify(data, { minify: true }), ); const [, unminifiedDuration] = timer(() => - JsonKit.parse(minified, { unminify: true, decompress: false }) + JsonKit.parse(minified, { unminify: true, decompress: false }), ); const [compressed, compressedStringifyDuration] = timer(() => - JsonKit.stringify(data, { compress: true }) + JsonKit.stringify(data, { compress: true }), ); const [, decompressedDuration] = timer(() => - JsonKit.parse(compressed, { unminify: false, decompress: true }) + JsonKit.parse(compressed, { unminify: false, decompress: true }), ); const [minifiedAndCompressed, minifiedAndCompressedStringifyDuration] = timer( @@ -33,21 +33,21 @@ const test = (data: any): void => { JsonKit.stringify(data, { minify: true, compress: true, - }) + }), ); const [, unminifiedAndDecompressedDuration] = timer(() => JsonKit.parse(minifiedAndCompressed, { unminify: true, decompress: true, - }) + }), ); console.info( `baseline: [parse: ${formatDuration( - originalDuration + originalDuration, )} (±0.00%)] [stringify: ${formatDuration( - originalStringifyDuration - )} (±0.00%)]` + originalStringifyDuration, + )} (±0.00%)]`, ); console.info( @@ -58,7 +58,7 @@ const test = (data: any): void => { ((basicStringifyDuration - originalStringifyDuration) / originalStringifyDuration) * 100 - ).toFixed(2)}%)]` + ).toFixed(2)}%)]`, ); console.info( @@ -66,12 +66,12 @@ const test = (data: any): void => { ((unminifiedDuration - originalDuration) / originalDuration) * 100 ).toFixed(2)}%)] [stringify: ${formatDuration( - minifiedStringifyDuration + minifiedStringifyDuration, )} (${( ((minifiedStringifyDuration - originalStringifyDuration) / originalStringifyDuration) * 100 - ).toFixed(2)}%)]` + ).toFixed(2)}%)]`, ); console.info( @@ -79,28 +79,28 @@ const test = (data: any): void => { ((decompressedDuration - originalDuration) / originalDuration) * 100 ).toFixed(2)}%)] [stringify: ${formatDuration( - compressedStringifyDuration + compressedStringifyDuration, )} (${( ((compressedStringifyDuration - originalStringifyDuration) / originalStringifyDuration) * 100 - ).toFixed(2)}%)]` + ).toFixed(2)}%)]`, ); console.info( `unminify + decompress: [parse: ${formatDuration( - unminifiedAndDecompressedDuration + unminifiedAndDecompressedDuration, )} (${( ((unminifiedAndDecompressedDuration - originalDuration) / originalDuration) * 100 ).toFixed(2)}%)] [stringify: ${formatDuration( - minifiedAndCompressedStringifyDuration + minifiedAndCompressedStringifyDuration, )} (${( ((minifiedAndCompressedStringifyDuration - originalStringifyDuration) / originalStringifyDuration) * 100 - ).toFixed(2)}%)]` + ).toFixed(2)}%)]`, ); }; diff --git a/test/parse.test.ts b/test/parse.test.ts index f0bc1b5..9f07e48 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -13,7 +13,7 @@ describe('[parse] basic', () => { expect(JsonKit.parse(text)).toStrictEqual(JSON.parse(text)); expect(JsonKit.parse(text, reviver)).toStrictEqual( - JSON.parse(text, reviver) + JSON.parse(text, reviver), ); }); @@ -21,15 +21,15 @@ describe('[parse] basic', () => { const reviver: ParseReviverFunction = () => 'test'; expect( - JsonKit.parse(text, { extended: { enable: true, relaxed: false } }) + JsonKit.parse(text, { extended: { enable: true, relaxed: false } }), ).toStrictEqual(EJSON.parse(text, { relaxed: false })); expect( - JsonKit.parse(text, { extended: { enable: true, relaxed: true } }) + JsonKit.parse(text, { extended: { enable: true, relaxed: true } }), ).toStrictEqual(EJSON.parse(text, { relaxed: true })); expect(JsonKit.parse(text, reviver, { extended: true })).toStrictEqual( - EJSON.parse(text) + EJSON.parse(text), ); }); @@ -86,7 +86,7 @@ describe('[parse] unminify', () => { expect( JsonKit.parse(text, (key, value) => (key !== 'arr' ? value : undefined), { unminify: { enable: true, keyMap: undefined }, - }) + }), ).toStrictEqual({ 'very-long-name': obj['very-long-name'], normal: obj['normal'], @@ -102,7 +102,7 @@ describe('[parse] unminify', () => { enable: true, keyMap: { vln: 'very-long-name', vln2: 'very-long-name-2' }, }, - }) + }), ).toStrictEqual(obj); }); }); @@ -130,11 +130,11 @@ describe('[parse] decompress', () => { const reviver: ParseReviverFunction = () => 'test'; expect(JsonKit.parse(text, { decompress: true })).toStrictEqual( - JSON.parse(JsonKit.decompressString(text)) + JSON.parse(JsonKit.decompressString(text)), ); expect(JsonKit.parse(text, reviver, { decompress: true })).toStrictEqual( - JSON.parse(JsonKit.decompressString(text), reviver) + JSON.parse(JsonKit.decompressString(text), reviver), ); }); @@ -154,22 +154,22 @@ describe('[parse] decompress', () => { JsonKit.parse(text, { extended: { enable: true, relaxed: false }, decompress: { enable: true }, - }) + }), ).toStrictEqual( - EJSON.parse(JsonKit.decompressString(text), { relaxed: false }) + EJSON.parse(JsonKit.decompressString(text), { relaxed: false }), ); expect( JsonKit.parse(textRelaxed, { extended: { enable: true, relaxed: true }, decompress: { enable: true }, - }) + }), ).toStrictEqual( - EJSON.parse(JsonKit.decompressString(textRelaxed), { relaxed: true }) + EJSON.parse(JsonKit.decompressString(textRelaxed), { relaxed: true }), ); expect( - JsonKit.parse(textRelaxed, reviver, { extended: true, decompress: true }) + JsonKit.parse(textRelaxed, reviver, { extended: true, decompress: true }), ).toStrictEqual(EJSON.parse(JsonKit.decompressString(textRelaxed))); }); }); @@ -191,8 +191,8 @@ describe('[parse] type guard', () => { text, null, { extended: false, unminify: false, decompress: false }, - typeGuard - ) + typeGuard, + ), ).not.toThrow(); }); @@ -203,8 +203,8 @@ describe('[parse] type guard', () => { text, null, { extended: false, unminify: false, decompress: false }, - typeGuard - ) + typeGuard, + ), ).toThrow(); }); }); diff --git a/test/stringify.perf.test.ts b/test/stringify.perf.test.ts index 81e99c3..9359474 100644 --- a/test/stringify.perf.test.ts +++ b/test/stringify.perf.test.ts @@ -9,49 +9,49 @@ const test = (data: any): void => { const [basic, basicDuration] = timer(() => JsonKit.stringify(data)); const [minified, minifiedDuration] = timer(() => - JsonKit.stringify(data, { minify: true }) + JsonKit.stringify(data, { minify: true }), ); const [compressed, compressedDuration] = timer(() => - JsonKit.stringify(data, { compress: true }) + JsonKit.stringify(data, { compress: true }), ); const [minifiedAndCompressed, minifiedAndCompressedDuration] = timer(() => JsonKit.stringify(data, { minify: true, compress: true, - }) + }), ); console.info( `baseline: ${original.length} (±0.00%) [${formatDuration( - originalDuration - )}]` + originalDuration, + )}]`, ); console.info( - `basic: ${basic.length} (±0.00%) [${formatDuration(basicDuration)}]` + `basic: ${basic.length} (±0.00%) [${formatDuration(basicDuration)}]`, ); console.info( `minify: ${minified.length} (${( ((minified.length - original.length) / original.length) * 100 - ).toFixed(2)}%) [${formatDuration(minifiedDuration)}]` + ).toFixed(2)}%) [${formatDuration(minifiedDuration)}]`, ); console.info( `compress: ${compressed.length} (${( ((compressed.length - original.length) / original.length) * 100 - ).toFixed(2)}%) [${formatDuration(compressedDuration)}]` + ).toFixed(2)}%) [${formatDuration(compressedDuration)}]`, ); console.info( `minify + compress: ${minifiedAndCompressed.length} (${( ((minifiedAndCompressed.length - original.length) / original.length) * 100 - ).toFixed(2)}%) [${formatDuration(minifiedAndCompressedDuration)}]` + ).toFixed(2)}%) [${formatDuration(minifiedAndCompressedDuration)}]`, ); }; diff --git a/test/stringify.test.ts b/test/stringify.test.ts index fceaaa6..c7ab86a 100644 --- a/test/stringify.test.ts +++ b/test/stringify.test.ts @@ -30,11 +30,11 @@ describe('[stringify] basic', () => { expect(JsonKit.stringify(obj)).toEqual(JSON.stringify(obj)); expect(JsonKit.stringify(obj, replacerArr, ' ')).toEqual( - JSON.stringify(obj, replacerArr, ' ') + JSON.stringify(obj, replacerArr, ' '), ); expect(JsonKit.stringify(obj, replacerFunc, ' ')).toEqual( - JSON.stringify(obj, replacerFunc, ' ') + JSON.stringify(obj, replacerFunc, ' '), ); }); @@ -43,19 +43,19 @@ describe('[stringify] basic', () => { const replacerFunc: StringifyReplacerFunction = () => 'test'; expect( - JsonKit.stringify(obj, { extended: { enable: true, relaxed: false } }) + JsonKit.stringify(obj, { extended: { enable: true, relaxed: false } }), ).toEqual(EJSON.stringify(obj, { relaxed: false })); expect( - JsonKit.stringify(obj, { extended: { enable: true, relaxed: true } }) + JsonKit.stringify(obj, { extended: { enable: true, relaxed: true } }), ).toEqual(EJSON.stringify(obj, { relaxed: true })); expect(JsonKit.stringify(obj, replacerArr, 4, { extended: true })).toEqual( - EJSON.stringify(obj, replacerArr, 4) + EJSON.stringify(obj, replacerArr, 4), ); expect(JsonKit.stringify(obj, replacerFunc, 4, { extended: true })).toEqual( - EJSON.stringify(obj, replacerFunc, 4) + EJSON.stringify(obj, replacerFunc, 4), ); }); @@ -174,10 +174,10 @@ describe('[stringify] minify', () => { obj, ['very-long-name', 'very-long-name-2', 'normal'], 0, - { minify: { enable: true, keyMap: undefined } } - ) + { minify: { enable: true, keyMap: undefined } }, + ), ).toEqual( - '{"_jkv":{"normal":"string","a0":{"a0":"very-long-name","a1":2}},"_jkm":{"a0":"very-long-name","a1":"very-long-name-2"}}' + '{"_jkv":{"normal":"string","a0":{"a0":"very-long-name","a1":2}},"_jkm":{"a0":"very-long-name","a1":"very-long-name-2"}}', ); }); @@ -197,10 +197,10 @@ describe('[stringify] minify', () => { enable: true, keyMap: { 'very-long-name': 'vln', 'very-long-name-2': 'vln2' }, }, - } - ) + }, + ), ).toEqual( - '{"_jkv":{"number":1,"boolean":true,"null":null,"undefined":"undefined","date":"2022-12-01T13:44:15.177Z","arr":[{"vln":{"vln":"very-long-name"}},{"testKeys":{"1":"a","2":"b","3":"c","4":"d","5":"e","6":"f","7":"g","8":"h","9":"i","10":"j","11":"k","12":"l","13":"m","14":"n","15":"o","16":"p","17":"q","18":"r","19":"s","20":"t","21":"u","22":"v","23":"w","24":"x","25":"y","26":"z","a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26},"vln2":true}],"normal":"string","vln":{"vln":"very-long-name","vln2":2}},"_jkm":{"vln":"very-long-name","vln2":"very-long-name-2"}}' + '{"_jkv":{"number":1,"boolean":true,"null":null,"undefined":"undefined","date":"2022-12-01T13:44:15.177Z","arr":[{"vln":{"vln":"very-long-name"}},{"testKeys":{"1":"a","2":"b","3":"c","4":"d","5":"e","6":"f","7":"g","8":"h","9":"i","10":"j","11":"k","12":"l","13":"m","14":"n","15":"o","16":"p","17":"q","18":"r","19":"s","20":"t","21":"u","22":"v","23":"w","24":"x","25":"y","26":"z","a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26},"vln2":true}],"normal":"string","vln":{"vln":"very-long-name","vln2":2}},"_jkm":{"vln":"very-long-name","vln2":"very-long-name-2"}}', ); }); }); @@ -226,11 +226,11 @@ describe('[stringify] compress', () => { const replacer: StringifyReplacerFunction = () => 'test'; expect(JsonKit.stringify(obj, { compress: true })).toEqual( - JsonKit.compressString(JSON.stringify(obj)) + JsonKit.compressString(JSON.stringify(obj)), ); expect(JsonKit.stringify(obj, replacer, ' ', { compress: true })).toEqual( - JsonKit.compressString(JSON.stringify(obj, replacer, ' ')) + JsonKit.compressString(JSON.stringify(obj, replacer, ' ')), ); }); @@ -241,18 +241,18 @@ describe('[stringify] compress', () => { JsonKit.stringify(obj, { extended: { enable: true, relaxed: false }, compress: { enable: true }, - }) + }), ).toEqual(JsonKit.compressString(EJSON.stringify(obj, { relaxed: false }))); expect( JsonKit.stringify(obj, { extended: { enable: true, relaxed: true }, compress: { enable: true }, - }) + }), ).toEqual(JsonKit.compressString(EJSON.stringify(obj, { relaxed: true }))); expect( - JsonKit.stringify(obj, replacer, 4, { extended: true, compress: true }) + JsonKit.stringify(obj, replacer, 4, { extended: true, compress: true }), ).toEqual(JsonKit.compressString(EJSON.stringify(obj, replacer, 4))); }); }); diff --git a/test/util/timer.ts b/test/util/timer.ts index 6dcd5fa..14604cd 100644 --- a/test/util/timer.ts +++ b/test/util/timer.ts @@ -4,10 +4,10 @@ */ export function timer(target: () => T): [T, number]; export async function timer>( - target: () => T + target: () => T, ): Promise<[Awaited, number]>; export function timer( - target: () => any + target: () => any, ): [any, number] | Promise<[any, number]> { const start = performance.now(); const result = target(); From bba21a6c7b0cd0c002d3d59eb480ff7317aa8688 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:16:33 +0000 Subject: [PATCH 03/23] chore: prefer use type over interface with eslint --- .eslintrc.json | 1 + src/parse.ts | 8 ++++---- src/stringify.ts | 8 ++++---- test/parse.test.ts | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index deef486..bfbd71f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,6 +5,7 @@ "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], "rules": { "@typescript-eslint/consistent-type-imports": "error", + "@typescript-eslint/consistent-type-definitions": ["error", "type"], "@typescript-eslint/no-unused-vars": [ "error", { diff --git a/src/parse.ts b/src/parse.ts index c231eb6..7a52d60 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -5,17 +5,17 @@ import { decompress } from 'lz4js'; export type ParseTypeGuardFunction = (obj: any) => obj is T; export type ParseReviverFunction = (this: any, key: string, value: any) => any; -export interface ParseOptions { +export type ParseOptions = { extended?: boolean | { enable: boolean; relaxed?: boolean }; unminify?: boolean | { enable: boolean; keyMap?: Record }; decompress?: boolean | { enable: boolean }; -} +}; -interface _ParseOptions { +type _ParseOptions = { extended: { enable: boolean; relaxed: boolean }; unminify: { enable: boolean; keyMap?: Record }; decompress: { enable: boolean }; -} +}; const defaultOptions: _ParseOptions = { extended: { enable: false, relaxed: true }, diff --git a/src/stringify.ts b/src/stringify.ts index 2dd6994..b051aaa 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -14,17 +14,17 @@ export type StringifyReplacerFunction = ( value: any, ) => any; -export interface StringifyOptions { +export type StringifyOptions = { extended?: boolean | { enable: boolean; relaxed?: boolean }; minify?: boolean | { enable: boolean; keyMap?: Record }; compress?: boolean | { enable: boolean }; -} +}; -interface _StringifyOptions { +type _StringifyOptions = { extended: { enable: boolean; relaxed: boolean }; minify: { enable: boolean; keyMap: Record | undefined }; compress: { enable: boolean }; -} +}; const defaultOptions: _StringifyOptions = { extended: { enable: false, relaxed: true }, diff --git a/test/parse.test.ts b/test/parse.test.ts index 9f07e48..ae786f6 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -175,10 +175,10 @@ describe('[parse] decompress', () => { }); describe('[parse] type guard', () => { - interface TestObject { + type TestObject = { a: number; b: string; - } + }; const typeGuard = (obj: any): obj is TestObject => { return typeof obj?.a === 'number' && typeof obj?.b === 'string'; From 33dd7ba2a495aa6b6c1aa93d3fb2276c5dceb4b6 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:24:31 +0000 Subject: [PATCH 04/23] chore(types): use generic TypeGuardFunction type --- src/parse.ts | 4 ++-- src/types.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 src/types.ts diff --git a/src/parse.ts b/src/parse.ts index 7a52d60..67a3f7c 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,8 +1,8 @@ import { EJSON } from 'bson'; import { Base64 } from 'js-base64'; import { decompress } from 'lz4js'; +import type { TypeGuardFunction } from './types.js'; -export type ParseTypeGuardFunction = (obj: any) => obj is T; export type ParseReviverFunction = (this: any, key: string, value: any) => any; export type ParseOptions = { @@ -73,7 +73,7 @@ export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, options?: ParseOptions, - typeGuard?: ParseTypeGuardFunction, + typeGuard?: TypeGuardFunction, ): T { let _reviver: ParseReviverFunction | undefined = undefined; if (typeof reviver === 'function') { diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..b55c433 --- /dev/null +++ b/src/types.ts @@ -0,0 +1 @@ +export type TypeGuardFunction = (obj: any) => obj is T; From 6542bf6afee31b8066cc8ff66320a7986e2e482d Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:27:10 +0000 Subject: [PATCH 05/23] feat(serialize): add generic type and typeGuard parameter to serialize and deserialize --- src/serialize.ts | 9 +++++++-- test/serialize.test.ts | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/serialize.ts b/src/serialize.ts index 231713b..ea57909 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -1,32 +1,37 @@ import { parse } from './parse.js'; import { stringify } from './stringify.js'; +import type { TypeGuardFunction } from './types.js'; -export function serialize(obj: any) { +export function serialize(obj: any, typeGuard?: TypeGuardFunction) { return parse( stringify(obj, { extended: { enable: true, relaxed: true }, compress: { enable: false }, minify: { enable: false }, }), + null, { extended: { enable: false }, decompress: { enable: false }, unminify: { enable: false }, }, + typeGuard, ); } -export function deserialize(obj: any) { +export function deserialize(obj: any, typeGuard?: TypeGuardFunction) { return parse( stringify(obj, { extended: { enable: false }, compress: { enable: false }, minify: { enable: false }, }), + null, { extended: { enable: true, relaxed: true }, decompress: { enable: false }, unminify: { enable: false }, }, + typeGuard, ); } diff --git a/test/serialize.test.ts b/test/serialize.test.ts index 08a13b4..64c555e 100644 --- a/test/serialize.test.ts +++ b/test/serialize.test.ts @@ -7,7 +7,7 @@ describe('[serialize] serialize', () => { }; it('serialize should transform JavaScript object into Extended JSON object', () => { - const serializedObj = JsonKit.serialize(obj); + const serializedObj = JsonKit.serialize<{ date: unknown }>(obj); expect(serializedObj.date).toHaveProperty('$date'); }); }); @@ -18,7 +18,7 @@ describe('[serialize] deserialize', () => { }; it('deserialize should transform Extended JSON object into JavaScript object', () => { - const deserializedObj = JsonKit.deserialize(obj); + const deserializedObj = JsonKit.deserialize<{ date: unknown }>(obj); expect(deserializedObj.date).toBeInstanceOf(Date); }); }); From 759eaa7024a0d4d91f7d4682f78626a7878d92e9 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:31:30 +0000 Subject: [PATCH 06/23] fix(types): use namespace export for better consumption --- src/external.ts | 9 +++++++++ src/index.ts | 10 +--------- 2 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 src/external.ts diff --git a/src/external.ts b/src/external.ts new file mode 100644 index 0000000..0e257c2 --- /dev/null +++ b/src/external.ts @@ -0,0 +1,9 @@ +export * as Parse from './parse.js'; +export * as Serialize from './serialize.js'; +export * as Stringify from './stringify.js'; +export * as Types from './types.js'; + +export * from './parse.js'; +export * from './serialize.js'; +export * from './stringify.js'; +export * from './types.js'; diff --git a/src/index.ts b/src/index.ts index b0b19a7..80f2115 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1 @@ -import * as Parse from './parse.js'; -import * as Serialize from './serialize.js'; -import * as Stringify from './stringify.js'; - -export const JsonKit = { - ...Parse, - ...Serialize, - ...Stringify, -} as const; +export * as JsonKit from './external.js'; From 95ef4b86b74195c5651784e54d471dfe12ce7a93 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:39:41 +0000 Subject: [PATCH 07/23] feat(signature): provide overloads for stringify and parse BREAKING CHANGES --- src/parse.ts | 27 +++++++++++++++++++++++---- src/stringify.ts | 19 ++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index 67a3f7c..bc07d6c 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -23,7 +23,7 @@ const defaultOptions: _ParseOptions = { decompress: { enable: true }, }; -function mergeWithDefaultOptions(input?: ParseOptions): _ParseOptions { +function mergeWithDefaultOptions(input?: ParseOptions | null): _ParseOptions { if (input == null) return defaultOptions; if (input.extended == null) { @@ -69,13 +69,24 @@ function mergeWithDefaultOptions(input?: ParseOptions): _ParseOptions { return input as _ParseOptions; } +export function parse( + text: string, + reviver?: ParseReviverFunction | null, + options?: ParseOptions | null, + typeGuard?: TypeGuardFunction, +): T; +export function parse( + text: string, + options?: ParseOptions | null, + typeGuard?: TypeGuardFunction, +): T; export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, - options?: ParseOptions, + options?: ParseOptions | TypeGuardFunction | null, typeGuard?: TypeGuardFunction, ): T { - let _reviver: ParseReviverFunction | undefined = undefined; + let _reviver: ParseReviverFunction | undefined; if (typeof reviver === 'function') { _reviver = reviver satisfies ParseReviverFunction; } else if (options == null) { @@ -83,6 +94,14 @@ export function parse( reviver = undefined; } + let _typeGuard: TypeGuardFunction | undefined; + if (typeof options === 'function') { + _typeGuard = options satisfies TypeGuardFunction; + options = undefined; + } else { + _typeGuard = typeGuard; + } + const _options = mergeWithDefaultOptions(options); if (_options.decompress.enable) { @@ -107,7 +126,7 @@ export function parse( } try { - if (typeGuard?.(result) === false) { + if (_typeGuard?.(result) === false) { throw new Error( 'Please throw a custom error in the type guard function to track the problems', ); diff --git a/src/stringify.ts b/src/stringify.ts index b051aaa..03ebafd 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -32,7 +32,9 @@ const defaultOptions: _StringifyOptions = { compress: { enable: false }, }; -function mergeWithDefaultOptions(input?: StringifyOptions): _StringifyOptions { +function mergeWithDefaultOptions( + input?: StringifyOptions | null, +): _StringifyOptions { if (input == null) return defaultOptions; if (input.extended == null) { @@ -78,6 +80,13 @@ function mergeWithDefaultOptions(input?: StringifyOptions): _StringifyOptions { return input as _StringifyOptions; } +export function stringify( + obj: any, + replacer?: StringifyReplacerArray | StringifyReplacerFunction | null, + space?: string | number | null, + options?: StringifyOptions | null, +): string; +export function stringify(obj: any, options?: StringifyOptions | null): string; export function stringify( obj: any, replacer?: @@ -85,8 +94,8 @@ export function stringify( | StringifyReplacerFunction | StringifyOptions | null, - space?: string | number, - options?: StringifyOptions, + space?: string | number | null, + options?: StringifyOptions | null, ): string { let _replacer: any = undefined; if (Array.isArray(replacer)) { @@ -143,11 +152,11 @@ export function stringify( let result: string; if (_options.extended.enable) { - result = EJSON.stringify(obj, _replacer, space, { + result = EJSON.stringify(obj, _replacer, space ?? undefined, { relaxed: _options.extended.relaxed, }); } else { - result = JSON.stringify(obj, _replacer, space); + result = JSON.stringify(obj, _replacer, space ?? undefined); } if (_options.compress.enable) { From fb58f123ec7c061c447ff3374fbefc9dd68875a8 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:44:19 +0000 Subject: [PATCH 08/23] chore(types): remove = any as the default value of generic types --- src/parse.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index bc07d6c..d3ff6cc 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -69,18 +69,18 @@ function mergeWithDefaultOptions(input?: ParseOptions | null): _ParseOptions { return input as _ParseOptions; } -export function parse( +export function parse( text: string, reviver?: ParseReviverFunction | null, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; -export function parse( +export function parse( text: string, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; -export function parse( +export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, options?: ParseOptions | TypeGuardFunction | null, From 20cc6102b98d2cc7ae1a9c6ecc5a1673602552f8 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:46:58 +0000 Subject: [PATCH 09/23] chore(types): remove redundant generic type --- src/stringify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stringify.ts b/src/stringify.ts index 03ebafd..9ed1478 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -173,7 +173,7 @@ function reverseKeyMap(keyMap: Record): Record { return reversedKeyMap; } -function getMinifyKeyMap(obj: T): Record { +function getMinifyKeyMap(obj: any): Record { const keyMap: Record = {}; const keyCounts: Readonly> = findAllJsonKeys(obj); From 2e231921d5bac7f96c8a4341ca08d4e1ece590c9 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:50:24 +0000 Subject: [PATCH 10/23] chore: remove lint-staged --- .husky/pre-commit | 2 +- package.json | 4 -- pnpm-lock.yaml | 165 ---------------------------------------------- 3 files changed, 1 insertion(+), 170 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index e04bf1f..e9884e8 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -pnpm run lint && pnpm exec lint-staged +pnpm run lint diff --git a/package.json b/package.json index 9c9ce66..6178741 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,6 @@ "eslint": "8.45.0", "husky": "8.0.3", "jest": "29.6.1", - "lint-staged": "13.2.3", "prettier": "3.0.0", "renovate": "36.8.11", "rollup": "3.26.2", @@ -104,8 +103,5 @@ "marked-terminal@5>marked": "5" } } - }, - "lint-staged": { - "*": "prettier --write" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff9d7e4..a978f91 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -79,9 +79,6 @@ devDependencies: jest: specifier: 29.6.1 version: 29.6.1(@types/node@20.4.2)(ts-node@10.9.1) - lint-staged: - specifier: 13.2.3 - version: 13.2.3 prettier: specifier: 3.0.0 version: 3.0.0 @@ -3992,11 +3989,6 @@ packages: safer-buffer: 2.1.2 dev: true - /astral-regex@2.0.0: - resolution: {integrity: sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==} - engines: {node: '>=8'} - dev: true - /auth-header@1.0.0: resolution: {integrity: sha512-CPPazq09YVDUNNVWo4oSPTQmtwIzHusZhQmahCKvIsk0/xH6U3QsMAv3sM+7+Q0B1K2KJ/Q38OND317uXs4NHA==} dev: true @@ -4385,13 +4377,6 @@ packages: escape-string-regexp: 5.0.0 dev: true - /cli-cursor@3.1.0: - resolution: {integrity: sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==} - engines: {node: '>=8'} - dependencies: - restore-cursor: 3.1.0 - dev: true - /cli-table3@0.6.3: resolution: {integrity: sha512-w5Jac5SykAeZJKntOxJCrm63Eg5/4dhMWIcuTbo9rpE+brgaSZo0RuNJZeOyMgsUdhDeojvgyQLmjI+K50ZGyg==} engines: {node: 10.* || >= 12.*} @@ -4401,22 +4386,6 @@ packages: '@colors/colors': 1.5.0 dev: true - /cli-truncate@2.1.0: - resolution: {integrity: sha512-n8fOixwDD6b/ObinzTrp1ZKFzbgvKZvuz/TvejnLn1aQfC6r52XEx85FmuC+3HI+JM7coBRXUvNqEU2PHVrHpg==} - engines: {node: '>=8'} - dependencies: - slice-ansi: 3.0.0 - string-width: 4.2.3 - dev: true - - /cli-truncate@3.1.0: - resolution: {integrity: sha512-wfOBkjXteqSnI59oPcJkcPl/ZmwvMMOj340qUIY1SKZCv0B9Cf4D4fAucRkIKQmsIuYK3x1rrgU7MeGRruiuiA==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - dependencies: - slice-ansi: 5.0.0 - string-width: 5.1.2 - dev: true - /clipanion@3.2.0-rc.4(typanion@3.13.0): resolution: {integrity: sha512-wkW5IYIK63i2aSmFr8EoRjEpZmy3KFXezDn4K8dBct7pq0hWWDFUoqwXTMIXWyBIEJFekmZ9v5wX+JtRBMZbOA==} peerDependencies: @@ -4481,15 +4450,6 @@ packages: dev: true optional: true - /colorette@2.0.20: - resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==} - dev: true - - /commander@10.0.1: - resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==} - engines: {node: '>=14'} - dev: true - /commander@11.0.0: resolution: {integrity: sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==} engines: {node: '>=16'} @@ -6035,11 +5995,6 @@ packages: engines: {node: '>=8'} dev: true - /is-fullwidth-code-point@4.0.0: - resolution: {integrity: sha512-O4L094N2/dZ7xqVdrXhh9r1KODPJpFms8B5sGdJLPy664AgvXsreZUyCQQNItZRDlYug4xStLjNp/sz3HvBowQ==} - engines: {node: '>=12'} - dev: true - /is-generator-fn@2.1.0: resolution: {integrity: sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==} engines: {node: '>=6'} @@ -6798,11 +6753,6 @@ packages: immediate: 3.0.6 dev: true - /lilconfig@2.1.0: - resolution: {integrity: sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==} - engines: {node: '>=10'} - dev: true - /lines-and-columns@1.2.4: resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==} dev: true @@ -6818,48 +6768,6 @@ packages: uc.micro: 1.0.6 dev: true - /lint-staged@13.2.3: - resolution: {integrity: sha512-zVVEXLuQIhr1Y7R7YAWx4TZLdvuzk7DnmrsTNL0fax6Z3jrpFcas+vKbzxhhvp6TA55m1SQuWkpzI1qbfDZbAg==} - engines: {node: ^14.13.1 || >=16.0.0} - hasBin: true - dependencies: - chalk: 5.2.0 - cli-truncate: 3.1.0 - commander: 10.0.1 - debug: 4.3.4 - execa: 7.1.1 - lilconfig: 2.1.0 - listr2: 5.0.8 - micromatch: 4.0.5 - normalize-path: 3.0.0 - object-inspect: 1.12.3 - pidtree: 0.6.0 - string-argv: 0.3.2 - yaml: 2.3.1 - transitivePeerDependencies: - - enquirer - - supports-color - dev: true - - /listr2@5.0.8: - resolution: {integrity: sha512-mC73LitKHj9w6v30nLNGPetZIlfpUniNSsxxrbaPcWOjDb92SHPzJPi/t+v1YC/lxKz/AJ9egOjww0qUuFxBpA==} - engines: {node: ^14.13.1 || >=16.0.0} - peerDependencies: - enquirer: '>= 2.3.0 < 3' - peerDependenciesMeta: - enquirer: - optional: true - dependencies: - cli-truncate: 2.1.0 - colorette: 2.0.20 - log-update: 4.0.0 - p-map: 4.0.0 - rfdc: 1.3.0 - rxjs: 7.8.1 - through: 2.3.8 - wrap-ansi: 7.0.0 - dev: true - /load-json-file@4.0.0: resolution: {integrity: sha512-Kx8hMakjX03tiGTLAIdJ+lL0htKnXjEZN6hk/tozf/WOuYGdZBJrZ+rCJRbVCugsjB3jMLn9746NsQIf5VjBMw==} engines: {node: '>=4'} @@ -6981,16 +6889,6 @@ packages: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} dev: true - /log-update@4.0.0: - resolution: {integrity: sha512-9fkkDevMefjg0mmzWFBW8YkFP91OrizzkW3diF7CpG+S2EYdy4+TVfGwz1zeF8x7hCx1ovSPTOE9Ngib74qqUg==} - engines: {node: '>=10'} - dependencies: - ansi-escapes: 4.3.2 - cli-cursor: 3.1.0 - slice-ansi: 4.0.0 - wrap-ansi: 6.2.0 - dev: true - /longest-streak@2.0.4: resolution: {integrity: sha512-vM6rUVCVUJJt33bnmHiZEvr7wPT78ztX7rojL+LW51bHtLh6HTjx84LA5W4+oa6aKEJA7jJu5LR6vQRBpA5DVg==} dev: true @@ -8028,12 +7926,6 @@ packages: engines: {node: '>=8.6'} dev: true - /pidtree@0.6.0: - resolution: {integrity: sha512-eG2dWTVw5bzqGRztnHExczNxt5VGsE6OwTeCG3fdUf9KBsZzO3R5OIIIzWR+iZA0NtZ+RDVdaoE2dK1cn6jH4g==} - engines: {node: '>=0.10'} - hasBin: true - dev: true - /pify@3.0.0: resolution: {integrity: sha512-C3FsVNH1udSEX48gGX1xfvwTWfsYWj5U+8/uK15BGzIGrKoUpghX8hWZwa/OFnakBiiVNmBvemTJR5mcy7iPcg==} engines: {node: '>=4'} @@ -8546,14 +8438,6 @@ packages: lowercase-keys: 2.0.0 dev: true - /restore-cursor@3.1.0: - resolution: {integrity: sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==} - engines: {node: '>=8'} - dependencies: - onetime: 5.1.2 - signal-exit: 3.0.7 - dev: true - /retry@0.12.0: resolution: {integrity: sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow==} engines: {node: '>= 4'} @@ -8565,10 +8449,6 @@ packages: engines: {iojs: '>=1.0.0', node: '>=0.10.0'} dev: true - /rfdc@1.3.0: - resolution: {integrity: sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==} - dev: true - /rimraf@2.4.5: resolution: {integrity: sha512-J5xnxTyqaiw06JjMftq7L9ouA448dw/E7dKghkP9WpKNuwmARNNg+Gk8/u5ryb9N/Yo2+z3MCwuqFK/+qPOPfQ==} hasBin: true @@ -8820,32 +8700,6 @@ packages: engines: {node: '>=12'} dev: true - /slice-ansi@3.0.0: - resolution: {integrity: sha512-pSyv7bSTC7ig9Dcgbw9AuRNUb5k5V6oDudjZoMBSr13qpLBG7tB+zgCkARjq7xIUgdz5P1Qe8u+rSGdouOOIyQ==} - engines: {node: '>=8'} - dependencies: - ansi-styles: 4.3.0 - astral-regex: 2.0.0 - is-fullwidth-code-point: 3.0.0 - dev: true - - /slice-ansi@4.0.0: - resolution: {integrity: sha512-qMCMfhY040cVHT43K9BFygqYbUPFZKHOg7K73mtTWJRb8pyP3fzf4Ixd5SzdEJQ6MRUg/WBnOLxghZtKKurENQ==} - engines: {node: '>=10'} - dependencies: - ansi-styles: 4.3.0 - astral-regex: 2.0.0 - is-fullwidth-code-point: 3.0.0 - dev: true - - /slice-ansi@5.0.0: - resolution: {integrity: sha512-FC+lgizVPfie0kkhqUScwRu1O/lF6NOgJmlCgK+/LYxDCTk8sGelYaHDhFcDN+Sn3Cv+3VSa4Byeo+IMCzpMgQ==} - engines: {node: '>=12'} - dependencies: - ansi-styles: 6.2.1 - is-fullwidth-code-point: 4.0.0 - dev: true - /slugify@1.6.6: resolution: {integrity: sha512-h+z7HKHYXj6wJU+AnS/+IH8Uh9fdcX1Lrhg1/VMdf9PwoBQXFcXiAdsy2tSK0P6gKwJLXp02r90ahUCqHk9rrw==} engines: {node: '>=8.0.0'} @@ -9000,11 +8854,6 @@ packages: stream-to-array: 2.3.0 dev: true - /string-argv@0.3.2: - resolution: {integrity: sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==} - engines: {node: '>=0.6.19'} - dev: true - /string-length@4.0.2: resolution: {integrity: sha512-+l6rNN5fYHNhZZy41RXsYptCjA2Igmq4EG7kZAYFQI1E1VTXarr6ZPXBg6eq7Y6eK4FEhY6AJlyuFIb/v/S0VQ==} engines: {node: '>=10'} @@ -9661,15 +9510,6 @@ packages: resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==} dev: true - /wrap-ansi@6.2.0: - resolution: {integrity: sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==} - engines: {node: '>=8'} - dependencies: - ansi-styles: 4.3.0 - string-width: 4.2.3 - strip-ansi: 6.0.1 - dev: true - /wrap-ansi@7.0.0: resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==} engines: {node: '>=10'} @@ -9749,11 +9589,6 @@ packages: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} dev: true - /yaml@2.3.1: - resolution: {integrity: sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==} - engines: {node: '>= 14'} - dev: true - /yargs-parser@18.1.3: resolution: {integrity: sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==} engines: {node: '>=6'} From 46ec3963384722f7e73c2c15b115c8431bb324f2 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:51:00 +0000 Subject: [PATCH 11/23] chore(types): use const type parameter for function generic type --- src/parse.ts | 6 +++--- src/serialize.ts | 7 +++++-- test/util/timer.ts | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index d3ff6cc..988a4f5 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -69,18 +69,18 @@ function mergeWithDefaultOptions(input?: ParseOptions | null): _ParseOptions { return input as _ParseOptions; } -export function parse( +export function parse( text: string, reviver?: ParseReviverFunction | null, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; -export function parse( +export function parse( text: string, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; -export function parse( +export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, options?: ParseOptions | TypeGuardFunction | null, diff --git a/src/serialize.ts b/src/serialize.ts index ea57909..88772f3 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -2,7 +2,7 @@ import { parse } from './parse.js'; import { stringify } from './stringify.js'; import type { TypeGuardFunction } from './types.js'; -export function serialize(obj: any, typeGuard?: TypeGuardFunction) { +export function serialize(obj: any, typeGuard?: TypeGuardFunction) { return parse( stringify(obj, { extended: { enable: true, relaxed: true }, @@ -19,7 +19,10 @@ export function serialize(obj: any, typeGuard?: TypeGuardFunction) { ); } -export function deserialize(obj: any, typeGuard?: TypeGuardFunction) { +export function deserialize( + obj: any, + typeGuard?: TypeGuardFunction, +) { return parse( stringify(obj, { extended: { enable: false }, diff --git a/test/util/timer.ts b/test/util/timer.ts index 14604cd..1bffb63 100644 --- a/test/util/timer.ts +++ b/test/util/timer.ts @@ -2,8 +2,8 @@ * @param target target function to measure * @returns Array of [result, durationInMs] */ -export function timer(target: () => T): [T, number]; -export async function timer>( +export function timer(target: () => T): [T, number]; +export async function timer>( target: () => T, ): Promise<[Awaited, number]>; export function timer( From 04577d35dcbe2a68b22b4c86897bd8e14d3e7525 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:56:26 +0000 Subject: [PATCH 12/23] fix(serialize): add missing return type --- src/serialize.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/serialize.ts b/src/serialize.ts index 88772f3..65be8c8 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -2,7 +2,10 @@ import { parse } from './parse.js'; import { stringify } from './stringify.js'; import type { TypeGuardFunction } from './types.js'; -export function serialize(obj: any, typeGuard?: TypeGuardFunction) { +export function serialize( + obj: any, + typeGuard?: TypeGuardFunction, +): T { return parse( stringify(obj, { extended: { enable: true, relaxed: true }, @@ -22,7 +25,7 @@ export function serialize(obj: any, typeGuard?: TypeGuardFunction) { export function deserialize( obj: any, typeGuard?: TypeGuardFunction, -) { +): T { return parse( stringify(obj, { extended: { enable: false }, From 2b7eaaacb3ecca2398e9156788c4a25e8c4c2d4e Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:58:37 +0000 Subject: [PATCH 13/23] chore(test): add test for export members --- test/index.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/index.test.ts diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..1cc1d3b --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,21 @@ +import { JsonKit } from '../src/index.js'; + +describe('[export] exports', () => { + const exportedMembers = [ + 'Parse', + 'Serialize', + 'Stringify', + 'parse', + 'decompressString', + 'serialize', + 'deserialize', + 'stringify', + 'compressString', + ] as const; + + it('named export should contain all the exported members', () => { + exportedMembers.forEach((exportedMember) => { + expect(JsonKit).toHaveProperty(exportedMember); + }); + }); +}); From f068d5442ee0beb5833295ac85dbcfaa674fe356 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 08:59:17 +0000 Subject: [PATCH 14/23] chore(test): update parse.test.ts --- test/parse.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/parse.test.ts b/test/parse.test.ts index ae786f6..fe75587 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -181,7 +181,8 @@ describe('[parse] type guard', () => { }; const typeGuard = (obj: any): obj is TestObject => { - return typeof obj?.a === 'number' && typeof obj?.b === 'string'; + const _obj: Partial | null | undefined = obj; + return typeof _obj?.a === 'number' && typeof _obj?.b === 'string'; }; it('parse with type guard passing', () => { @@ -189,7 +190,6 @@ describe('[parse] type guard', () => { expect(() => JsonKit.parse( text, - null, { extended: false, unminify: false, decompress: false }, typeGuard, ), @@ -201,7 +201,6 @@ describe('[parse] type guard', () => { expect(() => JsonKit.parse( text, - null, { extended: false, unminify: false, decompress: false }, typeGuard, ), From dd818e0634700a0876081588531bbe02bd375cff Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 09:07:35 +0000 Subject: [PATCH 15/23] chore: remove empty code block in README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index ead6ae3..e2aff2c 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,3 @@ import { JsonKit } from "@kit-p/json-kit"; ## License MIT - -``` - -``` From 5879daef4125ed3c3dc5c9fab271581dd53316c7 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 09:10:27 +0000 Subject: [PATCH 16/23] chore(docs): add TSDoc with TypeDoc for documentation site --- .gitignore | 3 + package.json | 2 + pnpm-lock.yaml | 48 +++++++++++++ src/constants.ts | 16 +++++ src/index.ts | 11 +++ src/lz4js.d.ts | 11 +++ src/parse.ts | 174 ++++++++++++++++++++++++++++++++++++++++++++++- src/serialize.ts | 30 ++++++++ src/stringify.ts | 154 ++++++++++++++++++++++++++++++++++++++++- src/types.ts | 17 +++++ typedoc.json | 14 ++++ 11 files changed, 474 insertions(+), 6 deletions(-) create mode 100644 typedoc.json diff --git a/.gitignore b/.gitignore index 6704566..26550ae 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ dist # TernJS port file .tern-port + +# TypeDoc generated static site for documentations +docs/ diff --git a/package.json b/package.json index 6178741..2815fa8 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "format": "eslint --fix --ext .ts . && prettier --write .", "build": "concurrently \"pnpm:build:*\"", "build:types": "tsc -d --emitDeclarationOnly -p ./tsconfig.build.json", + "build:docs": "typedoc", "build:js": "rollup -c rollup.config.mjs && pnpm run minify", "minify": "concurrently \"pnpm:minify:*\"", "minify:umd": "terser dist/index.umd.cjs --compress --mangle > dist/index.umd.min.cjs", @@ -95,6 +96,7 @@ "terser": "5.19.0", "ts-jest": "29.1.1", "tslib": "2.6.0", + "typedoc": "0.24.8", "typescript": "5.1.6" }, "pnpm": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a978f91..4130acb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -100,6 +100,9 @@ devDependencies: tslib: specifier: 2.6.0 version: 2.6.0 + typedoc: + specifier: 0.24.8 + version: 0.24.8(typescript@5.1.6) typescript: specifier: 5.1.6 version: 5.1.6 @@ -3890,6 +3893,10 @@ packages: engines: {node: '>=12'} dev: true + /ansi-sequence-parser@1.1.0: + resolution: {integrity: sha512-lEm8mt52to2fT8GhciPCGeCXACSz2UwIN4X2e2LJSnZ5uAbn2/dsYdOmUXq0AtWS5cpAupysIneExOgH0Vd2TQ==} + dev: true + /ansi-styles@3.2.1: resolution: {integrity: sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==} engines: {node: '>=4'} @@ -6921,6 +6928,10 @@ packages: engines: {node: '>=12'} dev: true + /lunr@2.3.9: + resolution: {integrity: sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==} + dev: true + /luxon@3.3.0: resolution: {integrity: sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==} engines: {node: '>=12'} @@ -7020,6 +7031,12 @@ packages: supports-hyperlinks: 2.3.0 dev: true + /marked@4.3.0: + resolution: {integrity: sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==} + engines: {node: '>= 12'} + hasBin: true + dev: true + /marked@5.1.1: resolution: {integrity: sha512-bTmmGdEINWmOMDjnPWDxGPQ4qkDLeYorpYbEtFOXzOruTwUE671q4Guiuchn4N8h/v6NGd7916kXsm3Iz4iUSg==} engines: {node: '>= 18'} @@ -8642,6 +8659,15 @@ packages: resolution: {integrity: sha512-6j1W9l1iAs/4xYBI1SYOVZyFcCis9b4KCLQ8fgAGG07QvzaRLVVRQvAy85yNmmZSjYjg4MWh4gNvlPujU/5LpA==} dev: true + /shiki@0.14.3: + resolution: {integrity: sha512-U3S/a+b0KS+UkTyMjoNojvTgrBHjgp7L6ovhFVZsXmBGnVdQ4K4U9oK0z63w538S91ATngv1vXigHCSWOwnr+g==} + dependencies: + ansi-sequence-parser: 1.1.0 + jsonc-parser: 3.2.0 + vscode-oniguruma: 1.7.0 + vscode-textmate: 8.0.0 + dev: true + /shimmer@1.2.1: resolution: {integrity: sha512-sQTKC1Re/rM6XyFM6fIAGHRPVGvyXfgzIDvzoq608vM+jeyVD0Tu1E6Np0Kc2zAIFWIj963V2800iF/9LPieQw==} dev: true @@ -9276,6 +9302,20 @@ packages: is-typedarray: 1.0.0 dev: true + /typedoc@0.24.8(typescript@5.1.6): + resolution: {integrity: sha512-ahJ6Cpcvxwaxfu4KtjA8qZNqS43wYt6JL27wYiIgl1vd38WW/KWX11YuAeZhuz9v+ttrutSsgK+XO1CjL1kA3w==} + engines: {node: '>= 14.14'} + hasBin: true + peerDependencies: + typescript: 4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x + dependencies: + lunr: 2.3.9 + marked: 4.3.0 + minimatch: 9.0.3 + shiki: 0.14.3 + typescript: 5.1.6 + dev: true + /typescript@5.1.6: resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==} engines: {node: '>=14.17'} @@ -9458,6 +9498,14 @@ packages: vfile-message: 2.0.4 dev: true + /vscode-oniguruma@1.7.0: + resolution: {integrity: sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA==} + dev: true + + /vscode-textmate@8.0.0: + resolution: {integrity: sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg==} + dev: true + /vuln-vects@1.1.0: resolution: {integrity: sha512-LGDwn9nRz94YoeqOn2TZqQXzyonBc5FJppSgH34S/1U+3bgPONq/vvfiCbCQ4MeBll58xx+kDmhS73ac+EHBBw==} dev: true diff --git a/src/constants.ts b/src/constants.ts index 4e30cca..01bf7c3 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,3 +1,11 @@ +/** + * Array of identifiers that can be used as the initial part of a constructed identifier. + * + * @privateRemarks + * This is not intended to be used publicly. + * + * @internal + */ export const MINIFY_STARTING_CANDIDATES = [ 'a', 'b', @@ -27,6 +35,14 @@ export const MINIFY_STARTING_CANDIDATES = [ 'z', ] as const; +/** + * Array of identifiers that can be used as the remaining part of a constructed identifier. + * + * @privateRemarks + * This is not intended to be used publicly. + * + * @internal + */ export const MINIFY_REMAINING_CANDIDATES = [ '0', '1', diff --git a/src/index.ts b/src/index.ts index 80f2115..f1aedca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,12 @@ +/** + * A library to work with JSON and EJSON (Extended JSON). + * + * @packageDocumentation + */ + +/** + * Object containing all the methods and types for this library. + * + * @public + */ export * as JsonKit from './external.js'; diff --git a/src/lz4js.d.ts b/src/lz4js.d.ts index d665fc3..5469749 100644 --- a/src/lz4js.d.ts +++ b/src/lz4js.d.ts @@ -1,3 +1,14 @@ +/** + * Module declaration for the `lz4js` dependency. + * + * @remarks + * The type definitions for the APIs reference {@link https://github.com/Benzinga/lz4js#api | the package documentation}. + * + * @privateRemarks + * The type `number[]` here assumes all numbers are unsigned 8-bit integers. + * + * @internal + */ declare module 'lz4js' { export function compress( buffer: number[] | Uint8Array, diff --git a/src/parse.ts b/src/parse.ts index 988a4f5..1abea79 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -3,12 +3,79 @@ import { Base64 } from 'js-base64'; import { decompress } from 'lz4js'; import type { TypeGuardFunction } from './types.js'; +/** + * Type for the `reviver` parameter of {@link parse}. + * Refer to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter | the MDN documentation} for more details. + * + * @public + */ export type ParseReviverFunction = (this: any, key: string, value: any) => any; +/** + * Type for the `options` parameter of {@link parse}. + * + * @privateRemarks + * Refer to {@link defaultOptions} for the default values. + * + * @public + */ export type ParseOptions = { - extended?: boolean | { enable: boolean; relaxed?: boolean }; - unminify?: boolean | { enable: boolean; keyMap?: Record }; - decompress?: boolean | { enable: boolean }; + /** + * Specifies if the input string is in JSON or EJSON format, additional options to be passed into {@link https://github.com/mongodb/js-bson#ejsonparsetext-options | `bson.EJSON.parse()`} can be supplied with the long form (only effective if `enable` is `true`. + * @defaultValue `false` + */ + extended?: + | boolean + | { + /** + * `true` to specify the input is an EJSON string. + */ + enable: boolean; + /** + * Refer to {@link https://github.com/mongodb/js-bson#ejsonparsetext-options | `bson.EJSON.parse()`}. + * + * @defaultValue `true` + */ + relaxed?: boolean; + }; + /** + * Specifies if the input string is created with the `minify` option enabled in {@link Stringify.stringify}, a custom key map (shortened:original) can be supplied with the long form (only effective if `enable` is `true`. + * + * @remarks + * Will automatically disable if the unminifcation fails. + * + * @defaultValue `true` + */ + unminify?: + | boolean + | { + /** + * `true` to specify the input is created with the `minify` option enabled in {@link Stringify.stringify}, will automatically disable if the unminification fails. + */ + enable: boolean; + /** + * custom key map of form (shortened:original), if supplied then only the specified keys will be replaced. + * + * @defaultValue `undefined` + */ + keyMap?: Record; + }; + /** + * Specifies if the input string is compressed with {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}. + * + * @remarks + * Will automatically disable if the decompression fails. + * + * @defaultValue `true` + */ + decompress?: + | boolean + | { + /** + * `true` to specify the input is compressed with {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}, will automatically disable if the decompression fails. + */ + enable: boolean; + }; }; type _ParseOptions = { @@ -69,17 +136,110 @@ function mergeWithDefaultOptions(input?: ParseOptions | null): _ParseOptions { return input as _ParseOptions; } +/** + * Turns the input string into an object. + * + * @remarks + * With the custom options, the input string can be either + * - a JSON string (identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | `JSON.parse()`}) + * - an EJSON string (identical to {@link https://github.com/mongodb/js-bson#ejsonparsetext-options | `bson.EJSON.parse()`}) + * - a minified version of either of the above, where some or specified keys are replaced with a shorter identifier + * - a compressed version (by {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}) of either of the above + * + * @param text - The input string + * @param reviver - The `reviver` parameter of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter | `JSON.parse()`} + * @param options - The custom options, refer to {@link ParseOptions} for details + * @param typeGuard - The type guard function, ensures that the output object is of the expected type, refer to {@link Types.TypeGuardFunction} for an example. + * @typeParam T - Type of the output object + * @returns The output object + * + * @example + * ```ts + * type Foo { + * long_key: string + * } + * + * parse( + * "{\"lk\":\"A Large Object\"}", + * (key, val) => { + * if (key === "lk") { + * return "A Modified Large Object" + * } + * return val + * }, + * { + * extended: false, + * unminify: { enable: true, keyMap: { "lk": "long_key" } }, + * decompress: false + * }, + * (obj: any): obj is Foo => { + * const _obj: Partial | null | undefined = obj + * return typeof _obj?.long_key === "string" + * } + * ) + * ``` + * + * @public + * {@label FULL} + */ export function parse( text: string, reviver?: ParseReviverFunction | null, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; +/** + * Turns the input string into an object. + * + * @remarks + * With the custom options, the input string can be either + * - a JSON string (identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | `JSON.parse()`}) + * - an EJSON string (identical to {@link https://github.com/mongodb/js-bson#ejsonparsetext-options | `bson.EJSON.parse()`}) + * - a minified version of either of the above, where some or specified keys are replaced with a shorter identifier + * - a compressed version (by {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}) of either of the above + * + * @param text - The input string + * @param options - The custom options, refer to {@link ParseOptions} for details + * @param typeGuard - The type guard function, ensures that the output object is of the expected type, refer to {@link Types.TypeGuardFunction} for an example. + * @typeParam T - Type of the output object + * @returns The output object + * + * @example + * ```ts + * type Foo { + * long_key: string + * } + * + * parse( + * "{\"lk\":\"A Large Object\"}", + * { + * extended: false, + * unminify: { enable: true, keyMap: { "lk": "long_key" } }, + * decompress: false + * }, + * (obj: any): obj is Foo => { + * const _obj: Partial | null | undefined = obj + * return typeof _obj?.long_key === "string" + * } + * ) + * ``` + * + * @public + * {@label MINIMAL} + */ export function parse( text: string, options?: ParseOptions | null, typeGuard?: TypeGuardFunction, ): T; +/** + * Implementation details. + * + * @remarks + * If you see this, your are NOT calling this function with a valid signature. + * + * {@label IMPLEMENTATION} + */ export function parse( text: string, reviver?: ParseReviverFunction | ParseOptions | null, @@ -181,6 +341,14 @@ function unminifyKeys( return obj; } +/** + * Decompresses a string with {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L538 | `lz4js.decompress()`}. + * + * @param str - The input string + * @returns str - The decompressed string + * + * @public + */ export function decompressString(str: string): string { try { return new TextDecoder().decode( diff --git a/src/serialize.ts b/src/serialize.ts index 65be8c8..c0c8a57 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -2,6 +2,21 @@ import { parse } from './parse.js'; import { stringify } from './stringify.js'; import type { TypeGuardFunction } from './types.js'; +/** + * Turns the input object into an EJSON object. + * + * @remarks + * It is equivalent to (except a bit more performant due to custom options) + * ```ts + * let obj = { ... } + * parse(stringify(obj, { extended: true })) + * ``` + * + * @param obj - The input object + * @param typeGuard - The type guard function, ensures that the output object is of the expected type, refer to {@link TypeGuardFunction} for an example. + * @typeParam T - Type of the output object + * @returns The EJSON object + */ export function serialize( obj: any, typeGuard?: TypeGuardFunction, @@ -22,6 +37,21 @@ export function serialize( ); } +/** + * Turns the input object into an JSON object. + * + * @remarks + * It is equivalent to (except a bit more performant due to custom options) + * ```ts + * let obj = { ... } + * parse(stringify(obj), { extended: true }) + * ``` + * + * @param obj - The input object + * @param typeGuard - The type guard function, ensures that the output object is of the expected type, refer to {@link Types.TypeGuardFunction} for an example. + * @typeParam T - Type of the output object + * @returns The JSON object + */ export function deserialize( obj: any, typeGuard?: TypeGuardFunction, diff --git a/src/stringify.ts b/src/stringify.ts index 9ed1478..3e16030 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -7,17 +7,82 @@ import { MINIFY_STARTING_CANDIDATES, } from './constants.js'; +/** + * `Array` type for the `replacer` parameter of {@link stringify}. + * Refer to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter | the MDN documentation} for more details. + * + * @public + */ export type StringifyReplacerArray = (string | number)[]; +/** + * `Function` type for the `replacer` parameter of {@link stringify}. + * Refer to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter | the MDN documentation} for more details. + * + * @public + */ export type StringifyReplacerFunction = ( this: any, key: string, value: any, ) => any; +/** + * Type for the `options` parameter of {@link stringify}. + * + * @privateRemarks + * Refer to {@link defaultOptions} for the default values. + * + * @public + */ export type StringifyOptions = { - extended?: boolean | { enable: boolean; relaxed?: boolean }; - minify?: boolean | { enable: boolean; keyMap?: Record }; - compress?: boolean | { enable: boolean }; + /** + * Determines if the output string is in JSON or EJSON format, additional options to be passed into {@link https://github.com/mongodb/js-bson#ejsonstringifyvalue-replacer-space-options | `bson.EJSON.stringify()`} can be supplied with the long form (only effective if `enable` is `true`. + * @defaultValue `false` + */ + extended?: + | boolean + | { + /** + * `true` to produce an EJSON string, otherwise to produce a JSON string. + */ + enable: boolean; + /** + * Refer to {@link https://github.com/mongodb/js-bson#ejsonstringifyvalue-replacer-space-options | `bson.EJSON.stringify()`}. + * + * @defaultValue `true` + */ + relaxed?: boolean; + }; + /** + * Determines if the output string will have some of the keys replaced with a shorter identifier, a custom key map (original:shortened) can be supplied with the long form (only effective if `enable` is `true`. + * @defaultValue `false` + */ + minify?: + | boolean + | { + /** + * `true` to replace some keys with a shorter identifier, otherwise output it as is. + */ + enable: boolean; + /** + * custom key map of form (original:shortened), if supplied then only the specified keys will be replaced. + * + * @defaultValue `undefined` + */ + keyMap?: Record; + }; + /** + * Determines if the output string will be compressed with {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}. + * @defaultValue `false` + */ + compress?: + | boolean + | { + /** + * `true` to output a compressed string (by {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}), otherwise output it as is. + */ + enable: boolean; + }; }; type _StringifyOptions = { @@ -80,13 +145,88 @@ function mergeWithDefaultOptions( return input as _StringifyOptions; } +/** + * Turns the input object into a string. + * + * @remarks + * With the custom options, the output string can be either + * - a JSON string (identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | `JSON.stringify()`}) + * - an EJSON string (identical to {@link https://github.com/mongodb/js-bson#ejsonstringifyvalue-replacer-space-options | `bson.EJSON.stringify()`}) + * - a minified version of either of the above, where some or specified keys will be replaced with a shorter identifier + * - a compressed version (by {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}) of either of the above + * + * @param obj - The input object + * @param replacer - The `replacer` parameter of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter | `JSON.stringify()`} + * @param space - The `space` parameter of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_space_parameter | `JSON.stringify()`} + * @param options - The custom options, refer to {@link StringifyOptions} for details + * @returns The output string + * + * @example + * ```ts + * stringify( + * { long_key: "A Large Object" }, + * (key, val) => { + * if (key === "long_key") { + * return "A Modified Large Object" + * } + * return val + * }, + * 2, + * { + * extended: false, + * minify: { enable: true, keyMap: { long_key: "lk" } }, + * compress: false + * } + * ) + * ``` + * + * @public + * {@label FULL} + */ export function stringify( obj: any, replacer?: StringifyReplacerArray | StringifyReplacerFunction | null, space?: string | number | null, options?: StringifyOptions | null, ): string; +/** + * Turns the input object into a string. + * + * @remarks + * With the custom options, the output string can be either + * - a JSON string (identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | `JSON.stringify()`}) + * - an EJSON string (identical to {@link https://github.com/mongodb/js-bson#ejsonstringifyvalue-replacer-space-options | `bson.EJSON.stringify()`}) + * - a minified version of either of the above, where some or specified keys will be replaced with a shorter identifier + * - a compressed version (by {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}) of either of the above + * + * @param obj - The input object + * @param options - The custom options, refer to {@link StringifyOptions} for details + * @returns The output string + * + * @example + * ```ts + * stringify( + * { long_key: "A Large Object" }, + * { + * extended: false, + * minify: { enable: true, keyMap: { long_key: "lk" } }, + * compress: false + * } + * ) + * ``` + * + * @public + * {@label MINIMAL} + */ export function stringify(obj: any, options?: StringifyOptions | null): string; +/** + * Implementation details. + * + * @remarks + * If you see this, your are NOT calling this function with a valid signature. + * + * {@label IMPLEMENTATION} + */ export function stringify( obj: any, replacer?: @@ -256,6 +396,14 @@ function minifyKeys( return obj; } +/** + * Compresses a string with {@link https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557 | `lz4js.compress()`}. + * + * @param str - The input string + * @returns str - The compressed string + * + * @public + */ export function compressString(str: string): string { return Base64.fromUint8Array( compress(new TextEncoder().encode(str)) as Uint8Array, diff --git a/src/types.ts b/src/types.ts index b55c433..20dc338 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1 +1,18 @@ +/** + * A function that checks the input object against a specific type. + * + * @typeParam T - The type to check against + * + * @example + * ```ts + * type Foo { + * bar: number + * } + * + * function isFoo(obj: any): obj is Foo { + * const _obj: Partial | null | undefined = obj + * return typeof _obj?.bar === "number" + * } + * ``` + */ export type TypeGuardFunction = (obj: any) => obj is T; diff --git a/typedoc.json b/typedoc.json new file mode 100644 index 0000000..d061c64 --- /dev/null +++ b/typedoc.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://typedoc.org/schema.json", + "entryPoints": ["src/index.ts"], + "out": "docs", + "tsconfig": "./tsconfig.build.json", + "excludeInternal": false, + "excludePrivate": false, + "useTsLinkResolution": false, + "validation": { + "notExported": true, + "invalidLink": true, + "notDocumented": true + } +} From 1700d53609489495ede3724b060c782482121acd Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 09:42:22 +0000 Subject: [PATCH 17/23] chore(release): fix outdated path for CHANGELOG.md --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 50b7baf..6c7c0ee 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -52,4 +52,4 @@ jobs: branch: release delete-branch: true title: "chore(release): bump version and update CHANGELOG.md [skip ci]" - body: "This PR bumps the version in `package.json` and updates `docs/CHANGELOG.md`." + body: "This PR bumps the version in `package.json` and updates `CHANGELOG.md`." From c705c975e984dad01e8379cb4e7bb5d63f09acb7 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Mon, 17 Jul 2023 16:05:36 +0000 Subject: [PATCH 18/23] chore: add steps to publish documentations in the CI/CD pipeline --- .github/workflows/publish.yml | 74 +++++++++++++++++++++++++++++++++++ .github/workflows/release.yml | 12 ------ .gitignore | 5 ++- .releaserc.json | 16 ++++++-- docs/.gitkeep | 0 package.json | 11 +++++- pnpm-lock.yaml | 72 ++++++++++++++++++++++++++++++++++ typedoc.json | 8 ++-- 8 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 docs/.gitkeep diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..829a29f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,74 @@ +name: Publish + +on: + release: + types: ["released"] + + workflow_dispatch: + inputs: + version: + description: "Version" + type: string + required: true + +jobs: + build_and_publish_wiki: + name: Build and Publish GitHub Wiki + runs-on: ubuntu-latest + environment: Publish + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "lts/*" + - uses: pnpm/action-setup@v2 + name: Install pnpm + id: pnpm-install + with: + run_install: false + - name: Get pnpm store directory + id: pnpm-cache + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT + - uses: actions/cache@v3 + name: Setup pnpm cache + with: + path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + - name: Install dependencies + run: pnpm install + - name: Build documentations + run: pnpm exec concurrently "pnpm:build:docs:site" "pnpm:build:docs:wiki" + - name: Publish GitHub Wiki + uses: Andrew-Chen-Wang/github-wiki-action@v4 + with: + commit-message: "chore(publish): GitHub Wiki for ${{ inputs.version || github.ref_name || github.sha }} [skip ci]" + - name: Upload GitHub Pages Artifacts + uses: actions/upload-pages-artifact@v2 + with: + path: "site/" + publish_pages: + name: Publish GitHub Pages + needs: build_and_publish_wiki + runs-on: ubuntu-latest + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + permissions: + pages: write + id-token: write + steps: + - name: Setup GitHub Pages + uses: actions/configure-pages@v3 + - name: Publish GitHub Pages + id: deployment + uses: actions/deploy-pages@v2 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6c7c0ee..60d8bae 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,15 +41,3 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: rm -rf dist && pnpm run pre-release && pnpm run semantic-release - - name: Bump package version - uses: peter-evans/create-pull-request@v5 - with: - add-paths: | - CHANGELOG.md - package.json - commit-message: "chore(release): bump version and update CHANGELOG.md [skip ci]" - committer: "release-bot " - branch: release - delete-branch: true - title: "chore(release): bump version and update CHANGELOG.md [skip ci]" - body: "This PR bumps the version in `package.json` and updates `CHANGELOG.md`." diff --git a/.gitignore b/.gitignore index 26550ae..3f6e76d 100644 --- a/.gitignore +++ b/.gitignore @@ -103,5 +103,6 @@ dist # TernJS port file .tern-port -# TypeDoc generated static site for documentations -docs/ +# TypeDoc generated documentations +site/ +wiki/ diff --git a/.releaserc.json b/.releaserc.json index 56c718f..c0d5db7 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -20,17 +20,25 @@ "noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"] }, "writerOpts": { - "commitsSort": ["subject", "scope"] + "commitsSort": ["scope", "type", "subject"] } } ], + "@semantic-release/changelog", + "@semantic-release/npm", [ - "@semantic-release/changelog", + "@semantic-release/exec", { - "changelogFile": "CHANGELOG.md" + "prepareCmd": "pnpm run build:docs:md" + } + ], + [ + "@semantic-release/git", + { + "assets": ["package.json", "CHANGELOG.md", "docs/"], + "message": "chore(release): release v${nextRelease.version} [skip ci]" } ], - "@semantic-release/npm", [ "@semantic-release/github", { diff --git a/docs/.gitkeep b/docs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 2815fa8..b05344c 100644 --- a/package.json +++ b/package.json @@ -32,9 +32,12 @@ "lint:style": "prettier --check .", "lint:types": "tsc --noEmit", "format": "eslint --fix --ext .ts . && prettier --write .", - "build": "concurrently \"pnpm:build:*\"", + "build": "concurrently \"pnpm:build:*(!docs)\"", "build:types": "tsc -d --emitDeclarationOnly -p ./tsconfig.build.json", - "build:docs": "typedoc", + "build:docs": "concurrently \"pnpm:build:docs:*\"", + "build:docs:site": "typedoc", + "build:docs:md": "typedoc --out docs --plugin typedoc-plugin-markdown", + "build:docs:wiki": "typedoc --out wiki --plugin typedoc-plugin-markdown --plugin typedoc-github-wiki-theme", "build:js": "rollup -c rollup.config.mjs && pnpm run minify", "minify": "concurrently \"pnpm:minify:*\"", "minify:umd": "terser dist/index.umd.cjs --compress --mangle > dist/index.umd.min.cjs", @@ -76,6 +79,8 @@ "@rollup/plugin-node-resolve": "15.1.0", "@rollup/plugin-typescript": "11.1.2", "@semantic-release/changelog": "6.0.3", + "@semantic-release/exec": "6.0.3", + "@semantic-release/git": "10.0.1", "@semantic-release/npm": "10.0.4", "@swc/core": "1.3.69", "@swc/jest": "0.2.26", @@ -97,6 +102,8 @@ "ts-jest": "29.1.1", "tslib": "2.6.0", "typedoc": "0.24.8", + "typedoc-github-wiki-theme": "1.1.0", + "typedoc-plugin-markdown": "3.15.3", "typescript": "5.1.6" }, "pnpm": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4130acb..2ced7b1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -40,6 +40,12 @@ devDependencies: '@semantic-release/changelog': specifier: 6.0.3 version: 6.0.3(semantic-release@21.0.7) + '@semantic-release/exec': + specifier: 6.0.3 + version: 6.0.3(semantic-release@21.0.7) + '@semantic-release/git': + specifier: 10.0.1 + version: 10.0.1(semantic-release@21.0.7) '@semantic-release/npm': specifier: 10.0.4 version: 10.0.4(semantic-release@21.0.7) @@ -103,6 +109,12 @@ devDependencies: typedoc: specifier: 0.24.8 version: 0.24.8(typescript@5.1.6) + typedoc-github-wiki-theme: + specifier: 1.1.0 + version: 1.1.0(typedoc-plugin-markdown@3.15.3)(typedoc@0.24.8) + typedoc-plugin-markdown: + specifier: 3.15.3 + version: 3.15.3(typedoc@0.24.8) typescript: specifier: 5.1.6 version: 5.1.6 @@ -2649,6 +2661,42 @@ packages: engines: {node: '>=18'} dev: true + /@semantic-release/exec@6.0.3(semantic-release@21.0.7): + resolution: {integrity: sha512-bxAq8vLOw76aV89vxxICecEa8jfaWwYITw6X74zzlO0mc/Bgieqx9kBRz9z96pHectiTAtsCwsQcUyLYWnp3VQ==} + engines: {node: '>=14.17'} + peerDependencies: + semantic-release: '>=18.0.0' + dependencies: + '@semantic-release/error': 3.0.0 + aggregate-error: 3.1.0 + debug: 4.3.4 + execa: 5.1.1 + lodash: 4.17.21 + parse-json: 5.2.0 + semantic-release: 21.0.7 + transitivePeerDependencies: + - supports-color + dev: true + + /@semantic-release/git@10.0.1(semantic-release@21.0.7): + resolution: {integrity: sha512-eWrx5KguUcU2wUPaO6sfvZI0wPafUKAMNC18aXY4EnNcrZL86dEmpNVnC9uMpGZkmZJ9EfCVJBQx4pV4EMGT1w==} + engines: {node: '>=14.17'} + peerDependencies: + semantic-release: '>=18.0.0' + dependencies: + '@semantic-release/error': 3.0.0 + aggregate-error: 3.1.0 + debug: 4.3.4 + dir-glob: 3.0.1 + execa: 5.1.1 + lodash: 4.17.21 + micromatch: 4.0.5 + p-reduce: 2.1.0 + semantic-release: 21.0.7 + transitivePeerDependencies: + - supports-color + dev: true + /@semantic-release/github@9.0.3(semantic-release@21.0.7): resolution: {integrity: sha512-X6gq4USKVlCxPwIIyXb99jU7gwVWlnsKOevs+OyABRdoqc+OIRITbFmrrYU3eE1vGMGk+Qu/GAoLUQQQwC3YOA==} engines: {node: '>=18'} @@ -7791,6 +7839,11 @@ packages: p-timeout: 3.2.0 dev: true + /p-reduce@2.1.0: + resolution: {integrity: sha512-2USApvnsutq8uoxZBGbbWM0JIYLiEMJ9RlaN7fAzVNb9OZN0SHjjTTfIcb667XynS5Y1VhwDJVDa72TnPzAYWw==} + engines: {node: '>=8'} + dev: true + /p-reduce@3.0.0: resolution: {integrity: sha512-xsrIUgI0Kn6iyDYm9StOpOeK29XM1aboGji26+QEortiFST1hGZaUQOLhtEbqHErPpGW/aSz6allwK2qcptp0Q==} engines: {node: '>=12'} @@ -9302,6 +9355,25 @@ packages: is-typedarray: 1.0.0 dev: true + /typedoc-github-wiki-theme@1.1.0(typedoc-plugin-markdown@3.15.3)(typedoc@0.24.8): + resolution: {integrity: sha512-VyFmz8ZV2j/qEsCjD5EtR6FgZsCoy64Zr6SS9kCTcq7zx69Cx4UJBx8Ga/naxqs08TDggE6myIfODY6awwAGcA==} + peerDependencies: + typedoc: '>=0.24.0' + typedoc-plugin-markdown: '>=3.15.0' + dependencies: + typedoc: 0.24.8(typescript@5.1.6) + typedoc-plugin-markdown: 3.15.3(typedoc@0.24.8) + dev: true + + /typedoc-plugin-markdown@3.15.3(typedoc@0.24.8): + resolution: {integrity: sha512-idntFYu3vfaY3eaD+w9DeRd0PmNGqGuNLKihPU9poxFGnATJYGn9dPtEhn2QrTdishFMg7jPXAhos+2T6YCWRQ==} + peerDependencies: + typedoc: '>=0.24.0' + dependencies: + handlebars: 4.7.7 + typedoc: 0.24.8(typescript@5.1.6) + dev: true + /typedoc@0.24.8(typescript@5.1.6): resolution: {integrity: sha512-ahJ6Cpcvxwaxfu4KtjA8qZNqS43wYt6JL27wYiIgl1vd38WW/KWX11YuAeZhuz9v+ttrutSsgK+XO1CjL1kA3w==} engines: {node: '>= 14.14'} diff --git a/typedoc.json b/typedoc.json index d061c64..9beaa54 100644 --- a/typedoc.json +++ b/typedoc.json @@ -1,11 +1,11 @@ { "$schema": "https://typedoc.org/schema.json", "entryPoints": ["src/index.ts"], - "out": "docs", + "out": "site", "tsconfig": "./tsconfig.build.json", - "excludeInternal": false, - "excludePrivate": false, - "useTsLinkResolution": false, + "includeVersion": true, + "excludeInternal": true, + "excludePrivate": true, "validation": { "notExported": true, "invalidLink": true, From 87ec043cbb36fd6c529f02890867597513e4fd19 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Sat, 22 Jul 2023 08:36:27 +0000 Subject: [PATCH 19/23] chore(style): turn off eslint rules conflicting with prettier with eslint-config-prettier --- .eslintrc.json | 6 +++++- package.json | 1 + pnpm-lock.yaml | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index bfbd71f..92c57ec 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,7 +2,11 @@ "root": true, "parser": "@typescript-eslint/parser", "plugins": ["@typescript-eslint"], - "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "prettier" + ], "rules": { "@typescript-eslint/consistent-type-imports": "error", "@typescript-eslint/consistent-type-definitions": ["error", "type"], diff --git a/package.json b/package.json index b05344c..9faf109 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "@typescript-eslint/parser": "6.0.0", "concurrently": "8.2.0", "eslint": "8.45.0", + "eslint-config-prettier": "8.8.0", "husky": "8.0.3", "jest": "29.6.1", "prettier": "3.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2ced7b1..8056652 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -79,6 +79,9 @@ devDependencies: eslint: specifier: 8.45.0 version: 8.45.0 + eslint-config-prettier: + specifier: 8.8.0 + version: 8.8.0(eslint@8.45.0) husky: specifier: 8.0.3 version: 8.0.3 @@ -5037,6 +5040,15 @@ packages: engines: {node: '>=12'} dev: true + /eslint-config-prettier@8.8.0(eslint@8.45.0): + resolution: {integrity: sha512-wLbQiFre3tdGgpDv67NQKnJuTlcUVYHas3k+DZCc2U2BadthoEY4B7hLPvAxaqdyOGCzuLfii2fqGph10va7oA==} + hasBin: true + peerDependencies: + eslint: '>=7.0.0' + dependencies: + eslint: 8.45.0 + dev: true + /eslint-scope@5.1.1: resolution: {integrity: sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==} engines: {node: '>=8.0.0'} From 40e998dd053ae72e142de56f3287d1d4578b3159 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Sun, 30 Jul 2023 09:30:40 +0000 Subject: [PATCH 20/23] chore(format): ignore site and wiki directories --- .eslintrc.json | 2 ++ .prettierignore | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index 92c57ec..30a356d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,6 +23,8 @@ "node_modules", "dist", "docs", + "site", + "wiki", "coverage", "test/dataset", "*.cjs" diff --git a/.prettierignore b/.prettierignore index d81c543..40c2f1f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,6 +2,8 @@ node_modules/ dist/ docs/ +site/ +wiki/ coverage/ test/dataset/ .editorconfig From ac02a54324d54ab3bf0dcf9062944e3bc752a5fe Mon Sep 17 00:00:00 2001 From: Kit-p Date: Sun, 30 Jul 2023 09:30:52 +0000 Subject: [PATCH 21/23] chore(renovate): use default semantic commit types --- renovate.json | 1 - 1 file changed, 1 deletion(-) diff --git a/renovate.json b/renovate.json index 75577e0..cf24d6c 100644 --- a/renovate.json +++ b/renovate.json @@ -3,7 +3,6 @@ "extends": [ "config:js-lib", ":semanticCommits", - ":semanticCommitTypeAll(chore)", ":semanticCommitScope(deps)", "npm:unpublishSafe", ":assignAndReview(Kit-p)", From 109caecd12209ef114b216fc96338bf63ec955f2 Mon Sep 17 00:00:00 2001 From: Kit-p Date: Sun, 30 Jul 2023 09:31:18 +0000 Subject: [PATCH 22/23] chore(test): configure jest threshold --- jest.config.mjs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/jest.config.mjs b/jest.config.mjs index 96b2737..63c84f3 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -8,6 +8,14 @@ const config = { transform: { "^.+\\.(c|m)?(t|j)sx?$": ["@swc/jest"], }, + coverageThreshold: { + global: { + branches: 100, + functions: 100, + lines: 100, + statements: 100, + }, + }, }; export default config; From 3b398cc535e4626a2a5ef4305b16a49ac7d5cfff Mon Sep 17 00:00:00 2001 From: Kit-p Date: Sun, 30 Jul 2023 09:31:52 +0000 Subject: [PATCH 23/23] chore(docs): update README.md --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e2aff2c..e58de8f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,18 @@

json-kit