Files
portfolio2023/node_modules/@poppinss/utils/build/src/safeStringify.js
T
2023-11-24 22:35:41 +01:00

40 lines
1.2 KiB
JavaScript

"use strict";
/*
* @poppinss/utils
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.safeStringify = void 0;
const fast_safe_stringify_1 = require("./fast-safe-stringify");
/**
* Replacer to handle bigints and remove Circular values all together
*/
function jsonStringifyReplacer(replacer, removeCircular) {
return function (key, value) {
if (removeCircular && value === '[Circular]') {
return;
}
const val = replacer ? replacer.call(this, key, value) : value;
if (typeof val === 'bigint') {
return val.toString();
}
return val;
};
}
/**
* Safely stringifies a Javascript native value
*/
function safeStringify(value, replacer, space) {
try {
return JSON.stringify(value, jsonStringifyReplacer(replacer, false), space);
}
catch {
return (0, fast_safe_stringify_1.stringify)(value, jsonStringifyReplacer(replacer, true), space);
}
}
exports.safeStringify = safeStringify;