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

Function stringifyFnReplacer

index.js:289–383  ·  view source on GitHub ↗
(key, parent, stack, replacer, spacer, indentation)

Source from the content-addressed store, hash-verified

287 const failTypedArray = safe && fail ? makeSafe(fail) : fail
288
289 let stringifyFnReplacer = function (key, parent, stack, replacer, spacer, indentation) {
290 let value = parent[key]
291
292 if (typeof value === 'object' && value !== null && typeof value.toJSON === 'function') {
293 value = safe ? callSafe(value.toJSON, value, key) : value.toJSON(key)
294 }
295 value = replacer.call(parent, key, value)
296
297 if (typeof value === 'object') {
298 if (value === null) {
299 return 'null'
300 }
301 const circularIndex = stack.indexOf(value)
302 if (circularIndex !== -1) {
303 return typeof circularValue === 'function' ? circularValue(circularIndex) : circularValue
304 }
305
306 let res = ''
307 let join = ','
308 const originalIndentation = indentation
309
310 if (Array.isArray(value)) {
311 if (value.length === 0) {
312 return '[]'
313 }
314 if (maximumDepth < stack.length + 1) {
315 return '"[Array]"'
316 }
317 stack.push(value)
318 if (spacer !== '') {
319 indentation += spacer
320 res += `\n${indentation}`
321 join = `,\n${indentation}`
322 }
323 const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
324 let i = 0
325 for (; i < maximumValuesToStringify - 1; i++) {
326 const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation)
327 res += tmp !== undefined ? tmp : 'null'
328 res += join
329 }
330 const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation)
331 res += tmp !== undefined ? tmp : 'null'
332 if (value.length > maximumBreadth) {
333 const removedKeys = value.length - maximumBreadth
334 res += `${join}"... ${getItemCount(removedKeys)} not stringified"`
335 }
336 if (spacer !== '') {
337 res += `\n${originalIndentation}`
338 }
339 stack.pop()
340 return `[${res}]`
341 }
342
343 let keys = Object.keys(value)
344 const keyLength = keys.length
345 if (keyLength === 0) {
346 return '{}'

Callers 1

stringifyFunction · 0.85

Calls 5

callSafeFunction · 0.85
getItemCountFunction · 0.85
getTypedArrayTypeFunction · 0.85
sortFunction · 0.85
strEscapeFunction · 0.85

Tested by

no test coverage detected