@brillout/json-serializer
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -8,4 +8,4 @@ "use strict"; | ||
const isObject_1 = require("./utils/isObject"); | ||
function stringify(value, { forbidReactElements, space, valueName = 'value', sortObjectKeys } = {}) { | ||
const path = []; | ||
const replacerWithPath_1 = require("./replacerWithPath"); | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) { | ||
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`. | ||
@@ -17,14 +17,11 @@ // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions | ||
// - React elements | ||
const serializer = (val) => JSON.stringify(val, replacer, space); | ||
const serializer = (val) => JSON.stringify(val, (0, replacerWithPath_1.replacerWithPath)(replacer, !valueName), space); | ||
return serializer(value); | ||
function replacer(key, value) { | ||
if (key !== '') { | ||
path.push(key); | ||
} | ||
function replacer(key, value, path) { | ||
if (forbidReactElements && (0, isReactElement_1.isReactElement)(value)) { | ||
throw new Error(genErrMsg('React element')); | ||
throw genErr(genErrMsg('React element', path, valueName)); | ||
} | ||
if ((0, isCallable_1.isCallable)(value)) { | ||
const functionName = value.name; | ||
throw new Error(genErrMsg('function', path.length === 0 ? functionName : undefined)); | ||
throw genErr(genErrMsg('function', path, valueName, path.length === 0 ? functionName : undefined)); | ||
} | ||
@@ -49,9 +46,24 @@ const valueOriginal = this[key]; | ||
} | ||
function genErrMsg(valueType, valName) { | ||
const name = valName ? ' `' + valName + '`' : ''; | ||
const location = path.length === 0 ? '' : ` ${name ? 'at ' : ''}\`${valueName}[${path.map((p) => `'${p}'`).join('][')}]\``; | ||
const fallback = name === '' && location === '' ? ` ${valueName}` : ''; | ||
return `@brillout/json-serializer (https://github.com/brillout/json-serializer) cannot serialize${name}${location}${fallback} because it's a ${valueType}.`; | ||
} | ||
exports.stringify = stringify; | ||
function genErr(errMsg) { | ||
const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`); | ||
err.messageCore = errMsg; | ||
return err; | ||
} | ||
function genErrMsg(valueType, path, rootValueName, problematicValueName) { | ||
let subject; | ||
if (!path) { | ||
subject = rootValueName || problematicValueName || 'value'; | ||
} | ||
else { | ||
if (problematicValueName) { | ||
subject = problematicValueName + ' at '; | ||
} | ||
else { | ||
subject = ''; | ||
} | ||
subject = subject + (rootValueName || '') + path; | ||
} | ||
return `cannot serialize ${subject} because it's a ${valueType}`; | ||
} | ||
exports.stringify = stringify; |
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>]; | ||
declare type Type<T, IntermediateType> = { | ||
type Type<T, IntermediateType> = { | ||
is: (val: unknown) => asserts val is T; | ||
@@ -5,0 +5,0 @@ match: (str: string) => boolean; |
export { isObject }; | ||
declare type Object = Record<string, unknown>; | ||
type Object = Record<string, unknown>; | ||
declare function isObject(value: unknown): value is Object; |
@@ -6,4 +6,4 @@ export { stringify }; | ||
import { isObject } from './utils/isObject'; | ||
function stringify(value, { forbidReactElements, space, valueName = 'value', sortObjectKeys } = {}) { | ||
const path = []; | ||
import { replacerWithPath } from './replacerWithPath'; | ||
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) { | ||
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`. | ||
@@ -15,14 +15,11 @@ // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions | ||
// - React elements | ||
const serializer = (val) => JSON.stringify(val, replacer, space); | ||
const serializer = (val) => JSON.stringify(val, replacerWithPath(replacer, !valueName), space); | ||
return serializer(value); | ||
function replacer(key, value) { | ||
if (key !== '') { | ||
path.push(key); | ||
} | ||
function replacer(key, value, path) { | ||
if (forbidReactElements && isReactElement(value)) { | ||
throw new Error(genErrMsg('React element')); | ||
throw genErr(genErrMsg('React element', path, valueName)); | ||
} | ||
if (isCallable(value)) { | ||
const functionName = value.name; | ||
throw new Error(genErrMsg('function', path.length === 0 ? functionName : undefined)); | ||
throw genErr(genErrMsg('function', path, valueName, path.length === 0 ? functionName : undefined)); | ||
} | ||
@@ -47,8 +44,23 @@ const valueOriginal = this[key]; | ||
} | ||
function genErrMsg(valueType, valName) { | ||
const name = valName ? ' `' + valName + '`' : ''; | ||
const location = path.length === 0 ? '' : ` ${name ? 'at ' : ''}\`${valueName}[${path.map((p) => `'${p}'`).join('][')}]\``; | ||
const fallback = name === '' && location === '' ? ` ${valueName}` : ''; | ||
return `@brillout/json-serializer (https://github.com/brillout/json-serializer) cannot serialize${name}${location}${fallback} because it's a ${valueType}.`; | ||
} | ||
function genErr(errMsg) { | ||
const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`); | ||
err.messageCore = errMsg; | ||
return err; | ||
} | ||
function genErrMsg(valueType, path, rootValueName, problematicValueName) { | ||
let subject; | ||
if (!path) { | ||
subject = rootValueName || problematicValueName || 'value'; | ||
} | ||
else { | ||
if (problematicValueName) { | ||
subject = problematicValueName + ' at '; | ||
} | ||
else { | ||
subject = ''; | ||
} | ||
subject = subject + (rootValueName || '') + path; | ||
} | ||
return `cannot serialize ${subject} because it's a ${valueType}`; | ||
} |
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>]; | ||
declare type Type<T, IntermediateType> = { | ||
type Type<T, IntermediateType> = { | ||
is: (val: unknown) => asserts val is T; | ||
@@ -5,0 +5,0 @@ match: (str: string) => boolean; |
export { isObject }; | ||
declare type Object = Record<string, unknown>; | ||
type Object = Record<string, unknown>; | ||
declare function isObject(value: unknown): value is Object; |
{ | ||
"name": "@brillout/json-serializer", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"dependencies": {}, | ||
"description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.", | ||
"main": "./index.mjs", | ||
"license": "MIT", | ||
"exports": { | ||
@@ -23,3 +23,3 @@ ".": "./index.mjs", | ||
"build": "pnpm run clean && pnpm run tsc:esm && pnpm run tsc:cjs", | ||
"test": "self-import && node test/", | ||
"test": "vitest", | ||
"docs": "mdocs", | ||
@@ -37,8 +37,16 @@ "tsc:esm": "tsc", | ||
"@brillout/release-me": "^0.0.5", | ||
"@types/node": "17.0.13", | ||
"@types/node": "^20.5.6", | ||
"@types/react": "^18.2.21", | ||
"lodash.isequal": "^4.5.0", | ||
"react": "^17.0.2", | ||
"self-import": "^1.0.1", | ||
"typescript": "^4.8.2" | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.3" | ||
}, | ||
"packageManager": "pnpm@8.6.12", | ||
"// Use @vitest/snapshot PR https://github.com/vitest-dev/vitest/pull/3961": "", | ||
"pnpm": { | ||
"overrides": { | ||
"vitest>@vitest/snapshot": "npm:@brillout/vitest-snapshot@0.35.0-prerelease" | ||
} | ||
}, | ||
"files": [ | ||
@@ -51,3 +59,3 @@ "dist/", | ||
"repository": "github:brillout/json-serializer", | ||
"packageManager": "pnpm@7.9.5", | ||
"license": "MIT", | ||
"publishConfig": { | ||
@@ -54,0 +62,0 @@ "access": "public" |
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
29367
37
518
8