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

Function stringifyArrayReplacer

index.js:393–475  ·  view source on GitHub ↗
(key, value, stack, replacer, spacer, indentation)

Source from the content-addressed store, hash-verified

391 * @param {string} indentation
392 */
393 let stringifyArrayReplacer = function (key, value, stack, replacer, spacer, indentation) {
394 if (value === null) {
395 return 'null'
396 }
397 if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
398 if (typeof key === 'number') key = String(key)
399 const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
400 if (typeof jsonValue !== 'object') {
401 return stringifyPrimitive(jsonValue)
402 }
403 if (jsonValue === null) {
404 return 'null'
405 }
406 value = jsonValue
407 }
408 const circularIndex = stack.indexOf(value)
409 if (circularIndex !== -1) {
410 return typeof circularValue === 'function' ? circularValue(circularIndex) : circularValue
411 }
412
413 const originalIndentation = indentation
414 let res = ''
415 let join = ','
416
417 if (Array.isArray(value)) {
418 if (value.length === 0) {
419 return '[]'
420 }
421 if (maximumDepth < stack.length + 1) {
422 return '"[Array]"'
423 }
424 stack.push(value)
425 if (spacer !== '') {
426 indentation += spacer
427 res += `\n${indentation}`
428 join = `,\n${indentation}`
429 }
430 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
431 let i = 0
432 for (; i < maximumValuesToStringify - 1; i++) {
433 const item = value[i]
434 const tmp = typeof item === 'object' ? stringifyArrayReplacer(i, item, stack, replacer, spacer, indentation) : stringifyPrimitive(item)
435 res += tmp !== undefined ? tmp : 'null'
436 res += join
437 }
438 const item = value[i]
439 const tmp = typeof item === 'object' ? stringifyArrayReplacer(i, item, stack, replacer, spacer, indentation) : stringifyPrimitive(item)
440 res += tmp !== undefined ? tmp : 'null'
441 if (value.length > maximumBreadth) {
442 const removedKeys = value.length - maximumBreadth
443 res += `${join}"... ${getItemCount(removedKeys)} not stringified"`
444 }
445 if (spacer !== '') {
446 res += `\n${originalIndentation}`
447 }
448 stack.pop()
449 return `[${res}]`
450 }

Callers 1

stringifyFunction · 0.85

Calls 4

callSafeFunction · 0.85
stringifyPrimitiveFunction · 0.85
getItemCountFunction · 0.85
strEscapeFunction · 0.85

Tested by

no test coverage detected