@naturalcycles/js-lib 14.60.0 → 14.60.1

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.
@@ -7,7 +7,14 @@ exports._safeJsonStringify = void 0;
7
7
  * Based on: https://github.com/moll/json-stringify-safe/
8
8
  */
9
9
  function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
10
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
10
+ try {
11
+ // Try native first (as it's ~3 times faster)
12
+ return JSON.stringify(obj, replacer, spaces);
13
+ }
14
+ catch {
15
+ // Native failed - resort to the "safe" serializer
16
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
17
+ }
11
18
  }
12
19
  exports._safeJsonStringify = _safeJsonStringify;
13
20
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._stringifyAny = void 0;
4
4
  const error_util_1 = require("../error/error.util");
5
5
  const json_util_1 = require("./json.util");
6
- const jsonStringifyFn = (obj, reviver, space) => JSON.stringify(obj, reviver, space);
6
+ const safeJsonStringify_1 = require("./safeJsonStringify");
7
7
  /**
8
8
  * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
9
9
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
@@ -83,7 +83,7 @@ function _stringifyAny(obj, opt = {}) {
83
83
  // Other
84
84
  //
85
85
  try {
86
- const { stringifyFn = jsonStringifyFn } = opt;
86
+ const { stringifyFn = safeJsonStringify_1._safeJsonStringify } = opt;
87
87
  s = stringifyFn(obj, undefined, 2);
88
88
  }
89
89
  catch {
@@ -4,7 +4,14 @@
4
4
  * Based on: https://github.com/moll/json-stringify-safe/
5
5
  */
6
6
  export function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
7
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
7
+ try {
8
+ // Try native first (as it's ~3 times faster)
9
+ return JSON.stringify(obj, replacer, spaces);
10
+ }
11
+ catch (_a) {
12
+ // Native failed - resort to the "safe" serializer
13
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
14
+ }
8
15
  }
9
16
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
10
17
  function serializer(replacer, cycleReplacer) {
@@ -1,6 +1,6 @@
1
1
  import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util';
2
2
  import { _jsonParseIfPossible } from './json.util';
3
- const jsonStringifyFn = (obj, reviver, space) => JSON.stringify(obj, reviver, space);
3
+ import { _safeJsonStringify } from './safeJsonStringify';
4
4
  /**
5
5
  * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
6
6
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
@@ -80,7 +80,7 @@ export function _stringifyAny(obj, opt = {}) {
80
80
  // Other
81
81
  //
82
82
  try {
83
- const { stringifyFn = jsonStringifyFn } = opt;
83
+ const { stringifyFn = _safeJsonStringify } = opt;
84
84
  s = stringifyFn(obj, undefined, 2);
85
85
  }
86
86
  catch (_a) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.60.0",
3
+ "version": "14.60.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -11,7 +11,13 @@ export function _safeJsonStringify(
11
11
  spaces?: number,
12
12
  cycleReplacer?: Reviver,
13
13
  ): string {
14
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
14
+ try {
15
+ // Try native first (as it's ~3 times faster)
16
+ return JSON.stringify(obj, replacer, spaces)
17
+ } catch {
18
+ // Native failed - resort to the "safe" serializer
19
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
20
+ }
15
21
  }
16
22
 
17
23
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
@@ -1,12 +1,10 @@
1
1
  import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util'
2
2
  import { Reviver } from '../types'
3
3
  import { _jsonParseIfPossible } from './json.util'
4
+ import { _safeJsonStringify } from './safeJsonStringify'
4
5
 
5
6
  export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string
6
7
 
7
- const jsonStringifyFn: JsonStringifyFunction = (obj, reviver, space) =>
8
- JSON.stringify(obj, reviver, space)
9
-
10
8
  export interface StringifyAnyOptions {
11
9
  /**
12
10
  * @default 10_000
@@ -116,7 +114,7 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
116
114
  // Other
117
115
  //
118
116
  try {
119
- const { stringifyFn = jsonStringifyFn } = opt
117
+ const { stringifyFn = _safeJsonStringify } = opt
120
118
 
121
119
  s = stringifyFn(obj, undefined, 2)
122
120
  } catch {