@brillout/json-serializer 0.5.6 → 0.5.7
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/cjs/stringify.d.ts +5 -0
- package/dist/cjs/stringify.js +11 -3
- package/dist/cjs/utils/isKeyDotNotationCompatible.d.ts +1 -0
- package/dist/cjs/utils/isKeyDotNotationCompatible.js +7 -0
- package/dist/cjs/{replacerWithPath.js → utils/replacerWithPath.js} +7 -5
- package/dist/esm/stringify.d.ts +5 -0
- package/dist/esm/stringify.js +10 -2
- package/dist/esm/utils/isKeyDotNotationCompatible.d.ts +1 -0
- package/dist/esm/utils/isKeyDotNotationCompatible.js +3 -0
- package/dist/esm/utils/replacerWithPath.js +23 -0
- package/package.json +1 -1
- package/dist/esm/replacerWithPath.js +0 -21
- /package/dist/cjs/{replacerWithPath.d.ts → utils/replacerWithPath.d.ts} +0 -0
- /package/dist/esm/{replacerWithPath.d.ts → utils/replacerWithPath.d.ts} +0 -0
package/dist/cjs/stringify.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export { stringify };
|
|
2
|
+
export { isJsonSerializerError };
|
|
2
3
|
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys }?: {
|
|
3
4
|
forbidReactElements?: boolean;
|
|
4
5
|
space?: number;
|
|
5
6
|
valueName?: string;
|
|
6
7
|
sortObjectKeys?: boolean;
|
|
7
8
|
}): string;
|
|
9
|
+
type JsonSerializerError = Error & {
|
|
10
|
+
messageCore: string;
|
|
11
|
+
};
|
|
12
|
+
declare function isJsonSerializerError(thing: unknown): thing is JsonSerializerError;
|
package/dist/cjs/stringify.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringify = void 0;
|
|
3
|
+
exports.isJsonSerializerError = exports.stringify = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const isReactElement_1 = require("./utils/isReactElement");
|
|
6
6
|
const isCallable_1 = require("./utils/isCallable");
|
|
7
7
|
const isObject_1 = require("./utils/isObject");
|
|
8
|
-
const replacerWithPath_1 = require("./replacerWithPath");
|
|
8
|
+
const replacerWithPath_1 = require("./utils/replacerWithPath");
|
|
9
9
|
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) {
|
|
10
10
|
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
|
|
11
11
|
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
|
|
@@ -43,11 +43,19 @@ function stringify(value, { forbidReactElements, space, valueName, sortObjectKey
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
exports.stringify = stringify;
|
|
46
|
+
const stamp = '_isJsonSerializerError';
|
|
46
47
|
function genErr(errMsg) {
|
|
47
48
|
const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`);
|
|
48
|
-
err
|
|
49
|
+
Object.assign(err, {
|
|
50
|
+
messageCore: errMsg,
|
|
51
|
+
[stamp]: true
|
|
52
|
+
});
|
|
49
53
|
return err;
|
|
50
54
|
}
|
|
55
|
+
function isJsonSerializerError(thing) {
|
|
56
|
+
return (0, isObject_1.isObject)(thing) && thing[stamp] === true;
|
|
57
|
+
}
|
|
58
|
+
exports.isJsonSerializerError = isJsonSerializerError;
|
|
51
59
|
function genErrMsg(valueType, path, rootValueName, problematicValueName) {
|
|
52
60
|
let subject;
|
|
53
61
|
if (!path) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isKeyDotNotationCompatible(key: string): boolean;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isKeyDotNotationCompatible = void 0;
|
|
4
|
+
function isKeyDotNotationCompatible(key) {
|
|
5
|
+
return /^[a-z0-9\$_]+$/i.test(key);
|
|
6
|
+
}
|
|
7
|
+
exports.isKeyDotNotationCompatible = isKeyDotNotationCompatible;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.replacerWithPath = void 0;
|
|
4
|
+
// https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path/63957172#63957172
|
|
5
|
+
const isKeyDotNotationCompatible_1 = require("./isKeyDotNotationCompatible");
|
|
4
6
|
function replacerWithPath(replacer, canBeFirstKey) {
|
|
5
7
|
const paths = new WeakMap();
|
|
6
8
|
return function (key, value) {
|
|
7
9
|
const prefix = paths.get(this);
|
|
8
|
-
const path = (prefix !== null && prefix !== void 0 ? prefix : '') + (key ?
|
|
10
|
+
const path = (prefix !== null && prefix !== void 0 ? prefix : '') + (key ? getPropAccessNotation(key, this, !prefix && canBeFirstKey) : '');
|
|
9
11
|
if (isIterable(value))
|
|
10
12
|
paths.set(value, path);
|
|
11
13
|
return replacer.call(this, key, value, path);
|
|
@@ -15,10 +17,10 @@ exports.replacerWithPath = replacerWithPath;
|
|
|
15
17
|
function isIterable(value) {
|
|
16
18
|
return value === Object(value);
|
|
17
19
|
}
|
|
18
|
-
function
|
|
20
|
+
function getPropAccessNotation(key, obj, isFirstKey) {
|
|
19
21
|
if (Array.isArray(obj))
|
|
20
22
|
return `[${key}]`;
|
|
21
|
-
if (
|
|
22
|
-
return !isFirstKey ? `.${key}` : key;
|
|
23
|
-
return `[${JSON.stringify(key)}]`;
|
|
23
|
+
if ((0, isKeyDotNotationCompatible_1.isKeyDotNotationCompatible)(key))
|
|
24
|
+
return !isFirstKey ? `.${key}` : key; // Dot notation
|
|
25
|
+
return `[${JSON.stringify(key)}]`; // Bracket notation
|
|
24
26
|
}
|
package/dist/esm/stringify.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export { stringify };
|
|
2
|
+
export { isJsonSerializerError };
|
|
2
3
|
declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys }?: {
|
|
3
4
|
forbidReactElements?: boolean;
|
|
4
5
|
space?: number;
|
|
5
6
|
valueName?: string;
|
|
6
7
|
sortObjectKeys?: boolean;
|
|
7
8
|
}): string;
|
|
9
|
+
type JsonSerializerError = Error & {
|
|
10
|
+
messageCore: string;
|
|
11
|
+
};
|
|
12
|
+
declare function isJsonSerializerError(thing: unknown): thing is JsonSerializerError;
|
package/dist/esm/stringify.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export { stringify };
|
|
2
|
+
export { isJsonSerializerError };
|
|
2
3
|
import { types } from './types';
|
|
3
4
|
import { isReactElement } from './utils/isReactElement';
|
|
4
5
|
import { isCallable } from './utils/isCallable';
|
|
5
6
|
import { isObject } from './utils/isObject';
|
|
6
|
-
import { replacerWithPath } from './replacerWithPath';
|
|
7
|
+
import { replacerWithPath } from './utils/replacerWithPath';
|
|
7
8
|
function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys } = {}) {
|
|
8
9
|
// The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
|
|
9
10
|
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
|
|
@@ -40,11 +41,18 @@ function stringify(value, { forbidReactElements, space, valueName, sortObjectKey
|
|
|
40
41
|
return value;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
44
|
+
const stamp = '_isJsonSerializerError';
|
|
43
45
|
function genErr(errMsg) {
|
|
44
46
|
const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`);
|
|
45
|
-
err
|
|
47
|
+
Object.assign(err, {
|
|
48
|
+
messageCore: errMsg,
|
|
49
|
+
[stamp]: true
|
|
50
|
+
});
|
|
46
51
|
return err;
|
|
47
52
|
}
|
|
53
|
+
function isJsonSerializerError(thing) {
|
|
54
|
+
return isObject(thing) && thing[stamp] === true;
|
|
55
|
+
}
|
|
48
56
|
function genErrMsg(valueType, path, rootValueName, problematicValueName) {
|
|
49
57
|
let subject;
|
|
50
58
|
if (!path) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isKeyDotNotationCompatible(key: string): boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { replacerWithPath };
|
|
2
|
+
// https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path/63957172#63957172
|
|
3
|
+
import { isKeyDotNotationCompatible } from './isKeyDotNotationCompatible';
|
|
4
|
+
function replacerWithPath(replacer, canBeFirstKey) {
|
|
5
|
+
const paths = new WeakMap();
|
|
6
|
+
return function (key, value) {
|
|
7
|
+
const prefix = paths.get(this);
|
|
8
|
+
const path = (prefix ?? '') + (key ? getPropAccessNotation(key, this, !prefix && canBeFirstKey) : '');
|
|
9
|
+
if (isIterable(value))
|
|
10
|
+
paths.set(value, path);
|
|
11
|
+
return replacer.call(this, key, value, path);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function isIterable(value) {
|
|
15
|
+
return value === Object(value);
|
|
16
|
+
}
|
|
17
|
+
function getPropAccessNotation(key, obj, isFirstKey) {
|
|
18
|
+
if (Array.isArray(obj))
|
|
19
|
+
return `[${key}]`;
|
|
20
|
+
if (isKeyDotNotationCompatible(key))
|
|
21
|
+
return !isFirstKey ? `.${key}` : key; // Dot notation
|
|
22
|
+
return `[${JSON.stringify(key)}]`; // Bracket notation
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export { replacerWithPath };
|
|
2
|
-
function replacerWithPath(replacer, canBeFirstKey) {
|
|
3
|
-
const paths = new WeakMap();
|
|
4
|
-
return function (key, value) {
|
|
5
|
-
const prefix = paths.get(this);
|
|
6
|
-
const path = (prefix ?? '') + (key ? getKeyName(key, this, !prefix && canBeFirstKey) : '');
|
|
7
|
-
if (isIterable(value))
|
|
8
|
-
paths.set(value, path);
|
|
9
|
-
return replacer.call(this, key, value, path);
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function isIterable(value) {
|
|
13
|
-
return value === Object(value);
|
|
14
|
-
}
|
|
15
|
-
function getKeyName(key, obj, isFirstKey) {
|
|
16
|
-
if (Array.isArray(obj))
|
|
17
|
-
return `[${key}]`;
|
|
18
|
-
if (/^[a-z0-9\$_]+$/i.test(key))
|
|
19
|
-
return !isFirstKey ? `.${key}` : key;
|
|
20
|
-
return `[${JSON.stringify(key)}]`;
|
|
21
|
-
}
|
|
File without changes
|
|
File without changes
|