@naturalcycles/js-lib 14.127.0 → 14.128.0
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.
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import type { Reviver } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
4
|
+
* in various cases.
|
|
5
|
+
*
|
|
6
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
7
|
+
*
|
|
8
|
+
* Defaults to _safeJsonStringify.
|
|
9
|
+
*
|
|
10
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
11
|
+
* as pretty-printing function.
|
|
12
|
+
*
|
|
13
|
+
* It's recommended that this function is circular-reference-safe.
|
|
14
|
+
*/
|
|
15
|
+
export declare function setGlobalStringifyFunction(fn: JsonStringifyFunction): void;
|
|
2
16
|
export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string;
|
|
3
17
|
export interface StringifyAnyOptions {
|
|
4
18
|
/**
|
|
@@ -22,7 +36,7 @@ export interface StringifyAnyOptions {
|
|
|
22
36
|
* Allows to pass custom "stringify function".
|
|
23
37
|
* E.g in Node.js you can pass `util.inspect` instead.
|
|
24
38
|
*
|
|
25
|
-
*
|
|
39
|
+
* Defaults to `globalStringifyFunction`, which defaults to `_safeJsonStringify`
|
|
26
40
|
*/
|
|
27
41
|
stringifyFn?: JsonStringifyFunction;
|
|
28
42
|
}
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._stringifyAny = void 0;
|
|
3
|
+
exports._stringifyAny = exports.setGlobalStringifyFunction = void 0;
|
|
4
4
|
const error_util_1 = require("../error/error.util");
|
|
5
5
|
const json_util_1 = require("./json.util");
|
|
6
6
|
const safeJsonStringify_1 = require("./safeJsonStringify");
|
|
7
|
+
let globalStringifyFunction = safeJsonStringify_1._safeJsonStringify;
|
|
8
|
+
/**
|
|
9
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
10
|
+
* in various cases.
|
|
11
|
+
*
|
|
12
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to _safeJsonStringify.
|
|
15
|
+
*
|
|
16
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
17
|
+
* as pretty-printing function.
|
|
18
|
+
*
|
|
19
|
+
* It's recommended that this function is circular-reference-safe.
|
|
20
|
+
*/
|
|
21
|
+
function setGlobalStringifyFunction(fn) {
|
|
22
|
+
globalStringifyFunction = fn;
|
|
23
|
+
}
|
|
24
|
+
exports.setGlobalStringifyFunction = setGlobalStringifyFunction;
|
|
7
25
|
/**
|
|
8
26
|
* Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
|
|
9
27
|
* Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
|
|
@@ -86,7 +104,7 @@ function _stringifyAny(obj, opt = {}) {
|
|
|
86
104
|
// Other
|
|
87
105
|
//
|
|
88
106
|
try {
|
|
89
|
-
const { stringifyFn =
|
|
107
|
+
const { stringifyFn = globalStringifyFunction } = opt;
|
|
90
108
|
s = stringifyFn(obj, undefined, 2);
|
|
91
109
|
}
|
|
92
110
|
catch {
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util';
|
|
2
2
|
import { _jsonParseIfPossible } from './json.util';
|
|
3
3
|
import { _safeJsonStringify } from './safeJsonStringify';
|
|
4
|
+
let globalStringifyFunction = _safeJsonStringify;
|
|
5
|
+
/**
|
|
6
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
7
|
+
* in various cases.
|
|
8
|
+
*
|
|
9
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
10
|
+
*
|
|
11
|
+
* Defaults to _safeJsonStringify.
|
|
12
|
+
*
|
|
13
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
14
|
+
* as pretty-printing function.
|
|
15
|
+
*
|
|
16
|
+
* It's recommended that this function is circular-reference-safe.
|
|
17
|
+
*/
|
|
18
|
+
export function setGlobalStringifyFunction(fn) {
|
|
19
|
+
globalStringifyFunction = fn;
|
|
20
|
+
}
|
|
4
21
|
/**
|
|
5
22
|
* Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
|
|
6
23
|
* Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
|
|
@@ -83,7 +100,7 @@ export function _stringifyAny(obj, opt = {}) {
|
|
|
83
100
|
// Other
|
|
84
101
|
//
|
|
85
102
|
try {
|
|
86
|
-
const { stringifyFn =
|
|
103
|
+
const { stringifyFn = globalStringifyFunction } = opt;
|
|
87
104
|
s = stringifyFn(obj, undefined, 2);
|
|
88
105
|
}
|
|
89
106
|
catch (_a) {
|
package/package.json
CHANGED
|
@@ -3,6 +3,25 @@ import type { Reviver } from '../types'
|
|
|
3
3
|
import { _jsonParseIfPossible } from './json.util'
|
|
4
4
|
import { _safeJsonStringify } from './safeJsonStringify'
|
|
5
5
|
|
|
6
|
+
let globalStringifyFunction: JsonStringifyFunction = _safeJsonStringify
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
10
|
+
* in various cases.
|
|
11
|
+
*
|
|
12
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to _safeJsonStringify.
|
|
15
|
+
*
|
|
16
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
17
|
+
* as pretty-printing function.
|
|
18
|
+
*
|
|
19
|
+
* It's recommended that this function is circular-reference-safe.
|
|
20
|
+
*/
|
|
21
|
+
export function setGlobalStringifyFunction(fn: JsonStringifyFunction): void {
|
|
22
|
+
globalStringifyFunction = fn
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string
|
|
7
26
|
|
|
8
27
|
export interface StringifyAnyOptions {
|
|
@@ -30,7 +49,7 @@ export interface StringifyAnyOptions {
|
|
|
30
49
|
* Allows to pass custom "stringify function".
|
|
31
50
|
* E.g in Node.js you can pass `util.inspect` instead.
|
|
32
51
|
*
|
|
33
|
-
*
|
|
52
|
+
* Defaults to `globalStringifyFunction`, which defaults to `_safeJsonStringify`
|
|
34
53
|
*/
|
|
35
54
|
stringifyFn?: JsonStringifyFunction
|
|
36
55
|
}
|
|
@@ -120,7 +139,7 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
|
|
|
120
139
|
// Other
|
|
121
140
|
//
|
|
122
141
|
try {
|
|
123
|
-
const { stringifyFn =
|
|
142
|
+
const { stringifyFn = globalStringifyFunction } = opt
|
|
124
143
|
|
|
125
144
|
s = stringifyFn(obj, undefined, 2)
|
|
126
145
|
} catch {
|