@brillout/json-serializer 0.5.20 → 0.5.22

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/parse.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  export { parse };
2
2
  export { parseTransform };
3
+ export type { Reviver };
4
+ type Reviver = (key: undefined | string, value: string, parser: (str: string) => unknown) => {
5
+ replacement: unknown;
6
+ resolved?: boolean;
7
+ } | undefined;
3
8
  type Options = {
4
- reviver?: (key: undefined | string, value: string, parser: (str: string) => unknown) => {
5
- replacement: unknown;
6
- resolved?: boolean;
7
- } | undefined;
9
+ reviver?: Reviver;
8
10
  };
9
11
  declare function parse(str: string, options?: Options): unknown;
10
12
  declare function parseTransform(value: unknown, options?: Options): unknown;
@@ -1,15 +1,17 @@
1
1
  export { stringify };
2
2
  export { isJsonSerializerError };
3
- import { type Iterable, type Path } from './utils/replacerWithPath.js';
3
+ export type { Replacer };
4
+ import { type Iterable, type Path } from './utils/addPathToReplacer.js';
5
+ type Replacer = (this: Iterable, key: string, value: unknown, serializer: (value: unknown) => string) => {
6
+ replacement: unknown;
7
+ resolved?: boolean;
8
+ } | undefined;
4
9
  declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: {
5
10
  forbidReactElements?: boolean;
6
11
  space?: number;
7
12
  valueName?: string;
8
13
  sortObjectKeys?: boolean;
9
- replacer?: (this: Iterable, key: string, value: unknown, serializer: (value: unknown) => string) => {
10
- replacement: unknown;
11
- resolved?: boolean;
12
- } | undefined;
14
+ replacer?: Replacer;
13
15
  }): string;
14
16
  type ErrAddendum = {
15
17
  messageCore: `cannot serialize ${string} because it's a function` | `cannot serialize ${string} because it's a React element`;
package/dist/stringify.js CHANGED
@@ -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 { replacerWithPath } from './utils/replacerWithPath.js';
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, replacerWithPath(replacer), space);
15
+ const serializer = (val) => JSON.stringify(val, addPathToReplacer(replacer), space);
16
16
  return serializer(value);
17
- function replacer(key, valueAfterJSON, path) {
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, valueAfterJSON, serializer);
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/json-serializer",
3
- "version": "0.5.20",
3
+ "version": "0.5.22",
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",
@@ -47,7 +47,7 @@
47
47
  "========= Build": "",
48
48
  "build": "rm -rf dist/ && tsc",
49
49
  "========= Test": "",
50
- "test": "vitest",
50
+ "test": "node ./examples && vitest run",
51
51
  "========= Build readme.md": "",
52
52
  "docs": "mdocs",
53
53
  "========= Formatting": "",
@@ -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
- }