MCPcopy Create free account
hub / github.com/BridgeAR/safe-stable-stringify / stringifyIndent

Function stringifyIndent

index.js:484–579  ·  view source on GitHub ↗
(key, value, stack, spacer, indentation)

Source from the content-addressed store, hash-verified

482 * @param {string} indentation
483 */
484 let stringifyIndent = function (key, value, stack, spacer, indentation) {
485 if (value === null) {
486 return 'null'
487 }
488 if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
489 if (typeof key === 'number') key = String(key)
490 const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
491 if (typeof jsonValue !== 'object') {
492 return stringifyPrimitive(jsonValue)
493 }
494 if (jsonValue === null) {
495 return 'null'
496 }
497 value = jsonValue
498 }
499 const circularIndex = stack.indexOf(value)
500 if (circularIndex !== -1) {
501 return typeof circularValue === 'function' ? circularValue(circularIndex) : circularValue
502 }
503 const originalIndentation = indentation
504
505 if (Array.isArray(value)) {
506 if (value.length === 0) {
507 return '[]'
508 }
509 if (maximumDepth < stack.length + 1) {
510 return '"[Array]"'
511 }
512 stack.push(value)
513 indentation += spacer
514 let res = `\n${indentation}`
515 const join = `,\n${indentation}`
516 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
517 let i = 0
518 for (; i < maximumValuesToStringify - 1; i++) {
519 const item = value[i]
520 const tmp = typeof item === 'object' ? stringifyIndent(i, item, stack, spacer, indentation) : stringifyPrimitive(item)
521 res += tmp !== undefined ? tmp : 'null'
522 res += join
523 }
524 const item = value[i]
525 const tmp = typeof item === 'object' ? stringifyIndent(i, item, stack, spacer, indentation) : stringifyPrimitive(item)
526 res += tmp !== undefined ? tmp : 'null'
527 if (value.length > maximumBreadth) {
528 const removedKeys = value.length - maximumBreadth
529 res += `${join}"... ${getItemCount(removedKeys)} not stringified"`
530 }
531 res += `\n${originalIndentation}`
532 stack.pop()
533 return `[${res}]`
534 }
535
536 let keys = Object.keys(value)
537 const keyLength = keys.length
538 if (keyLength === 0) {
539 return '{}'
540 }
541 if (maximumDepth < stack.length + 1) {

Callers 1

stringifyFunction · 0.85

Calls 7

callSafeFunction · 0.85
stringifyPrimitiveFunction · 0.85
getItemCountFunction · 0.85
getTypedArrayTypeFunction · 0.85
stringifyTypedArrayFunction · 0.85
sortFunction · 0.85
strEscapeFunction · 0.85

Tested by

no test coverage detected