Skip to content

Commit

Permalink
Improved TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Feb 20, 2024
1 parent 523a1e5 commit acbf0e6
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 82 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ php/test.php
flatted.jpg
package-lock.json
SPECS.md
tsconfig.json
30 changes: 26 additions & 4 deletions cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';
/// <reference types="../types.d.ts" />

// (c) 2020-present Andrea Giammarchi

const {parse: $parse, stringify: $stringify} = JSON;
Expand Down Expand Up @@ -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];
Expand All @@ -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) :
Expand Down Expand Up @@ -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;
30 changes: 26 additions & 4 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="../types.d.ts" />

// (c) 2020-present Andrea Giammarchi

const {parse: $parse, stringify: $stringify} = JSON;
Expand Down Expand Up @@ -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];
Expand All @@ -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) :
Expand Down Expand Up @@ -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));
37 changes: 31 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ self.Flatted = (function (exports) {
}, _typeof(o);
}

/// <reference types="../types.d.ts" />

// (c) 2020-present Andrea Giammarchi

var $parse = JSON.parse,
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"es.js",
"index.js",
"min.js",
"package-lock.json"
"package-lock.json",
"tsconfig.json"
]
}
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down Expand Up @@ -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"
},
Expand Down
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "esnext",
"moduleResolution": "nodenext",
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"declarationDir": "types"
},
"include": [
"esm/index.js",
]
}
62 changes: 0 additions & 62 deletions types.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;

4 comments on commit acbf0e6

@blakeIMG
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By no means an expert in TypeScript or anything, but noticed some issues related to this package in our build pipelines today. In the previous iteration types.d.ts, the reviver argument for parse was optional. IN the new types/index.d.ts that no longer appears to be the case. If this was by design, then my bad, we're just going to update some package references to use an older version.

@WebReflection
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blakeIMG nope, the reviver is flagged as optional so I will fix this, apologies for any inconvenience.

@WebReflection
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blakeIMG fixed in 3.3.1

@blakeIMG
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick response, and fix. Pipelines are no longer having an issue with 3.3.1

Please sign in to comment.