split.js
This file
Path
lib/stdio/split.js
109 lines · 3.2 KiB · JavaScript
Last changed by
Improve `verbose` option (#962)ehmickybefore adoption58beaa6
History of this fileTake it
Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 1why, one step back1// Split chunks line-wise for generators passed to the `std*` optionsImprove `encoding` option (#958)ehmickybefore adoption507b09c
line 2why, one step back2export const getSplitLinesGenerator = (binary, preserveNewlines, skipped, state) => binary || skippedFix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 3–4why, one step back3 ? undefined4 : initializeSplitLines(preserveNewlines, state);Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 5why, one step back5Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 6–10why, one step back6export const splitLinesSync = (chunk, preserveNewlines, objectMode) => objectMode7 ? chunk.flatMap(item => splitLinesItemSync(item, preserveNewlines))8 : splitLinesItemSync(chunk, preserveNewlines);910const splitLinesItemSync = (chunk, preserveNewlines) => {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 11why, one step back11 const {transform, final} = initializeSplitLines(preserveNewlines, {});Improve `verbose` option (#962)ehmickybefore adoption58beaa6
line 12why, one step back12 return [...transform(chunk), ...final()];Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 13–15why, one step back13};1415const initializeSplitLines = (preserveNewlines, state) => {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 16why, one step back16 state.previousChunks = '';Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 17why, one step back17 return {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 18why, one step back18 transform: splitGenerator.bind(undefined, state, preserveNewlines),Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 19–23why, one step back19 final: linesFinal.bind(undefined, state),20 };21};2223// This imperative logic is much faster than using `String.split()` and uses very low memory.Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 24why, one step back24const splitGenerator = function * (state, preserveNewlines, chunk) {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 25–29why, one step back25 if (typeof chunk !== 'string') {26 yield chunk;27 return;28 }29Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 30–33why, one step back30 let {previousChunks} = state;31 let start = -1;3233 for (let end = 0; end < chunk.length; end += 1) {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 34–35why, one step back34 if (chunk[end] === '\n') {35 const newlineLength = getNewlineLength(chunk, end, preserveNewlines, state);Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 36why, one step back36 let line = chunk.slice(start + 1, end + 1 - newlineLength);Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 37–38why, one step back3738 if (previousChunks.length > 0) {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 39–40why, one step back39 line = concatString(previousChunks, line);40 previousChunks = '';Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 41–48why, one step back41 }4243 yield line;44 start = end;45 }46 }4748 if (start !== chunk.length - 1) {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 49why, one step back49 previousChunks = concatString(previousChunks, chunk.slice(start + 1));Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 50–54why, one step back50 }5152 state.previousChunks = previousChunks;53};54Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 55why, one step back55const getNewlineLength = (chunk, end, preserveNewlines, state) => {Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 56–59why, one step back56 if (preserveNewlines) {57 return 0;58 }59Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 60why, one step back60 state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 61–63why, one step back61 return state.isWindowsNewline ? 2 : 1;62};63Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 64–67why, one step back64const linesFinal = function * ({previousChunks}) {65 if (previousChunks.length > 0) {66 yield previousChunks;67 }Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 68–76why, one step back68};6970// Unless `preserveNewlines: true` is used, we strip the newline of each line.71// This re-adds them after the user `transform` code has run.72export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode73 ? undefined74 : {transform: appendNewlineGenerator.bind(undefined, state)};7576const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 77why, one step back77 const {unixNewline, windowsNewline, LF, concatBytes} = typeof chunk === 'string' ? linesStringInfo : linesUint8ArrayInfo;Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 78–85why, one step back7879 if (chunk.at(-1) === LF) {80 yield chunk;81 return;82 }8384 const newline = isWindowsNewline ? windowsNewline : unixNewline;85 yield concatBytes(chunk, newline);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 86–108why, one step back86};8788const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;8990const linesStringInfo = {91 windowsNewline: '\r\n',92 unixNewline: '\n',93 LF: '\n',94 concatBytes: concatString,95};9697const concatUint8Array = (firstChunk, secondChunk) => {98 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);99 chunk.set(firstChunk, 0);100 chunk.set(secondChunk, firstChunk.length);101 return chunk;102};103104const linesUint8ArrayInfo = {105 windowsNewline: new Uint8Array([0x0D, 0x0A]),106 unixNewline: new Uint8Array([0x0A]),107 LF: 0x0A,108 concatBytes: concatUint8Array,Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 109why, one step back109};