@brillout/json-serializer 0.5.18 → 0.5.19-commit-8a9605c

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.
Files changed (37) hide show
  1. package/dist/{esm/parse.js → parse.js} +1 -1
  2. package/dist/{esm/stringify.d.ts → stringify.d.ts} +1 -1
  3. package/dist/{esm/stringify.js → stringify.js} +5 -5
  4. package/package.json +10 -15
  5. package/parse.d.ts +1 -1
  6. package/parse.js +3 -11
  7. package/stringify.d.ts +1 -1
  8. package/stringify.js +3 -10
  9. package/dist/cjs/parse.js +0 -50
  10. package/dist/cjs/stringify.d.ts +0 -21
  11. package/dist/cjs/stringify.js +0 -122
  12. package/dist/cjs/types.js +0 -83
  13. package/dist/cjs/utils/isCallable.js +0 -6
  14. package/dist/cjs/utils/isObject.js +0 -12
  15. package/dist/cjs/utils/isReactElement.js +0 -8
  16. package/dist/cjs/utils/replacerWithPath.js +0 -21
  17. package/dist/esm/parse.d.ts +0 -10
  18. package/dist/esm/types.d.ts +0 -8
  19. package/dist/esm/utils/isCallable.d.ts +0 -1
  20. package/dist/esm/utils/isKeyDotNotationCompatible.d.ts +0 -0
  21. package/dist/esm/utils/isKeyDotNotationCompatible.js +0 -1
  22. package/dist/esm/utils/isObject.d.ts +0 -3
  23. package/dist/esm/utils/isReactElement.d.ts +0 -1
  24. package/dist/esm/utils/replacerWithPath.d.ts +0 -7
  25. /package/dist/{cjs/parse.d.ts → parse.d.ts} +0 -0
  26. /package/dist/{cjs/types.d.ts → types.d.ts} +0 -0
  27. /package/dist/{esm/types.js → types.js} +0 -0
  28. /package/dist/{cjs/utils → utils}/isCallable.d.ts +0 -0
  29. /package/dist/{esm/utils → utils}/isCallable.js +0 -0
  30. /package/dist/{cjs/utils → utils}/isKeyDotNotationCompatible.d.ts +0 -0
  31. /package/dist/{cjs/utils → utils}/isKeyDotNotationCompatible.js +0 -0
  32. /package/dist/{cjs/utils → utils}/isObject.d.ts +0 -0
  33. /package/dist/{esm/utils → utils}/isObject.js +0 -0
  34. /package/dist/{cjs/utils → utils}/isReactElement.d.ts +0 -0
  35. /package/dist/{esm/utils → utils}/isReactElement.js +0 -0
  36. /package/dist/{cjs/utils → utils}/replacerWithPath.d.ts +0 -0
  37. /package/dist/{esm/utils → utils}/replacerWithPath.js +0 -0
@@ -1,7 +1,7 @@
1
1
  export { parse };
2
2
  // Used by Vike: https://github.com/vikejs/vike/blob/b4ba6b70e6bdc2e1f460c0d2e4c3faae5d0a733c/vike/shared/page-configs/serialize/parseConfigValuesSerialized.ts#L13
3
3
  export { parseTransform };
4
- import { types } from './types';
4
+ import { types } from './types.js';
5
5
  function parse(str, options = {}) {
6
6
  // We don't use the reviver option in `JSON.parse(str, reviver)` because it doesn't support `undefined` values
7
7
  const value = JSON.parse(str);
@@ -1,6 +1,6 @@
1
1
  export { stringify };
2
2
  export { isJsonSerializerError };
3
- import { type Iterable, type Path } from './utils/replacerWithPath';
3
+ import { type Iterable, type Path } from './utils/replacerWithPath.js';
4
4
  declare function stringify(value: unknown, { forbidReactElements, space, valueName, sortObjectKeys, replacer: replacerUserProvided, }?: {
5
5
  forbidReactElements?: boolean;
6
6
  space?: number;
@@ -1,10 +1,10 @@
1
1
  export { stringify };
2
2
  export { isJsonSerializerError };
3
- import { types } from './types';
4
- import { isReactElement } from './utils/isReactElement';
5
- import { isCallable } from './utils/isCallable';
6
- import { isObject } from './utils/isObject';
7
- import { replacerWithPath } from './utils/replacerWithPath';
3
+ import { types } from './types.js';
4
+ import { isReactElement } from './utils/isReactElement.js';
5
+ import { isCallable } from './utils/isCallable.js';
6
+ import { isObject } from './utils/isObject.js';
7
+ import { replacerWithPath } from './utils/replacerWithPath.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
package/package.json CHANGED
@@ -1,21 +1,20 @@
1
1
  {
2
2
  "name": "@brillout/json-serializer",
3
- "version": "0.5.18",
3
+ "version": "0.5.19-commit-8a9605c",
4
4
  "description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.",
5
5
  "main": "./index.mjs",
6
+ "type": "module",
6
7
  "exports": {
7
8
  ".": "./index.mjs",
8
9
  "./parse": {
9
- "import": "./dist/esm/parse.js",
10
- "require": "./dist/cjs/parse.js",
11
- "types": "./dist/esm/parse.d.ts",
12
- "default": "./dist/esm/parse.js"
10
+ "import": "./dist/parse.js",
11
+ "types": "./dist/parse.d.ts",
12
+ "default": "./dist/parse.js"
13
13
  },
14
14
  "./stringify": {
15
- "import": "./dist/esm/stringify.js",
16
- "require": "./dist/cjs/stringify.js",
17
- "types": "./dist/esm/stringify.d.ts",
18
- "default": "./dist/esm/stringify.js"
15
+ "import": "./dist/stringify.js",
16
+ "types": "./dist/stringify.d.ts",
17
+ "default": "./dist/stringify.js"
19
18
  }
20
19
  },
21
20
  "devDependencies": {
@@ -44,13 +43,9 @@
44
43
  },
45
44
  "scripts": {
46
45
  "========= Dev": "",
47
- "dev": "pnpm run tsc:watch:cjs",
46
+ "dev": "npm tsc --watch",
48
47
  "========= Build": "",
49
- "build": "rm -rf dist/ && pnpm run tsc:esm && pnpm run tsc:cjs",
50
- "tsc:esm": "tsc",
51
- "tsc:cjs": "tsc --project ./tsconfig.cjs.json",
52
- "tsc:watch:esm": "tsc --incremental --watch",
53
- "tsc:watch:cjs": "tsc --incremental --watch --project ./tsconfig.cjs.json",
48
+ "build": "rm -rf dist/ && tsc",
54
49
  "========= Test": "",
55
50
  "test": "vitest",
56
51
  "========= Build readme.md": "",
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/esm/parse'
2
+ export * from './dist/parse'
package/parse.js CHANGED
@@ -1,11 +1,3 @@
1
- // Some tools don't support `package.json#exports`, such as:
2
- // - Nuxt v2
3
- // - Expo/Metro
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/stringify.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Help TS's resolver until it supports `package.json#exports`
2
- export * from './dist/esm/stringify'
2
+ export * from './dist/stringify'
package/stringify.js CHANGED
@@ -1,10 +1,3 @@
1
- // Some tools don't support `package.json#exports`, such as:
2
- // - Nuxt v2
3
- // - Expo/Metro
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_1 = require("./types");
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_1.types) {
45
- if (match(value)) {
46
- return deserialize(value, parser);
47
- }
48
- }
49
- return value;
50
- }
@@ -1,21 +0,0 @@
1
- export { stringify };
2
- export { isJsonSerializerError };
3
- import { type Iterable, type Path } from './utils/replacerWithPath';
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;
@@ -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_1 = require("./types");
6
- const isReactElement_1 = require("./utils/isReactElement");
7
- const isCallable_1 = require("./utils/isCallable");
8
- const isObject_1 = require("./utils/isObject");
9
- const replacerWithPath_1 = require("./utils/replacerWithPath");
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_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_1.isReactElement)(value)) {
31
- throw genErr({
32
- value,
33
- valueType: 'React element',
34
- path,
35
- rootValueName: valueName,
36
- });
37
- }
38
- if ((0, isCallable_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_1.types.slice().reverse()) {
49
- if (is(value)) {
50
- //@ts-ignore
51
- return serialize(value, serializer);
52
- }
53
- }
54
- if (sortObjectKeys && (0, isObject_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_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,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isCallable = isCallable;
4
- function isCallable(thing) {
5
- return thing instanceof Function || typeof thing === 'function';
6
- }
@@ -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,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
- }
@@ -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;
@@ -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,3 +0,0 @@
1
- export { isObject };
2
- type Object = Record<string, unknown>;
3
- declare function isObject(value: unknown): value is Object;
@@ -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;
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