-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
42 lines (39 loc) · 1.18 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { maxDepthReplacer, serializeMapReplacer } from "./replacers.ts";
import { Replacer } from "./types.d.ts";
import { isPlainObject, pipeReplacers } from "./utils.ts";
type ReplacerOptions = {
/** max level of nested objects to print */
maxDepth?: number;
/** enables serialization of ES6 Map */
supportMap?: boolean;
};
const replacerFromOptions = ({
maxDepth,
supportMap,
}: ReplacerOptions) =>
pipeReplacers(
[supportMap && serializeMapReplacer, maxDepth && maxDepthReplacer(maxDepth)]
.filter(
Boolean,
) as Replacer[],
);
/**
* JSON.stringify on steroids, second argument could be an object with options to create replacers.
* E.g. `stringify(obj, { maxDepth: 2 })` to print only items no more than 2 levels deep in input object.
*/
export const stringify = (
value: Parameters<typeof JSON.stringify>[0],
replacer?:
| ReplacerOptions
| Parameters<typeof JSON.stringify>[1]
| Replacer,
space?: Parameters<typeof JSON.stringify>[2],
) =>
JSON.stringify(
value,
isPlainObject(replacer)
? replacerFromOptions(replacer as ReplacerOptions)
: // deno-lint-ignore no-explicit-any
(replacer as any),
space,
);