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

Function stringifyArrayReplacer

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

Source from the content-addressed store, hash-verified

299 }
300
301 function stringifyArrayReplacer (key, value, stack, replacer, spacer, indentation) {
302 if (typeof value === 'object' && value !== null && typeof value.toJSON === 'function') {
303 value = value.toJSON(key)
304 }
305
306 switch (typeof value) {
307 case 'string':
308 return strEscape(value)
309 case 'object': {
310 if (value === null) {
311 return 'null'
312 }
313 if (stack.indexOf(value) !== -1) {
314 return circularValue
315 }
316
317 const originalIndentation = indentation
318 let res = ''
319 let join = ','
320
321 if (Array.isArray(value)) {
322 if (value.length === 0) {
323 return '[]'
324 }
325 if (maximumDepth < stack.length + 1) {
326 return '"[Array]"'
327 }
328 stack.push(value)
329 if (spacer !== '') {
330 indentation += spacer
331 res += `\n${indentation}`
332 join = `,\n${indentation}`
333 }
334 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
335 let i = 0
336 for (; i < maximumValuesToStringify - 1; i++) {
337 const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
338 res += tmp !== undefined ? tmp : 'null'
339 res += join
340 }
341 const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
342 res += tmp !== undefined ? tmp : 'null'
343 if (value.length - 1 > maximumBreadth) {
344 const removedKeys = value.length - maximumBreadth - 1
345 res += `${join}"... ${getItemCount(removedKeys)} not stringified"`
346 }
347 if (spacer !== '') {
348 res += `\n${originalIndentation}`
349 }
350 stack.pop()
351 return `[${res}]`
352 }
353 stack.push(value)
354 let whitespace = ''
355 if (spacer !== '') {
356 indentation += spacer
357 join = `,\n${indentation}`
358 whitespace = ' '

Callers 1

stringifyFunction · 0.85

Calls 2

strEscapeFunction · 0.85
getItemCountFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…