@brillout/json-serializer
Advanced tools
Comparing version 0.5.8 to 0.5.9
@@ -15,3 +15,6 @@ "use strict"; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if ( | ||
// Also matches arrays | ||
typeof value === 'object' && | ||
value !== null) { | ||
Object.entries(value).forEach(([key, val]) => { | ||
@@ -18,0 +21,0 @@ ; |
export { stringify }; | ||
export { isJsonSerializerError }; | ||
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys }?: { | ||
import { type Iterable } from './utils/replacerWithPath'; | ||
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: { | ||
forbidReactElements?: boolean; | ||
@@ -8,2 +9,5 @@ space?: number; | ||
sortObjectKeys?: boolean; | ||
replacer?: (this: Iterable, key: string, value: unknown, path: string) => void | { | ||
replacement: unknown; | ||
}; | ||
}): string; | ||
@@ -10,0 +14,0 @@ type JsonSerializerError = Error & { |
@@ -9,3 +9,3 @@ "use strict"; | ||
const replacerWithPath_1 = require("./utils/replacerWithPath"); | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) { | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, } = {}) { | ||
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`. | ||
@@ -20,2 +20,7 @@ // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions | ||
function replacer(key, value, path) { | ||
{ | ||
const ret = replacerUserProvided === null || replacerUserProvided === void 0 ? void 0 : replacerUserProvided.call(this, key, value, path); | ||
if (ret) | ||
return ret.replacement; | ||
} | ||
if (forbidReactElements && (0, isReactElement_1.isReactElement)(value)) { | ||
@@ -53,3 +58,3 @@ throw genErr(genErrMsg('React element', path, valueName)); | ||
messageCore: errMsg, | ||
[stamp]: true | ||
[stamp]: true, | ||
}); | ||
@@ -56,0 +61,0 @@ return err; |
export { types }; | ||
declare const types: readonly [Type<undefined, unknown>, Type<number, unknown>, Type<number, unknown>, Type<number, unknown>, Type<Date, any>, Type<BigInt, any>, Type<RegExp, any>, Type<Map<any, any>, any[]>, Type<Set<unknown>, unknown[]>, Type<string, any>]; | ||
type Type<T, IntermediateType> = { | ||
is: (val: unknown) => asserts val is T; | ||
declare const types: readonly Type<any, any>[]; | ||
type Type<ValueType, IntermediateType> = { | ||
is: (val: unknown) => asserts val is ValueType; | ||
match: (str: string) => boolean; | ||
serialize: (val: T, serializer: (val: IntermediateType) => string) => string; | ||
deserialize: (str: string, deserializer: (str: string) => IntermediateType) => T; | ||
serialize: (val: ValueType, serializer: (val: IntermediateType) => string) => string; | ||
deserialize: (str: string, deserializer: (str: string) => IntermediateType) => ValueType; | ||
}; |
@@ -9,3 +9,3 @@ "use strict"; | ||
serialize: () => '!undefined', | ||
deserialize: () => undefined | ||
deserialize: () => undefined, | ||
}), | ||
@@ -16,3 +16,3 @@ ts({ | ||
serialize: () => '!Infinity', | ||
deserialize: () => Infinity | ||
deserialize: () => Infinity, | ||
}), | ||
@@ -23,3 +23,3 @@ ts({ | ||
serialize: () => '!-Infinity', | ||
deserialize: () => -Infinity | ||
deserialize: () => -Infinity, | ||
}), | ||
@@ -30,3 +30,3 @@ ts({ | ||
serialize: () => '!NaN', | ||
deserialize: () => NaN | ||
deserialize: () => NaN, | ||
}), | ||
@@ -37,3 +37,3 @@ ts({ | ||
serialize: (val) => '!Date:' + val.toISOString(), | ||
deserialize: (str) => new Date(str.slice('!Date:'.length)) | ||
deserialize: (str) => new Date(str.slice('!Date:'.length)), | ||
}), | ||
@@ -49,3 +49,3 @@ ts({ | ||
return BigInt(str.slice('!BigInt:'.length)); | ||
} | ||
}, | ||
}), | ||
@@ -63,3 +63,3 @@ ts({ | ||
return new RegExp(pattern, flags); | ||
} | ||
}, | ||
}), | ||
@@ -70,3 +70,3 @@ ts({ | ||
serialize: (val, serializer) => '!Map:' + serializer(Array.from(val.entries())), | ||
deserialize: (str, deserializer) => new Map(deserializer(str.slice('!Map:'.length))) | ||
deserialize: (str, deserializer) => new Map(deserializer(str.slice('!Map:'.length))), | ||
}), | ||
@@ -77,3 +77,3 @@ ts({ | ||
serialize: (val, serializer) => '!Set:' + serializer(Array.from(val.values())), | ||
deserialize: (str, deserializer) => new Set(deserializer(str.slice('!Set:'.length))) | ||
deserialize: (str, deserializer) => new Set(deserializer(str.slice('!Set:'.length))), | ||
}), | ||
@@ -85,4 +85,4 @@ // Avoid collisions with the special strings defined above | ||
serialize: (val) => '!' + val, | ||
deserialize: (str) => str.slice(1) | ||
}) | ||
deserialize: (str) => str.slice(1), | ||
}), | ||
]; | ||
@@ -89,0 +89,0 @@ exports.types = types; |
export { replacerWithPath }; | ||
export type { Iterable }; | ||
type Iterable = Record<string, unknown>; | ||
declare function replacerWithPath(replacer: (this: Iterable, key: string, value: unknown, path: string) => unknown, canBeFirstKey: boolean): (this: Iterable, key: string, value: unknown) => unknown; | ||
type Replacer = (this: Iterable, key: string, value: unknown, path: string) => unknown; | ||
declare function replacerWithPath(replacer: Replacer, canBeFirstKey: boolean): (this: Iterable, key: string, value: unknown) => unknown; |
@@ -12,3 +12,6 @@ export { parse }; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if ( | ||
// Also matches arrays | ||
typeof value === 'object' && | ||
value !== null) { | ||
Object.entries(value).forEach(([key, val]) => { | ||
@@ -15,0 +18,0 @@ ; |
export { stringify }; | ||
export { isJsonSerializerError }; | ||
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys }?: { | ||
import { type Iterable } from './utils/replacerWithPath'; | ||
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: { | ||
forbidReactElements?: boolean; | ||
@@ -8,2 +9,5 @@ space?: number; | ||
sortObjectKeys?: boolean; | ||
replacer?: (this: Iterable, key: string, value: unknown, path: string) => void | { | ||
replacement: unknown; | ||
}; | ||
}): string; | ||
@@ -10,0 +14,0 @@ type JsonSerializerError = Error & { |
@@ -8,3 +8,3 @@ export { stringify }; | ||
import { replacerWithPath } from './utils/replacerWithPath'; | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) { | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, } = {}) { | ||
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`. | ||
@@ -19,2 +19,7 @@ // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions | ||
function replacer(key, value, path) { | ||
{ | ||
const ret = replacerUserProvided?.call(this, key, value, path); | ||
if (ret) | ||
return ret.replacement; | ||
} | ||
if (forbidReactElements && isReactElement(value)) { | ||
@@ -51,3 +56,3 @@ throw genErr(genErrMsg('React element', path, valueName)); | ||
messageCore: errMsg, | ||
[stamp]: true | ||
[stamp]: true, | ||
}); | ||
@@ -54,0 +59,0 @@ return err; |
export { types }; | ||
declare const types: readonly [Type<undefined, unknown>, Type<number, unknown>, Type<number, unknown>, Type<number, unknown>, Type<Date, any>, Type<BigInt, any>, Type<RegExp, any>, Type<Map<any, any>, any[]>, Type<Set<unknown>, unknown[]>, Type<string, any>]; | ||
type Type<T, IntermediateType> = { | ||
is: (val: unknown) => asserts val is T; | ||
declare const types: readonly Type<any, any>[]; | ||
type Type<ValueType, IntermediateType> = { | ||
is: (val: unknown) => asserts val is ValueType; | ||
match: (str: string) => boolean; | ||
serialize: (val: T, serializer: (val: IntermediateType) => string) => string; | ||
deserialize: (str: string, deserializer: (str: string) => IntermediateType) => T; | ||
serialize: (val: ValueType, serializer: (val: IntermediateType) => string) => string; | ||
deserialize: (str: string, deserializer: (str: string) => IntermediateType) => ValueType; | ||
}; |
@@ -7,3 +7,3 @@ export { types }; | ||
serialize: () => '!undefined', | ||
deserialize: () => undefined | ||
deserialize: () => undefined, | ||
}), | ||
@@ -14,3 +14,3 @@ ts({ | ||
serialize: () => '!Infinity', | ||
deserialize: () => Infinity | ||
deserialize: () => Infinity, | ||
}), | ||
@@ -21,3 +21,3 @@ ts({ | ||
serialize: () => '!-Infinity', | ||
deserialize: () => -Infinity | ||
deserialize: () => -Infinity, | ||
}), | ||
@@ -28,3 +28,3 @@ ts({ | ||
serialize: () => '!NaN', | ||
deserialize: () => NaN | ||
deserialize: () => NaN, | ||
}), | ||
@@ -35,3 +35,3 @@ ts({ | ||
serialize: (val) => '!Date:' + val.toISOString(), | ||
deserialize: (str) => new Date(str.slice('!Date:'.length)) | ||
deserialize: (str) => new Date(str.slice('!Date:'.length)), | ||
}), | ||
@@ -47,3 +47,3 @@ ts({ | ||
return BigInt(str.slice('!BigInt:'.length)); | ||
} | ||
}, | ||
}), | ||
@@ -61,3 +61,3 @@ ts({ | ||
return new RegExp(pattern, flags); | ||
} | ||
}, | ||
}), | ||
@@ -68,3 +68,3 @@ ts({ | ||
serialize: (val, serializer) => '!Map:' + serializer(Array.from(val.entries())), | ||
deserialize: (str, deserializer) => new Map(deserializer(str.slice('!Map:'.length))) | ||
deserialize: (str, deserializer) => new Map(deserializer(str.slice('!Map:'.length))), | ||
}), | ||
@@ -75,3 +75,3 @@ ts({ | ||
serialize: (val, serializer) => '!Set:' + serializer(Array.from(val.values())), | ||
deserialize: (str, deserializer) => new Set(deserializer(str.slice('!Set:'.length))) | ||
deserialize: (str, deserializer) => new Set(deserializer(str.slice('!Set:'.length))), | ||
}), | ||
@@ -83,4 +83,4 @@ // Avoid collisions with the special strings defined above | ||
serialize: (val) => '!' + val, | ||
deserialize: (str) => str.slice(1) | ||
}) | ||
deserialize: (str) => str.slice(1), | ||
}), | ||
]; | ||
@@ -87,0 +87,0 @@ // Type check |
export { replacerWithPath }; | ||
export type { Iterable }; | ||
type Iterable = Record<string, unknown>; | ||
declare function replacerWithPath(replacer: (this: Iterable, key: string, value: unknown, path: string) => unknown, canBeFirstKey: boolean): (this: Iterable, key: string, value: unknown) => unknown; | ||
type Replacer = (this: Iterable, key: string, value: unknown, path: string) => unknown; | ||
declare function replacerWithPath(replacer: Replacer, canBeFirstKey: boolean): (this: Iterable, key: string, value: unknown) => unknown; |
{ | ||
"name": "@brillout/json-serializer", | ||
"version": "0.5.8", | ||
"dependencies": {}, | ||
"version": "0.5.9", | ||
"description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.", | ||
@@ -21,6 +20,6 @@ "main": "./index.mjs", | ||
"scripts": { | ||
"========= Dev": "", | ||
"dev": "pnpm run tsc:watch:cjs", | ||
"build": "pnpm run clean && pnpm run tsc:esm && pnpm run tsc:cjs", | ||
"test": "vitest", | ||
"docs": "mdocs", | ||
"========= Build": "", | ||
"build": "rm -rf dist/ && pnpm run tsc:esm && pnpm run tsc:cjs", | ||
"tsc:esm": "tsc", | ||
@@ -30,12 +29,26 @@ "tsc:cjs": "tsc --project ./tsconfig.cjs.json", | ||
"tsc:watch:cjs": "tsc --incremental --watch --project ./tsconfig.cjs.json", | ||
"clean": "rm -rf dist/", | ||
"reset": "git clean -Xdf && pnpm install && pnpm build", | ||
"========= Test": "", | ||
"test": "vitest", | ||
"========= Build readme.md": "", | ||
"docs": "mdocs", | ||
"========= Clean": "", | ||
"// Remove all generated files": "", | ||
"clean": "git clean -Xdf", | ||
"reset": "pnpm run clean && pnpm install && pnpm run build", | ||
"========= Formatting": "", | ||
"format": "pnpm run format:biome", | ||
"format:prettier": "git ls-files | egrep '\\.(json|js|jsx|css|ts|tsx|vue|mjs|cjs)$' | grep --invert-match package.json | xargs pnpm exec prettier --write", | ||
"format:biome": "biome format --write .", | ||
"format:check": "biome format . || echo Fix formatting by running: $ pnpm -w run format", | ||
"========= Release": "", | ||
"release": "release-me patch" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.7.2", | ||
"@brillout/mdocs": "^0.1.30", | ||
"@brillout/release-me": "^0.0.5", | ||
"@brillout/release-me": "^0.3.7", | ||
"@types/node": "^20.5.6", | ||
"@types/react": "^18.2.21", | ||
"lodash.isequal": "^4.5.0", | ||
"prettier": "^3.2.5", | ||
"react": "^17.0.2", | ||
@@ -42,0 +55,0 @@ "typescript": "^5.2.2", |
@@ -6,4 +6,6 @@ // Some tools don't support `package.json#exports`, such as: | ||
// prettier-ignore | ||
// biome-ignore format: | ||
'use strict'; | ||
// prettier-ignore | ||
// biome-ignore format: | ||
exports.parse = require('./dist/cjs/parse.js').parse; |
@@ -6,4 +6,6 @@ // Some tools don't support `package.json#exports`, such as: | ||
// prettier-ignore | ||
// biome-ignore format: | ||
'use strict'; | ||
// prettier-ignore | ||
// biome-ignore format: | ||
exports.stringify = require('./dist/cjs/stringify.js').stringify; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32746
594
10