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

Function stringifyObject

index.js:601–694  ·  view source on GitHub ↗
(key, value, stack)

Source from the content-addressed store, hash-verified

599 }
600
601 let stringifyObject = function (key, value, stack) {
602 if (value === null) {
603 return 'null'
604 }
605 if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
606 if (typeof key === 'number') key = String(key)
607 const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
608 // Prevent calling `toJSON` again
609 if (typeof jsonValue !== 'object') {
610 return stringifyPrimitive(jsonValue)
611 }
612 if (jsonValue === null) {
613 return 'null'
614 }
615 value = jsonValue
616 }
617 const circularIndex = stack.indexOf(value)
618 if (circularIndex !== -1) {
619 return typeof circularValue === 'function' ? circularValue(circularIndex) : circularValue
620 }
621
622 let res = ''
623
624 const hasLength = /** @type {{length?: unknown}} */ (value).length !== undefined
625 if (hasLength && Array.isArray(value)) {
626 if (value.length === 0) {
627 return '[]'
628 }
629 if (maximumDepth < stack.length + 1) {
630 return '"[Array]"'
631 }
632 stack.push(value)
633 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
634 let i = 0
635 for (; i < maximumValuesToStringify - 1; i++) {
636 const item = value[i]
637 const tmp = typeof item === 'object'
638 ? stringifyObject(i, item, stack)
639 : stringifyPrimitive(item)
640 res += tmp !== undefined ? tmp : 'null'
641 res += ','
642 }
643 const item = value[i]
644 const tmp = typeof item === 'object'
645 ? stringifyObject(i, item, stack)
646 : stringifyPrimitive(item)
647 res += tmp !== undefined ? tmp : 'null'
648 if (value.length > maximumBreadth) {
649 const removedKeys = value.length - maximumBreadth
650 res += `,"... ${getItemCount(removedKeys)} not stringified"`
651 }
652 stack.pop()
653 return `[${res}]`
654 }
655
656 let keys = Object.keys(value)
657 const keyLength = keys.length
658 if (keyLength === 0) {

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