diff --git a/.npmignore b/.npmignore index 760a016..7da2203 100644 --- a/.npmignore +++ b/.npmignore @@ -10,3 +10,4 @@ php/test.php flatted.jpg package-lock.json SPECS.md +tsconfig.json diff --git a/cjs/index.js b/cjs/index.js index c5d0061..2e6ce02 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -1,6 +1,4 @@ 'use strict'; -/// - // (c) 2020-present Andrea Giammarchi const {parse: $parse, stringify: $stringify} = JSON; @@ -53,6 +51,12 @@ const set = (known, input, value) => { return index; }; +/** + * Converts a specialized flatted string into a JS value. + * @param {string} text + * @param {((this: any, key: string, value: any) => any) | undefined): any} [reviver] + * @returns {any} + */ const parse = (text, reviver) => { const input = $parse(text, Primitives).map(primitives); const value = input[0]; @@ -64,6 +68,13 @@ const parse = (text, reviver) => { }; exports.parse = parse; +/** + * Converts a JS value into a specialized flatted string. + * @param {any} value + * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer] + * @param {string | number | undefined} [string] + * @returns {string} + */ const stringify = (value, replacer, space) => { const $ = replacer && typeof replacer === object ? (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) : @@ -95,7 +106,18 @@ const stringify = (value, replacer, space) => { }; exports.stringify = stringify; -const toJSON = any => $parse(stringify(any)); +/** + * Converts a generic value into a JSON serializable object without losing recursion. + * @param {any} value + * @returns {any} + */ +const toJSON = value => $parse(stringify(value)); exports.toJSON = toJSON; -const fromJSON = any => parse($stringify(any)); + +/** + * Converts a previously serialized object with recursion into a recursive one. + * @param {any} value + * @returns {any} + */ +const fromJSON = value => parse($stringify(value)); exports.fromJSON = fromJSON; diff --git a/esm/index.js b/esm/index.js index d891776..2895500 100644 --- a/esm/index.js +++ b/esm/index.js @@ -1,5 +1,3 @@ -/// - // (c) 2020-present Andrea Giammarchi const {parse: $parse, stringify: $stringify} = JSON; @@ -52,6 +50,12 @@ const set = (known, input, value) => { return index; }; +/** + * Converts a specialized flatted string into a JS value. + * @param {string} text + * @param {((this: any, key: string, value: any) => any) | undefined): any} [reviver] + * @returns {any} + */ export const parse = (text, reviver) => { const input = $parse(text, Primitives).map(primitives); const value = input[0]; @@ -62,6 +66,13 @@ export const parse = (text, reviver) => { return $.call({'': tmp}, '', tmp); }; +/** + * Converts a JS value into a specialized flatted string. + * @param {any} value + * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer] + * @param {string | number | undefined} [string] + * @returns {string} + */ export const stringify = (value, replacer, space) => { const $ = replacer && typeof replacer === object ? (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) : @@ -92,5 +103,16 @@ export const stringify = (value, replacer, space) => { } }; -export const toJSON = any => $parse(stringify(any)); -export const fromJSON = any => parse($stringify(any)); +/** + * Converts a generic value into a JSON serializable object without losing recursion. + * @param {any} value + * @returns {any} + */ +export const toJSON = value => $parse(stringify(value)); + +/** + * Converts a previously serialized object with recursion into a recursive one. + * @param {any} value + * @returns {any} + */ +export const fromJSON = value => parse($stringify(value)); diff --git a/index.js b/index.js index 87a744f..46a89c1 100644 --- a/index.js +++ b/index.js @@ -11,8 +11,6 @@ self.Flatted = (function (exports) { }, _typeof(o); } - /// - // (c) 2020-present Andrea Giammarchi var $parse = JSON.parse, @@ -62,6 +60,13 @@ self.Flatted = (function (exports) { known.set(value, index); return index; }; + + /** + * Converts a specialized flatted string into a JS value. + * @param {string} text + * @param {((this: any, key: string, value: any) => any) | undefined): any} [reviver] + * @returns {any} + */ var parse = function parse(text, reviver) { var input = $parse(text, Primitives).map(primitives); var value = input[0]; @@ -71,6 +76,14 @@ self.Flatted = (function (exports) { '': tmp }, '', tmp); }; + + /** + * Converts a JS value into a specialized flatted string. + * @param {any} value + * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer] + * @param {string | number | undefined} [string] + * @returns {string} + */ var stringify = function stringify(value, replacer, space) { var $ = replacer && _typeof(replacer) === object ? function (k, v) { return k === '' || -1 < replacer.indexOf(k) ? v : void 0; @@ -102,11 +115,23 @@ self.Flatted = (function (exports) { return after; } }; - var toJSON = function toJSON(any) { - return $parse(stringify(any)); + + /** + * Converts a generic value into a JSON serializable object without losing recursion. + * @param {any} value + * @returns {any} + */ + var toJSON = function toJSON(value) { + return $parse(stringify(value)); }; - var fromJSON = function fromJSON(any) { - return parse($stringify(any)); + + /** + * Converts a previously serialized object with recursion into a recursive one. + * @param {any} value + * @returns {any} + */ + var fromJSON = function fromJSON(value) { + return parse($stringify(value)); }; exports.fromJSON = fromJSON; diff --git a/jsr.json b/jsr.json index b7d44ce..5181dd2 100644 --- a/jsr.json +++ b/jsr.json @@ -15,6 +15,7 @@ "es.js", "index.js", "min.js", - "package-lock.json" + "package-lock.json", + "tsconfig.json" ] } diff --git a/package-lock.json b/package-lock.json index 8c58a22..10f598c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,8 @@ "circular-json-es6": "^2.0.2", "jsan": "^3.1.14", "rollup": "^4.11.0", - "terser": "^5.27.0" + "terser": "^5.27.0", + "typescript": "^5.3.3" } }, "node_modules/@ampproject/remapping": { @@ -3154,6 +3155,19 @@ "node": ">=4" } }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/unicode-canonical-property-names-ecmascript": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz", diff --git a/package.json b/package.json index 965e960..8864345 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,9 @@ "version": "3.2.10", "description": "A super light and fast circular JSON parser.", "unpkg": "min.js", - "types": "types.d.ts", "main": "./cjs/index.js", "scripts": { - "build": "npm run cjs && npm run rollup:esm && npm run rollup:es && npm run rollup:babel && npm run min && npm run test && npm run size", + "build": "npm run cjs && npm run rollup:esm && npm run rollup:es && npm run rollup:babel && npm run min && npm run test && npm run ts && npm run size", "cjs": "ascjs esm cjs", "rollup:es": "rollup --config rollup/es.config.js && sed -i.bck 's/^var /self./' es.js && rm -rf es.js.bck", "rollup:esm": "rollup --config rollup/esm.config.js", @@ -16,6 +15,7 @@ "test": "c8 node test/index.js", "test:php": "php php/test.php", "test:py": "python python/test.py", + "ts": "tsc -p .", "coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info" }, "repository": { @@ -47,13 +47,14 @@ "circular-json-es6": "^2.0.2", "jsan": "^3.1.14", "rollup": "^4.11.0", - "terser": "^5.27.0" + "terser": "^5.27.0", + "typescript": "^5.3.3" }, "module": "./esm/index.js", "type": "module", "exports": { ".": { - "types": "./types.d.ts", + "types": "./types/index.d.ts", "import": "./esm/index.js", "default": "./cjs/index.js" }, diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..37a6a94 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "target": "esnext", + "moduleResolution": "nodenext", + "allowJs": true, + "declaration": true, + "emitDeclarationOnly": true, + "declarationDir": "types" + }, + "include": [ + "esm/index.js", + ] +} diff --git a/types.d.ts b/types.d.ts deleted file mode 100644 index d38b4a1..0000000 --- a/types.d.ts +++ /dev/null @@ -1,62 +0,0 @@ -interface Flatted { - /** - * Converts a JavaScript Object Notation (using Flatted encoding) string into an object. - * @param text A valid Flatted string. - * @param reviver A function that transforms the results. This function is called for each member of the object. - * If a member contains nested objects, the nested objects are transformed before the parent object is. - */ - parse( - text: string, - reviver?: (this: any, key: string, value: any) => any - ): any; - /** - * Converts a JavaScript value to a JavaScript Object Notation (using Flatted encoding) string. - * @param value A JavaScript value, usually an object or array, to be converted. - * @param replacer A function that transforms the results. - * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. - */ - stringify( - value: any, - replacer?: (this: any, key: string, value: any) => any, - space?: string | number - ): string; - /** - * Converts a JavaScript value to a JavaScript Object Notation (using Flatted encoding) string. - * @param value A JavaScript value, usually an object or array, to be converted. - * @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified. - * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. - */ - stringify( - value: any, - replacer?: (number | string)[] | null, - space?: string | number - ): string; - /** - * Helper to allow explicit conversions with classes. - * @param value The JSON to convert to JavaScript value - */ - fromJSON(value: any): any; - /** - * Helper to allow explicit conversions with classes. - * @param value A JavaScript value, usually an object or array, to be converted. - */ - toJSON(value: any): any; -} - -/** - * Fast and minimal circular JSON parser. - * logic example -```js -var a = [{one: 1}, {two: '2'}]; -a[0].a = a; -// a is the main object, will be at index '0' -// {one: 1} is the second object, index '1' -// {two: '2'} the third, in '2', and it has a string -// which will be found at index '3' -Flatted.stringify(a); -// [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"] -// a[one,two] {one: 1, a} {two: '2'} '2' -``` - */ -declare const Flatted: Flatted; -export = Flatted; diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..cb9e1da --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,4 @@ +export function parse(text: string, reviver: any): any; +export function stringify(value: any, replacer?: (string | number)[] | ((this: any, key: string, value: any) => any), space: any): string; +export function toJSON(value: any): any; +export function fromJSON(value: any): any;