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

Function stringifySimple

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

Source from the content-addressed store, hash-verified

498 }
499
500 function stringifySimple (key, value, stack) {
501 switch (typeof value) {
502 case 'string':
503 return strEscape(value)
504 case 'object': {
505 if (value === null) {
506 return 'null'
507 }
508 if (typeof value.toJSON === 'function') {
509 value = value.toJSON(key)
510 // Prevent calling `toJSON` again
511 if (typeof value !== 'object') {
512 return stringifySimple(key, value, stack)
513 }
514 if (value === null) {
515 return 'null'
516 }
517 }
518 if (stack.indexOf(value) !== -1) {
519 return circularValue
520 }
521
522 let res = ''
523
524 const hasLength = value.length !== undefined
525 if (hasLength && Array.isArray(value)) {
526 if (value.length === 0) {
527 return '[]'
528 }
529 if (maximumDepth < stack.length + 1) {
530 return '"[Array]"'
531 }
532 stack.push(value)
533 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
534 let i = 0
535 for (; i < maximumValuesToStringify - 1; i++) {
536 const tmp = stringifySimple(String(i), value[i], stack)
537 res += tmp !== undefined ? tmp : 'null'
538 res += ','
539 }
540 const tmp = stringifySimple(String(i), value[i], stack)
541 res += tmp !== undefined ? tmp : 'null'
542 if (value.length - 1 > maximumBreadth) {
543 const removedKeys = value.length - maximumBreadth - 1
544 res += `,"... ${getItemCount(removedKeys)} not stringified"`
545 }
546 stack.pop()
547 return `[${res}]`
548 }
549
550 let keys = Object.keys(value)
551 const keyLength = keys.length
552 if (keyLength === 0) {
553 return '{}'
554 }
555 if (maximumDepth < stack.length + 1) {
556 return '"[Object]"'
557 }

Callers 1

stringifyFunction · 0.85

Calls 5

strEscapeFunction · 0.85
getItemCountFunction · 0.85
isTypedArrayWithEntriesFunction · 0.85
stringifyTypedArrayFunction · 0.85
sortFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…