123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 'use strict';
- const utils = require('./utils.cjs');
- const hasOwn = typeof Object.hasOwn === 'function'
- ? Object.hasOwn
- : (object, key) => Object.hasOwnProperty.call(object, key);
- const escapableCharCodeSubstitution = {
- 0x08: '\\b',
- 0x09: '\\t',
- 0x0a: '\\n',
- 0x0c: '\\f',
- 0x0d: '\\r',
- 0x22: '\\\"',
- 0x5c: '\\\\'
- };
- const charLength2048 = Uint8Array.from({ length: 2048 }, (_, code) => {
- if (hasOwn(escapableCharCodeSubstitution, code)) {
- return 2;
- }
- if (code < 0x20) {
- return 6;
- }
- return code < 128 ? 1 : 2;
- });
- function isLeadingSurrogate(code) {
- return code >= 0xD800 && code <= 0xDBFF;
- }
- function isTrailingSurrogate(code) {
- return code >= 0xDC00 && code <= 0xDFFF;
- }
- function stringLength(str) {
-
- if (!/[^\x20\x21\x23-\x5B\x5D-\x7F]/.test(str)) {
- return str.length + 2;
- }
- let len = 0;
- let prevLeadingSurrogate = false;
- for (let i = 0; i < str.length; i++) {
- const code = str.charCodeAt(i);
- if (code < 2048) {
- len += charLength2048[code];
- } else if (isLeadingSurrogate(code)) {
- len += 6;
- prevLeadingSurrogate = true;
- continue;
- } else if (isTrailingSurrogate(code)) {
- len = prevLeadingSurrogate
- ? len - 2
- : len + 6;
- } else {
- len += 3;
- }
- prevLeadingSurrogate = false;
- }
- return len + 2;
- }
- function intLength(num) {
- let len = 0;
- if (num < 0) {
- len = 1;
- num = -num;
- }
- if (num >= 1e9) {
- len += 9;
- num = (num - num % 1e9) / 1e9;
- }
- if (num >= 1e4) {
- if (num >= 1e6) {
- return len + (num >= 1e8
- ? 9
- : num >= 1e7 ? 8 : 7
- );
- }
- return len + (num >= 1e5 ? 6 : 5);
- }
- return len + (num >= 1e2
- ? num >= 1e3 ? 4 : 3
- : num >= 10 ? 2 : 1
- );
- }
- function primitiveLength(value) {
- switch (typeof value) {
- case 'string':
- return stringLength(value);
- case 'number':
- return Number.isFinite(value)
- ? Number.isInteger(value)
- ? intLength(value)
- : String(value).length
- : 4 ;
- case 'boolean':
- return value ? 4 : 5 ;
- case 'undefined':
- case 'object':
- return 4;
- default:
- return 0;
- }
- }
- function stringifyInfo(value, ...args) {
- const { replacer, getKeys, ...options } = utils.normalizeStringifyOptions(...args);
- const continueOnCircular = Boolean(options.continueOnCircular);
- const space = options.space?.length || 0;
- const keysLength = new Map();
- const visited = new Map();
- const circular = new Set();
- const stack = [];
- const root = { '': value };
- let stop = false;
- let bytes = 0;
- let spaceBytes = 0;
- let objects = 0;
- walk(root, '', value);
-
- if (bytes === 0) {
- bytes += 9;
- }
- return {
- bytes: isNaN(bytes) ? Infinity : bytes + spaceBytes,
- spaceBytes: space > 0 && isNaN(bytes) ? Infinity : spaceBytes,
- circular: [...circular]
- };
- function walk(holder, key, value) {
- if (stop) {
- return;
- }
- value = utils.replaceValue(holder, key, value, replacer);
- if (value === null || typeof value !== 'object') {
-
- if (value !== undefined || Array.isArray(holder)) {
- bytes += primitiveLength(value);
- }
- } else {
-
- if (stack.includes(value)) {
- circular.add(value);
- bytes += 4;
- if (!continueOnCircular) {
- stop = true;
- }
- return;
- }
-
-
-
- if (visited.has(value)) {
- bytes += visited.get(value);
- return;
- }
- objects++;
- const prevObjects = objects;
- const valueBytes = bytes;
- let valueLength = 0;
- stack.push(value);
- if (Array.isArray(value)) {
-
- valueLength = value.length;
- for (let i = 0; i < valueLength; i++) {
- walk(value, i, value[i]);
- }
- } else {
-
- let prevLength = bytes;
- for (const key of getKeys(value)) {
- walk(value, key, value[key]);
- if (prevLength !== bytes) {
- let keyLen = keysLength.get(key);
- if (keyLen === undefined) {
- keysLength.set(key, keyLen = stringLength(key) + 1);
- }
-
- bytes += keyLen;
- valueLength++;
- prevLength = bytes;
- }
- }
- }
- bytes += valueLength === 0
- ? 2
- : 1 + valueLength;
- if (space > 0 && valueLength > 0) {
- spaceBytes +=
-
- (Array.isArray(value) ? 0 : valueLength) +
-
-
-
-
-
- (1 + stack.length * space) * (valueLength + 1) - space;
- }
- stack.pop();
-
- if (prevObjects !== objects) {
- visited.set(value, bytes - valueBytes);
- }
- }
- }
- }
- exports.stringifyInfo = stringifyInfo;
|