@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
39 lines (38 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringify = exports.mkReplacer = void 0;
var obj_utils_1 = require("@harmoniclabs/obj-utils");
var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
function mkReplacer(replacer, map) {
if (map === void 0) { map = new WeakMap(); }
if (typeof replacer !== "function")
replacer = function (k, v) { return v; };
return function (key, value) {
value = replacer(key, value);
if ((0, obj_utils_1.isObject)(value)) {
if (map.has(value)) {
return { _circular_ref_: map.get(value) };
}
map.set(value, key);
if (typeof value.buffer === "object" && value.buffer instanceof ArrayBuffer) {
value = new Uint8Array(value.buffer);
}
if (value instanceof Uint8Array) {
value = (0, uint8array_utils_1.toHex)(value);
}
}
if (typeof value === "bigint") {
value = value.toString();
}
return value;
};
}
exports.mkReplacer = mkReplacer;
function stringify(value, replacer, space) {
if (space === void 0) { space = 0; }
if (replacer) {
return JSON.stringify(value, replacer, space);
}
return JSON.stringify(value, mkReplacer(replacer, new WeakMap()), space);
}
exports.stringify = stringify;