| 1 | import type * as nbformat from '@jupyterlab/nbformat'; |
| 2 | |
| 3 | export function concatMultilineString(str: nbformat.MultilineString, trim?: boolean): string { |
| 4 | const nonLineFeedWhiteSpaceTrim = /(^[\t\f\v\r ]+|[\t\f\v\r ]+$)/g; // Local var so don't have to reset the lastIndex. |
| 5 | if (Array.isArray(str)) { |
| 6 | let result = ''; |
| 7 | for (let i = 0; i < str.length; i += 1) { |
| 8 | const s = str[i]; |
| 9 | if (i < str.length - 1 && !s.endsWith('\n')) { |
| 10 | result = result.concat(`${s}\n`); |
| 11 | } else { |
| 12 | result = result.concat(s); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | // Just trim whitespace. Leave \n in place |
| 17 | return trim ? result.replace(nonLineFeedWhiteSpaceTrim, '') : result; |
| 18 | } |
| 19 | return trim ? str.toString().replace(nonLineFeedWhiteSpaceTrim, '') : str.toString(); |
| 20 | } |