@brillout/json-serializer 0.5.19 → 0.5.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{esm/stringify.d.ts → stringify.d.ts} +1 -1
- package/dist/{esm/stringify.js → stringify.js} +4 -4
- package/dist/utils/addPathToReplacer.d.ts +12 -0
- package/dist/utils/addPathToReplacer.js +24 -0
- package/examples.js +44 -0
- package/package.json +10 -16
- package/parse.d.ts +1 -1
- package/parse.js +3 -11
- package/readme.md +15 -16
- package/stringify.d.ts +1 -1
- package/stringify.js +3 -10
- package/dist/cjs/parse.js +0 -50
- package/dist/cjs/stringify.d.ts +0 -21
- package/dist/cjs/stringify.js +0 -122
- package/dist/cjs/types.js +0 -83
- package/dist/cjs/utils/isCallable.js +0 -6
- package/dist/cjs/utils/isObject.js +0 -12
- package/dist/cjs/utils/isReactElement.js +0 -8
- package/dist/cjs/utils/replacerWithPath.d.ts +0 -7
- package/dist/cjs/utils/replacerWithPath.js +0 -21
- package/dist/esm/parse.d.ts +0 -10
- package/dist/esm/types.d.ts +0 -8
- package/dist/esm/utils/isCallable.d.ts +0 -1
- package/dist/esm/utils/isKeyDotNotationCompatible.d.ts +0 -0
- package/dist/esm/utils/isKeyDotNotationCompatible.js +0 -1
- package/dist/esm/utils/isObject.d.ts +0 -3
- package/dist/esm/utils/isReactElement.d.ts +0 -1
- package/dist/esm/utils/replacerWithPath.d.ts +0 -7
- package/dist/esm/utils/replacerWithPath.js +0 -18
- /package/dist/{cjs/parse.d.ts → parse.d.ts} +0 -0
- /package/dist/{esm/parse.js → parse.js} +0 -0
- /package/dist/{cjs/types.d.ts → types.d.ts} +0 -0
- /package/dist/{esm/types.js → types.js} +0 -0
- /package/dist/{cjs/utils → utils}/isCallable.d.ts +0 -0
- /package/dist/{esm/utils → utils}/isCallable.js +0 -0
- /package/dist/{cjs/utils → utils}/isKeyDotNotationCompatible.d.ts +0 -0
- /package/dist/{cjs/utils → utils}/isKeyDotNotationCompatible.js +0 -0
- /package/dist/{cjs/utils → utils}/isObject.d.ts +0 -0
- /package/dist/{esm/utils → utils}/isObject.js +0 -0
- /package/dist/{cjs/utils → utils}/isReactElement.d.ts +0 -0
- /package/dist/{esm/utils → utils}/isReactElement.js +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { stringify };
|
|
2
2
|
export { isJsonSerializerError };
|
|
3
|
-
import { type Iterable, type Path } from './utils/
|
|
3
|
+
import { type Iterable, type Path } from './utils/addPathToReplacer.js';
|
|
4
4
|
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: {
|
|
5
5
|
forbidReactElements?: boolean;
|
|
6
6
|
space?: number;
|
|
@@ -4,7 +4,7 @@ import { types } from './types.js';
|
|
|
4
4
|
import { isReactElement } from './utils/isReactElement.js';
|
|
5
5
|
import { isCallable } from './utils/isCallable.js';
|
|
6
6
|
import { isObject } from './utils/isObject.js';
|
|
7
|
-
import {
|
|
7
|
+
import { addPathToReplacer } from './utils/addPathToReplacer.js';
|
|
8
8
|
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, } = {}) {
|
|
9
9
|
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
|
|
10
10
|
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
|
|
@@ -12,13 +12,13 @@ function stringify(value, { forbidReactElements, space, valueName, sortObjectKey
|
|
|
12
12
|
// - Cyclic references
|
|
13
13
|
// - Functions
|
|
14
14
|
// - React elements
|
|
15
|
-
const serializer = (val) => JSON.stringify(val,
|
|
15
|
+
const serializer = (val) => JSON.stringify(val, addPathToReplacer(replacer), space);
|
|
16
16
|
return serializer(value);
|
|
17
|
-
function replacer(key,
|
|
17
|
+
function replacer(key, _valueAfterNativeJsonStringify, path) {
|
|
18
18
|
const valueOriginal = this[key];
|
|
19
19
|
let value = valueOriginal;
|
|
20
20
|
{
|
|
21
|
-
const ret = replacerUserProvided?.call(this, key,
|
|
21
|
+
const ret = replacerUserProvided?.call(this, key, valueOriginal, serializer);
|
|
22
22
|
if (ret) {
|
|
23
23
|
value = ret.replacement;
|
|
24
24
|
if (ret.resolved !== false)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { addPathToReplacer };
|
|
2
|
+
export type { Iterable };
|
|
3
|
+
export type { Path };
|
|
4
|
+
type Path = (string | number)[];
|
|
5
|
+
type Iterable = Record<string, unknown>;
|
|
6
|
+
type Replacer = (this: Iterable, key: string, valueAfterNativeJsonStringify: unknown, path: Path) => unknown;
|
|
7
|
+
/**
|
|
8
|
+
* The `replacer()` callback of `JSON.stringify()` doesn't provide the path of the object property that is being stringified.
|
|
9
|
+
*
|
|
10
|
+
* `addPathToReplacer(replacer)` adds the property path to the `replacer()` callback as last parameter.
|
|
11
|
+
*/
|
|
12
|
+
declare function addPathToReplacer(replacer: Replacer): (this: Iterable, key: string, valueAfterNativeJsonStringify: unknown) => unknown;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { addPathToReplacer };
|
|
2
|
+
/**
|
|
3
|
+
* The `replacer()` callback of `JSON.stringify()` doesn't provide the path of the object property that is being stringified.
|
|
4
|
+
*
|
|
5
|
+
* `addPathToReplacer(replacer)` adds the property path to the `replacer()` callback as last parameter.
|
|
6
|
+
*/
|
|
7
|
+
function addPathToReplacer(replacer) {
|
|
8
|
+
const pathMap = new WeakMap();
|
|
9
|
+
return replacerForJsonStringify;
|
|
10
|
+
function replacerForJsonStringify(key, valueAfterNativeJsonStringify) {
|
|
11
|
+
const pathPrevious = pathMap.get(this) ?? [];
|
|
12
|
+
const path = [...pathPrevious];
|
|
13
|
+
if (key !== '') {
|
|
14
|
+
const pathEntry = !Array.isArray(this) ? key : parseInt(key, 10);
|
|
15
|
+
path.push(pathEntry);
|
|
16
|
+
}
|
|
17
|
+
if (isIterable(valueAfterNativeJsonStringify))
|
|
18
|
+
pathMap.set(valueAfterNativeJsonStringify, path);
|
|
19
|
+
return replacer.call(this, key, valueAfterNativeJsonStringify, path);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function isIterable(value) {
|
|
23
|
+
return value === Object(value);
|
|
24
|
+
}
|
package/examples.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run all examples in the examples/ directory
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { readdir } from 'fs/promises'
|
|
6
|
+
import { fileURLToPath } from 'url'
|
|
7
|
+
import { dirname, join } from 'path'
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
9
|
+
const __dirname = dirname(__filename)
|
|
10
|
+
|
|
11
|
+
runAllExamples()
|
|
12
|
+
|
|
13
|
+
async function runAllExamples() {
|
|
14
|
+
console.log('🎯 Running all json-serializer examples...\n')
|
|
15
|
+
|
|
16
|
+
// Read all .js files from examples directory
|
|
17
|
+
const files = await readdir(join(__dirname, 'examples'))
|
|
18
|
+
const examples = files.filter((file) => file.endsWith('.js')).sort()
|
|
19
|
+
|
|
20
|
+
let successCount = 0
|
|
21
|
+
let failureCount = 0
|
|
22
|
+
|
|
23
|
+
for (const example of examples) {
|
|
24
|
+
try {
|
|
25
|
+
console.log(`🚀 Running ${example}...`)
|
|
26
|
+
await import(`./examples/${example}`)
|
|
27
|
+
console.log(`✅ ${example} completed successfully\n`)
|
|
28
|
+
successCount++
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.log(`❌ ${example} failed: ${error.message}\n`)
|
|
31
|
+
failureCount++
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
console.log('='.repeat(50))
|
|
36
|
+
console.log(`✅ Successful: ${successCount}`)
|
|
37
|
+
if (failureCount > 0) {
|
|
38
|
+
console.log(`❌ Failed: ${failureCount}`)
|
|
39
|
+
console.log(`📁 Total: ${examples.length}`)
|
|
40
|
+
process.exit(1)
|
|
41
|
+
} else {
|
|
42
|
+
console.log('🎉 All examples completed successfully!')
|
|
43
|
+
}
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brillout/json-serializer",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.21",
|
|
4
4
|
"description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.",
|
|
5
5
|
"main": "./index.mjs",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./index.mjs",
|
|
9
9
|
"./parse": {
|
|
10
|
-
"import": "./dist/
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"default": "./dist/esm/parse.js"
|
|
10
|
+
"import": "./dist/parse.js",
|
|
11
|
+
"types": "./dist/parse.d.ts",
|
|
12
|
+
"default": "./dist/parse.js"
|
|
14
13
|
},
|
|
15
14
|
"./stringify": {
|
|
16
|
-
"import": "./dist/
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"default": "./dist/esm/stringify.js"
|
|
15
|
+
"import": "./dist/stringify.js",
|
|
16
|
+
"types": "./dist/stringify.d.ts",
|
|
17
|
+
"default": "./dist/stringify.js"
|
|
20
18
|
}
|
|
21
19
|
},
|
|
22
20
|
"devDependencies": {
|
|
@@ -45,15 +43,11 @@
|
|
|
45
43
|
},
|
|
46
44
|
"scripts": {
|
|
47
45
|
"========= Dev": "",
|
|
48
|
-
"dev": "
|
|
46
|
+
"dev": "tsc --watch",
|
|
49
47
|
"========= Build": "",
|
|
50
|
-
"build": "rm -rf dist/ &&
|
|
51
|
-
"tsc:esm": "tsc",
|
|
52
|
-
"tsc:cjs": "tsc --project ./tsconfig.cjs.json",
|
|
53
|
-
"tsc:watch:esm": "tsc --incremental --watch",
|
|
54
|
-
"tsc:watch:cjs": "tsc --incremental --watch --project ./tsconfig.cjs.json",
|
|
48
|
+
"build": "rm -rf dist/ && tsc",
|
|
55
49
|
"========= Test": "",
|
|
56
|
-
"test": "vitest",
|
|
50
|
+
"test": "node ./examples && vitest run",
|
|
57
51
|
"========= Build readme.md": "",
|
|
58
52
|
"docs": "mdocs",
|
|
59
53
|
"========= Formatting": "",
|
package/parse.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Help TS's resolver until it supports `package.json#exports`
|
|
2
|
-
export * from './dist/
|
|
2
|
+
export * from './dist/parse'
|
package/parse.js
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
// Some tools
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// - ESLint
|
|
5
|
-
// prettier-ignore
|
|
6
|
-
// biome-ignore format:
|
|
7
|
-
'use strict';
|
|
8
|
-
// prettier-ignore
|
|
9
|
-
// biome-ignore format:
|
|
10
|
-
exports.parse = require('./dist/cjs/parse.js').parse;
|
|
11
|
-
exports.parseTransform = require('./dist/cjs/parse.js').parseTransform
|
|
1
|
+
// Some tools still need this as of January 2025
|
|
2
|
+
export * from './dist/parse.js'
|
|
3
|
+
export { default } from './dist/parse.js'
|
package/readme.md
CHANGED
|
@@ -91,10 +91,10 @@ JSON is a good serializer for JavaScript values but
|
|
|
91
91
|
is lacking some JavaScript types such as `Date`:
|
|
92
92
|
|
|
93
93
|
~~~js
|
|
94
|
-
|
|
94
|
+
import assert from 'assert'
|
|
95
95
|
|
|
96
96
|
let obj = {
|
|
97
|
-
time: new Date('2042-01-01')
|
|
97
|
+
time: new Date('2042-01-01'),
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
// JSON converts dates to strings
|
|
@@ -107,12 +107,12 @@ assert(obj.time === '2042-01-01T00:00:00.000Z')
|
|
|
107
107
|
Whereas with `@brillout/json-serializer`:
|
|
108
108
|
|
|
109
109
|
~~~js
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
111
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
112
|
+
import assert from 'assert'
|
|
113
113
|
|
|
114
114
|
let obj = {
|
|
115
|
-
time: new Date('2042-01-01')
|
|
115
|
+
time: new Date('2042-01-01'),
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
// `@brillout/json-serializer` preserves Date
|
|
@@ -137,12 +137,12 @@ assert(obj.time.getTime() === new Date('2042-01-01').getTime())
|
|
|
137
137
|
|
|
138
138
|
~~~js
|
|
139
139
|
// npm install @brillout/json-serializer
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
141
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
142
142
|
|
|
143
143
|
const obj = {
|
|
144
144
|
hello: 'from the future',
|
|
145
|
-
time: new Date('2042-01-01')
|
|
145
|
+
time: new Date('2042-01-01'),
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
// Serialize with @brillout/json-serializer
|
|
@@ -161,17 +161,16 @@ Example exposing all differences between JSON and `@brillout/json-serializer`.
|
|
|
161
161
|
~~~js
|
|
162
162
|
// /examples/json-serializer.js
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const { stringify } = require('@brillout/json-serializer/stringify')
|
|
164
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
165
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
166
|
+
import assert from 'assert'
|
|
168
167
|
|
|
169
168
|
const obj = {
|
|
170
169
|
date: new Date(),
|
|
171
170
|
undefined: undefined,
|
|
172
171
|
NaN: NaN,
|
|
173
172
|
Infinity: Infinity,
|
|
174
|
-
regexp: /^\d+$/g
|
|
173
|
+
regexp: /^\d+$/g,
|
|
175
174
|
}
|
|
176
175
|
|
|
177
176
|
// All of `obj` can be serialized with @brillout/json-serializer
|
|
@@ -217,7 +216,7 @@ Let's see how `@brillout/json-serializer` serializes an object:
|
|
|
217
216
|
~~~js
|
|
218
217
|
// /examples/inspect.js
|
|
219
218
|
|
|
220
|
-
|
|
219
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
221
220
|
|
|
222
221
|
const obj = {
|
|
223
222
|
date: new Date(),
|
|
@@ -225,7 +224,7 @@ const obj = {
|
|
|
225
224
|
collision: '!undefined',
|
|
226
225
|
NaN: NaN,
|
|
227
226
|
Infinity: Infinity,
|
|
228
|
-
regexp: /^\d+$/g
|
|
227
|
+
regexp: /^\d+$/g,
|
|
229
228
|
}
|
|
230
229
|
|
|
231
230
|
console.log(stringify(obj, undefined, 2))
|
package/stringify.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Help TS's resolver until it supports `package.json#exports`
|
|
2
|
-
export * from './dist/
|
|
2
|
+
export * from './dist/stringify'
|
package/stringify.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
// Some tools
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// - ESLint
|
|
5
|
-
// prettier-ignore
|
|
6
|
-
// biome-ignore format:
|
|
7
|
-
'use strict';
|
|
8
|
-
// prettier-ignore
|
|
9
|
-
// biome-ignore format:
|
|
10
|
-
exports.stringify = require('./dist/cjs/stringify.js').stringify;
|
|
1
|
+
// Some tools still need this as of January 2025
|
|
2
|
+
export * from './dist/stringify.js'
|
|
3
|
+
export { default } from './dist/stringify.js'
|
package/dist/cjs/parse.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parse = parse;
|
|
4
|
-
exports.parseTransform = parseTransform;
|
|
5
|
-
const types_js_1 = require("./types.js");
|
|
6
|
-
function parse(str, options = {}) {
|
|
7
|
-
// We don't use the reviver option in `JSON.parse(str, reviver)` because it doesn't support `undefined` values
|
|
8
|
-
const value = JSON.parse(str);
|
|
9
|
-
return parseTransform(value, options);
|
|
10
|
-
}
|
|
11
|
-
function parseTransform(value, options = {}) {
|
|
12
|
-
if (typeof value === 'string') {
|
|
13
|
-
return reviver(value, options);
|
|
14
|
-
}
|
|
15
|
-
if (
|
|
16
|
-
// Also matches arrays
|
|
17
|
-
typeof value === 'object' &&
|
|
18
|
-
value !== null) {
|
|
19
|
-
Object.entries(value).forEach(([key, val]) => {
|
|
20
|
-
;
|
|
21
|
-
value[key] = parseTransform(val, options);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
function reviver(value, options) {
|
|
27
|
-
var _a;
|
|
28
|
-
const parser = (str) => parse(str, options);
|
|
29
|
-
{
|
|
30
|
-
const res = (_a = options.reviver) === null || _a === void 0 ? void 0 : _a.call(options,
|
|
31
|
-
// TO-DO/eventually: provide key if some user needs it
|
|
32
|
-
undefined, value, parser);
|
|
33
|
-
if (res) {
|
|
34
|
-
if (typeof res.replacement !== 'string') {
|
|
35
|
-
return res.replacement;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
value = res.replacement;
|
|
39
|
-
if (res.resolved)
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
for (const { match, deserialize } of types_js_1.types) {
|
|
45
|
-
if (match(value)) {
|
|
46
|
-
return deserialize(value, parser);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return value;
|
|
50
|
-
}
|
package/dist/cjs/stringify.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export { stringify };
|
|
2
|
-
export { isJsonSerializerError };
|
|
3
|
-
import { type Iterable, type Path } from './utils/replacerWithPath.js';
|
|
4
|
-
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: {
|
|
5
|
-
forbidReactElements?: boolean;
|
|
6
|
-
space?: number;
|
|
7
|
-
valueName?: string;
|
|
8
|
-
sortObjectKeys?: boolean;
|
|
9
|
-
replacer?: (this: Iterable, key: string, value: unknown, serializer: (value: unknown) => string) => {
|
|
10
|
-
replacement: unknown;
|
|
11
|
-
resolved?: boolean;
|
|
12
|
-
} | undefined;
|
|
13
|
-
}): string;
|
|
14
|
-
type ErrAddendum = {
|
|
15
|
-
messageCore: `cannot serialize ${string} because it's a function` | `cannot serialize ${string} because it's a React element`;
|
|
16
|
-
value: unknown;
|
|
17
|
-
path: Path;
|
|
18
|
-
pathString: string;
|
|
19
|
-
subjectName: string;
|
|
20
|
-
};
|
|
21
|
-
declare function isJsonSerializerError(thing: unknown): thing is Error & ErrAddendum;
|
package/dist/cjs/stringify.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringify = stringify;
|
|
4
|
-
exports.isJsonSerializerError = isJsonSerializerError;
|
|
5
|
-
const types_js_1 = require("./types.js");
|
|
6
|
-
const isReactElement_js_1 = require("./utils/isReactElement.js");
|
|
7
|
-
const isCallable_js_1 = require("./utils/isCallable.js");
|
|
8
|
-
const isObject_js_1 = require("./utils/isObject.js");
|
|
9
|
-
const replacerWithPath_js_1 = require("./utils/replacerWithPath.js");
|
|
10
|
-
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, } = {}) {
|
|
11
|
-
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
|
|
12
|
-
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
|
|
13
|
-
// - This means we have total of 3 possible errors while serializing:
|
|
14
|
-
// - Cyclic references
|
|
15
|
-
// - Functions
|
|
16
|
-
// - React elements
|
|
17
|
-
const serializer = (val) => JSON.stringify(val, (0, replacerWithPath_js_1.replacerWithPath)(replacer), space);
|
|
18
|
-
return serializer(value);
|
|
19
|
-
function replacer(key, valueAfterJSON, path) {
|
|
20
|
-
const valueOriginal = this[key];
|
|
21
|
-
let value = valueOriginal;
|
|
22
|
-
{
|
|
23
|
-
const ret = replacerUserProvided === null || replacerUserProvided === void 0 ? void 0 : replacerUserProvided.call(this, key, valueAfterJSON, serializer);
|
|
24
|
-
if (ret) {
|
|
25
|
-
value = ret.replacement;
|
|
26
|
-
if (ret.resolved !== false)
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (forbidReactElements && (0, isReactElement_js_1.isReactElement)(value)) {
|
|
31
|
-
throw genErr({
|
|
32
|
-
value,
|
|
33
|
-
valueType: 'React element',
|
|
34
|
-
path,
|
|
35
|
-
rootValueName: valueName,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
if ((0, isCallable_js_1.isCallable)(value)) {
|
|
39
|
-
const functionName = value.name;
|
|
40
|
-
throw genErr({
|
|
41
|
-
value,
|
|
42
|
-
valueType: 'function',
|
|
43
|
-
path,
|
|
44
|
-
rootValueName: valueName,
|
|
45
|
-
problematicValueName: path.length === 0 ? functionName : undefined,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
for (const { is, serialize } of types_js_1.types.slice().reverse()) {
|
|
49
|
-
if (is(value)) {
|
|
50
|
-
//@ts-ignore
|
|
51
|
-
return serialize(value, serializer);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (sortObjectKeys && (0, isObject_js_1.isObject)(value)) {
|
|
55
|
-
const copy = {};
|
|
56
|
-
Object.keys(value)
|
|
57
|
-
.sort()
|
|
58
|
-
.forEach((key) => {
|
|
59
|
-
copy[key] = value[key];
|
|
60
|
-
});
|
|
61
|
-
value = copy;
|
|
62
|
-
}
|
|
63
|
-
return value;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function genErr({ value, valueType, path, rootValueName, problematicValueName, }) {
|
|
67
|
-
const subjectName = getSubjectName({ path, rootValueName, problematicValueName });
|
|
68
|
-
const messageCore = `cannot serialize ${subjectName} because it's a ${valueType}`;
|
|
69
|
-
const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${messageCore}.`);
|
|
70
|
-
const pathString = getPathString(path, true);
|
|
71
|
-
const errAddendum = {
|
|
72
|
-
[stamp]: true,
|
|
73
|
-
messageCore,
|
|
74
|
-
value,
|
|
75
|
-
path,
|
|
76
|
-
pathString,
|
|
77
|
-
subjectName,
|
|
78
|
-
};
|
|
79
|
-
Object.assign(err, errAddendum);
|
|
80
|
-
return err;
|
|
81
|
-
}
|
|
82
|
-
const stamp = '_isJsonSerializerError';
|
|
83
|
-
function isJsonSerializerError(thing) {
|
|
84
|
-
return (0, isObject_js_1.isObject)(thing) && thing[stamp] === true;
|
|
85
|
-
}
|
|
86
|
-
function getSubjectName({ path, rootValueName, problematicValueName, }) {
|
|
87
|
-
const pathString = getPathString(path, !rootValueName);
|
|
88
|
-
let subjectName;
|
|
89
|
-
if (!pathString) {
|
|
90
|
-
subjectName = rootValueName || problematicValueName || 'value';
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
if (problematicValueName) {
|
|
94
|
-
subjectName = problematicValueName + ' at ';
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
subjectName = '';
|
|
98
|
-
}
|
|
99
|
-
subjectName = subjectName + (rootValueName || '') + pathString;
|
|
100
|
-
}
|
|
101
|
-
return subjectName;
|
|
102
|
-
}
|
|
103
|
-
function getPathString(path, canBeFirstKey) {
|
|
104
|
-
const pathString = path
|
|
105
|
-
.map((key, i) => {
|
|
106
|
-
if (typeof key === 'number') {
|
|
107
|
-
return `[${key}]`;
|
|
108
|
-
}
|
|
109
|
-
if (i === 0 && canBeFirstKey && isKeyDotNotationCompatible(key)) {
|
|
110
|
-
return key;
|
|
111
|
-
}
|
|
112
|
-
return getPropAccessNotation(key);
|
|
113
|
-
})
|
|
114
|
-
.join('');
|
|
115
|
-
return pathString;
|
|
116
|
-
}
|
|
117
|
-
function getPropAccessNotation(key) {
|
|
118
|
-
return typeof key === 'string' && isKeyDotNotationCompatible(key) ? `.${key}` : `[${JSON.stringify(key)}]`;
|
|
119
|
-
}
|
|
120
|
-
function isKeyDotNotationCompatible(key) {
|
|
121
|
-
return /^[a-z0-9\$_]+$/i.test(key);
|
|
122
|
-
}
|
package/dist/cjs/types.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.types = void 0;
|
|
4
|
-
const types = [
|
|
5
|
-
ts({
|
|
6
|
-
is: (val) => val === undefined,
|
|
7
|
-
match: (str) => str === '!undefined',
|
|
8
|
-
serialize: () => '!undefined',
|
|
9
|
-
deserialize: () => undefined,
|
|
10
|
-
}),
|
|
11
|
-
ts({
|
|
12
|
-
is: (val) => val === Infinity,
|
|
13
|
-
match: (str) => str === '!Infinity',
|
|
14
|
-
serialize: () => '!Infinity',
|
|
15
|
-
deserialize: () => Infinity,
|
|
16
|
-
}),
|
|
17
|
-
ts({
|
|
18
|
-
is: (val) => val === -Infinity,
|
|
19
|
-
match: (str) => str === '!-Infinity',
|
|
20
|
-
serialize: () => '!-Infinity',
|
|
21
|
-
deserialize: () => -Infinity,
|
|
22
|
-
}),
|
|
23
|
-
ts({
|
|
24
|
-
is: (val) => typeof val === 'number' && isNaN(val),
|
|
25
|
-
match: (str) => str === '!NaN',
|
|
26
|
-
serialize: () => '!NaN',
|
|
27
|
-
deserialize: () => NaN,
|
|
28
|
-
}),
|
|
29
|
-
ts({
|
|
30
|
-
is: (val) => val instanceof Date,
|
|
31
|
-
match: (str) => str.startsWith('!Date:'),
|
|
32
|
-
serialize: (val) => '!Date:' + val.toISOString(),
|
|
33
|
-
deserialize: (str) => new Date(str.slice('!Date:'.length)),
|
|
34
|
-
}),
|
|
35
|
-
ts({
|
|
36
|
-
is: (val) => typeof val === 'bigint',
|
|
37
|
-
match: (str) => str.startsWith('!BigInt:'),
|
|
38
|
-
serialize: (val) => '!BigInt:' + val.toString(),
|
|
39
|
-
deserialize: (str) => {
|
|
40
|
-
if (typeof BigInt === 'undefined') {
|
|
41
|
-
throw new Error('Your JavaScript environement does not support BigInt. Consider adding a polyfill.');
|
|
42
|
-
}
|
|
43
|
-
return BigInt(str.slice('!BigInt:'.length));
|
|
44
|
-
},
|
|
45
|
-
}),
|
|
46
|
-
ts({
|
|
47
|
-
is: (val) => val instanceof RegExp,
|
|
48
|
-
match: (str) => str.startsWith('!RegExp:'),
|
|
49
|
-
serialize: (val) => '!RegExp:' + val.toString(),
|
|
50
|
-
deserialize: (str) => {
|
|
51
|
-
str = str.slice('!RegExp:'.length);
|
|
52
|
-
// const args: string[] = str.match(/\/(.*?)\/([gimy])?$/)!
|
|
53
|
-
const args = str.match(/\/(.*)\/(.*)?/);
|
|
54
|
-
const pattern = args[1];
|
|
55
|
-
const flags = args[2];
|
|
56
|
-
return new RegExp(pattern, flags);
|
|
57
|
-
},
|
|
58
|
-
}),
|
|
59
|
-
ts({
|
|
60
|
-
is: (val) => val instanceof Map,
|
|
61
|
-
match: (str) => str.startsWith('!Map:'),
|
|
62
|
-
serialize: (val, serializer) => '!Map:' + serializer(Array.from(val.entries())),
|
|
63
|
-
deserialize: (str, parser) => new Map(parser(str.slice('!Map:'.length))),
|
|
64
|
-
}),
|
|
65
|
-
ts({
|
|
66
|
-
is: (val) => val instanceof Set,
|
|
67
|
-
match: (str) => str.startsWith('!Set:'),
|
|
68
|
-
serialize: (val, serializer) => '!Set:' + serializer(Array.from(val.values())),
|
|
69
|
-
deserialize: (str, parser) => new Set(parser(str.slice('!Set:'.length))),
|
|
70
|
-
}),
|
|
71
|
-
// Avoid collisions with the special strings defined above
|
|
72
|
-
ts({
|
|
73
|
-
is: (val) => typeof val === 'string' && val.startsWith('!'),
|
|
74
|
-
match: (str) => str.startsWith('!'),
|
|
75
|
-
serialize: (val) => '!' + val,
|
|
76
|
-
deserialize: (str) => str.slice(1),
|
|
77
|
-
}),
|
|
78
|
-
];
|
|
79
|
-
exports.types = types;
|
|
80
|
-
// Type check
|
|
81
|
-
function ts(t) {
|
|
82
|
-
return t;
|
|
83
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isObject = isObject;
|
|
4
|
-
function isObject(value) {
|
|
5
|
-
if (typeof value !== 'object' || value === null) {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
if (Array.isArray(value)) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
return true;
|
|
12
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isReactElement = isReactElement;
|
|
4
|
-
function isReactElement(value) {
|
|
5
|
-
return (typeof value === 'object' &&
|
|
6
|
-
value !== null &&
|
|
7
|
-
String(value['$$typeof']) === 'Symbol(react.element)');
|
|
8
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { replacerWithPath };
|
|
2
|
-
export type { Iterable };
|
|
3
|
-
export type { Path };
|
|
4
|
-
type Path = (string | number)[];
|
|
5
|
-
type Iterable = Record<string, unknown>;
|
|
6
|
-
type Replacer = (this: Iterable, key: string, value: unknown, path: Path) => unknown;
|
|
7
|
-
declare function replacerWithPath(replacer: Replacer): (this: Iterable, key: string, value: unknown) => unknown;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.replacerWithPath = replacerWithPath;
|
|
4
|
-
function replacerWithPath(replacer) {
|
|
5
|
-
const pathMap = new WeakMap();
|
|
6
|
-
return function (key, value) {
|
|
7
|
-
var _a;
|
|
8
|
-
const pathPrevious = (_a = pathMap.get(this)) !== null && _a !== void 0 ? _a : [];
|
|
9
|
-
const path = [...pathPrevious];
|
|
10
|
-
if (key !== '') {
|
|
11
|
-
const pathEntry = !Array.isArray(this) ? key : parseInt(key, 10);
|
|
12
|
-
path.push(pathEntry);
|
|
13
|
-
}
|
|
14
|
-
if (isIterable(value))
|
|
15
|
-
pathMap.set(value, path);
|
|
16
|
-
return replacer.call(this, key, value, path);
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function isIterable(value) {
|
|
20
|
-
return value === Object(value);
|
|
21
|
-
}
|
package/dist/esm/parse.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { parse };
|
|
2
|
-
export { parseTransform };
|
|
3
|
-
type Options = {
|
|
4
|
-
reviver?: (key: undefined | string, value: string, parser: (str: string) => unknown) => {
|
|
5
|
-
replacement: unknown;
|
|
6
|
-
resolved?: boolean;
|
|
7
|
-
} | undefined;
|
|
8
|
-
};
|
|
9
|
-
declare function parse(str: string, options?: Options): unknown;
|
|
10
|
-
declare function parseTransform(value: unknown, options?: Options): unknown;
|
package/dist/esm/types.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { types };
|
|
2
|
-
declare const types: readonly Type<any, any>[];
|
|
3
|
-
type Type<ValueType, IntermediateType> = {
|
|
4
|
-
is: (val: unknown) => asserts val is ValueType;
|
|
5
|
-
match: (str: string) => boolean;
|
|
6
|
-
serialize: (val: ValueType, serializer: (val: IntermediateType) => string) => string;
|
|
7
|
-
deserialize: (str: string, parser: (str: string) => IntermediateType) => ValueType;
|
|
8
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isCallable<T extends (...args: unknown[]) => unknown>(thing: T | unknown): thing is T;
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isReactElement(value: unknown): boolean;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { replacerWithPath };
|
|
2
|
-
export type { Iterable };
|
|
3
|
-
export type { Path };
|
|
4
|
-
type Path = (string | number)[];
|
|
5
|
-
type Iterable = Record<string, unknown>;
|
|
6
|
-
type Replacer = (this: Iterable, key: string, value: unknown, path: Path) => unknown;
|
|
7
|
-
declare function replacerWithPath(replacer: Replacer): (this: Iterable, key: string, value: unknown) => unknown;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export { replacerWithPath };
|
|
2
|
-
function replacerWithPath(replacer) {
|
|
3
|
-
const pathMap = new WeakMap();
|
|
4
|
-
return function (key, value) {
|
|
5
|
-
const pathPrevious = pathMap.get(this) ?? [];
|
|
6
|
-
const path = [...pathPrevious];
|
|
7
|
-
if (key !== '') {
|
|
8
|
-
const pathEntry = !Array.isArray(this) ? key : parseInt(key, 10);
|
|
9
|
-
path.push(pathEntry);
|
|
10
|
-
}
|
|
11
|
-
if (isIterable(value))
|
|
12
|
-
pathMap.set(value, path);
|
|
13
|
-
return replacer.call(this, key, value, path);
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
function isIterable(value) {
|
|
17
|
-
return value === Object(value);
|
|
18
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|