swc/crates/swc_bundler/tests/.cache/untrusted/0c34b330f5a123d3464419f60d906110491b7b62.ts
2021-11-09 20:42:49 +09:00

38 lines
1.0 KiB
TypeScript

// Loaded from https://raw.githubusercontent.com/czabaj/stringify-replacers/main/replacers.ts
import { Replacer } from "./types.d.ts";
import { printType } from "./utils.ts";
export const identityReplacer: Replacer = (k, v) => v;
/**
* Prints deep objects only to certain depth
*/
export const maxDepthReplacer = (maxDepth: number): Replacer => {
if (maxDepth === 0) {
return identityReplacer;
}
if (maxDepth === 1) {
return (k, v) =>
k !== `` && v && typeof v === `object` ? printType.call(v) : v;
}
const depthTrack = new Map<Record<string, unknown>, number>();
return function (k, v) {
const currentDepth = depthTrack.get(this) || 0;
if (v && typeof v === `object`) {
if (currentDepth >= maxDepth) {
return printType.call(v);
}
depthTrack.set(v, currentDepth + 1);
}
return v;
};
};
/**
* Add support to serialize ES6 Map
*/
export const serializeMapReplacer: Replacer = (k, v) =>
v && v instanceof Map ? Object.fromEntries(v.entries()) : v;