MCPcopy
hub / github.com/serverless/serverless / joinTextTokens

Function joinTextTokens

packages/util/src/logger/index.js:430–459  ·  view source on GitHub ↗
(textTokens)

Source from the content-addressed store, hash-verified

428 * This function ensures that it can handle both array and non-array inputs gracefully.
429 */
430const joinTextTokens = (textTokens) => {
431 // Function to deeply flatten an array
432 const flattenDeep = (array) => {
433 // Make sure the input is actually an array; otherwise, wrap it in one
434 if (!Array.isArray(array)) return [array]
435
436 return array.reduce(
437 (acc, val) =>
438 Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val),
439 [],
440 )
441 }
442
443 // Make sure the initial input is treated as an array if it is not
444 const flattened = flattenDeep(
445 Array.isArray(textTokens) ? textTokens : [textTokens],
446 )
447
448 // Filter out non-string elements and join the remaining strings with newlines
449 const finalText = flattened
450 .filter((textToken) => typeof textToken === 'string')
451 .join('\n')
452
453 // If the final text does not end with a new line, add one for separation
454 if (!finalText.endsWith('\n')) {
455 return finalText + '\n'
456 }
457
458 return finalText
459}
460
461/**
462 * Handles progress state and write operations

Callers 1

writeTextFunction · 0.85

Calls 1

flattenDeepFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…