@brillout/json-serializer 0.5.11 → 0.5.12

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.
@@ -11,9 +11,10 @@ declare function stringify(value: unknown, { forbidReactElements, space, valueNa
11
11
  };
12
12
  }): string;
13
13
  type ErrAddendum = {
14
- messageCore: string;
14
+ messageCore: `cannot serialize ${string} because it's a function` | `cannot serialize ${string} because it's a React element`;
15
15
  value: unknown;
16
16
  path: Path;
17
17
  pathString: string;
18
+ subjectName: string;
18
19
  };
19
20
  declare function isJsonSerializerError(thing: unknown): thing is Error & ErrAddendum;
@@ -9,7 +9,6 @@ const replacerWithPath_1 = require("./utils/replacerWithPath");
9
9
  function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys,
10
10
  // Used by Vike: https://github.com/vikejs/vike/blob/b4ba6b70e6bdc2e1f460c0d2e4c3faae5d0a733c/vike/node/plugin/plugins/importUserCode/v1-design/getConfigValuesSerialized.ts#L78
11
11
  replacer: replacerUserProvided, } = {}) {
12
- const canBeFirstKey = !valueName;
13
12
  // The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
14
13
  // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
15
14
  // - This means we have total of 3 possible errors while serializing:
@@ -29,7 +28,6 @@ replacer: replacerUserProvided, } = {}) {
29
28
  value,
30
29
  valueType: 'React element',
31
30
  path,
32
- canBeFirstKey,
33
31
  rootValueName: valueName,
34
32
  });
35
33
  }
@@ -39,7 +37,6 @@ replacer: replacerUserProvided, } = {}) {
39
37
  value,
40
38
  valueType: 'function',
41
39
  path,
42
- canBeFirstKey,
43
40
  rootValueName: valueName,
44
41
  problematicValueName: path.length === 0 ? functionName : undefined,
45
42
  });
@@ -64,16 +61,18 @@ replacer: replacerUserProvided, } = {}) {
64
61
  }
65
62
  }
66
63
  exports.stringify = stringify;
67
- function genErr({ value, valueType, path, canBeFirstKey, rootValueName, problematicValueName, }) {
68
- const pathString = getPathString(path, canBeFirstKey);
69
- const errMsg = getErrMsg({ valueType, pathString, rootValueName, problematicValueName });
70
- const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`);
64
+ function genErr({ value, valueType, path, rootValueName, problematicValueName, }) {
65
+ const subjectName = getSubjectName({ path, rootValueName, problematicValueName });
66
+ const messageCore = `cannot serialize ${subjectName} because it's a ${valueType}`;
67
+ const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${messageCore}.`);
68
+ const pathString = getPathString(path, false);
71
69
  const errAddendum = {
72
- messageCore: errMsg,
73
70
  [stamp]: true,
71
+ messageCore,
74
72
  value,
75
73
  path,
76
74
  pathString,
75
+ subjectName,
77
76
  };
78
77
  Object.assign(err, errAddendum);
79
78
  return err;
@@ -83,21 +82,22 @@ function isJsonSerializerError(thing) {
83
82
  return (0, isObject_1.isObject)(thing) && thing[stamp] === true;
84
83
  }
85
84
  exports.isJsonSerializerError = isJsonSerializerError;
86
- function getErrMsg({ valueType, rootValueName, pathString, problematicValueName, }) {
87
- let subject;
85
+ function getSubjectName({ path, rootValueName, problematicValueName, }) {
86
+ const pathString = getPathString(path, !rootValueName);
87
+ let subjectName;
88
88
  if (!pathString) {
89
- subject = rootValueName || problematicValueName || 'value';
89
+ subjectName = rootValueName || problematicValueName || 'value';
90
90
  }
91
91
  else {
92
92
  if (problematicValueName) {
93
- subject = problematicValueName + ' at ';
93
+ subjectName = problematicValueName + ' at ';
94
94
  }
95
95
  else {
96
- subject = '';
96
+ subjectName = '';
97
97
  }
98
- subject = subject + (rootValueName || '') + pathString;
98
+ subjectName = subjectName + (rootValueName || '') + pathString;
99
99
  }
100
- return `cannot serialize ${subject} because it's a ${valueType}`;
100
+ return subjectName;
101
101
  }
102
102
  function getPathString(path, canBeFirstKey) {
103
103
  const pathString = path
@@ -11,9 +11,10 @@ declare function stringify(value: unknown, { forbidReactElements, space, valueNa
11
11
  };
12
12
  }): string;
13
13
  type ErrAddendum = {
14
- messageCore: string;
14
+ messageCore: `cannot serialize ${string} because it's a function` | `cannot serialize ${string} because it's a React element`;
15
15
  value: unknown;
16
16
  path: Path;
17
17
  pathString: string;
18
+ subjectName: string;
18
19
  };
19
20
  declare function isJsonSerializerError(thing: unknown): thing is Error & ErrAddendum;
@@ -8,7 +8,6 @@ import { replacerWithPath } from './utils/replacerWithPath';
8
8
  function stringify(value, { forbidReactElements, space, valueName, sortObjectKeys,
9
9
  // Used by Vike: https://github.com/vikejs/vike/blob/b4ba6b70e6bdc2e1f460c0d2e4c3faae5d0a733c/vike/node/plugin/plugins/importUserCode/v1-design/getConfigValuesSerialized.ts#L78
10
10
  replacer: replacerUserProvided, } = {}) {
11
- const canBeFirstKey = !valueName;
12
11
  // The only error `JSON.stringify()` can throw is `TypeError "cyclic object value"`.
13
12
  // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
14
13
  // - This means we have total of 3 possible errors while serializing:
@@ -28,7 +27,6 @@ replacer: replacerUserProvided, } = {}) {
28
27
  value,
29
28
  valueType: 'React element',
30
29
  path,
31
- canBeFirstKey,
32
30
  rootValueName: valueName,
33
31
  });
34
32
  }
@@ -38,7 +36,6 @@ replacer: replacerUserProvided, } = {}) {
38
36
  value,
39
37
  valueType: 'function',
40
38
  path,
41
- canBeFirstKey,
42
39
  rootValueName: valueName,
43
40
  problematicValueName: path.length === 0 ? functionName : undefined,
44
41
  });
@@ -62,16 +59,18 @@ replacer: replacerUserProvided, } = {}) {
62
59
  return value;
63
60
  }
64
61
  }
65
- function genErr({ value, valueType, path, canBeFirstKey, rootValueName, problematicValueName, }) {
66
- const pathString = getPathString(path, canBeFirstKey);
67
- const errMsg = getErrMsg({ valueType, pathString, rootValueName, problematicValueName });
68
- const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${errMsg}.`);
62
+ function genErr({ value, valueType, path, rootValueName, problematicValueName, }) {
63
+ const subjectName = getSubjectName({ path, rootValueName, problematicValueName });
64
+ const messageCore = `cannot serialize ${subjectName} because it's a ${valueType}`;
65
+ const err = new Error(`[@brillout/json-serializer](https://github.com/brillout/json-serializer) ${messageCore}.`);
66
+ const pathString = getPathString(path, false);
69
67
  const errAddendum = {
70
- messageCore: errMsg,
71
68
  [stamp]: true,
69
+ messageCore,
72
70
  value,
73
71
  path,
74
72
  pathString,
73
+ subjectName,
75
74
  };
76
75
  Object.assign(err, errAddendum);
77
76
  return err;
@@ -80,21 +79,22 @@ const stamp = '_isJsonSerializerError';
80
79
  function isJsonSerializerError(thing) {
81
80
  return isObject(thing) && thing[stamp] === true;
82
81
  }
83
- function getErrMsg({ valueType, rootValueName, pathString, problematicValueName, }) {
84
- let subject;
82
+ function getSubjectName({ path, rootValueName, problematicValueName, }) {
83
+ const pathString = getPathString(path, !rootValueName);
84
+ let subjectName;
85
85
  if (!pathString) {
86
- subject = rootValueName || problematicValueName || 'value';
86
+ subjectName = rootValueName || problematicValueName || 'value';
87
87
  }
88
88
  else {
89
89
  if (problematicValueName) {
90
- subject = problematicValueName + ' at ';
90
+ subjectName = problematicValueName + ' at ';
91
91
  }
92
92
  else {
93
- subject = '';
93
+ subjectName = '';
94
94
  }
95
- subject = subject + (rootValueName || '') + pathString;
95
+ subjectName = subjectName + (rootValueName || '') + pathString;
96
96
  }
97
- return `cannot serialize ${subject} because it's a ${valueType}`;
97
+ return subjectName;
98
98
  }
99
99
  function getPathString(path, canBeFirstKey) {
100
100
  const pathString = path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/json-serializer",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.",
5
5
  "main": "./index.mjs",
6
6
  "exports": {