UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.stringify = stringify;
7exports.parse = parse;
8exports.normalize = normalize;
9/* eslint-disable no-redeclare */
10var fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
11var UNDEFINED = 'undefined';
12function stringify(input) {
13 return input === undefined ? UNDEFINED : (0, fast_json_stable_stringify_1.default)(input);
14}
15// eslint-disable-next-line @typescript-eslint/no-explicit-any
16function parse(input) {
17 return input === UNDEFINED ? undefined : JSON.parse(input);
18}
19/**
20 * @internal
21 */
22function normalize(input, _a) {
23 var _b = _a === void 0 ? {} : _a, _c = _b.parse, parser = _c === void 0 ? parse : _c;
24 var result;
25 if (normalize.cache.has(input)) {
26 result = normalize.cache.get(input);
27 }
28 else {
29 var data = parser(input);
30 result = stringify(data);
31 if (result === input)
32 result = undefined;
33 normalize.cache.set(input, result);
34 }
35 return result === undefined ? input : result;
36}
37/**
38 * @internal
39 */
40// eslint-disable-next-line @typescript-eslint/no-namespace
41(function (normalize) {
42 normalize.cache = new Map();
43})(normalize || (exports.normalize = normalize = {}));