split.js
This file
Path
lib/transform/split.js
110 lines · 3.2 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–5why, one step back1// Split chunks line-wise for generators passed to the `std*` options2export const getSplitLinesGenerator = (binary, preserveNewlines, skipped, state) => binary || skipped3 ? undefined4 : initializeSplitLines(preserveNewlines, state);5Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 6why, one step back6// Same but for synchronous methodsRename files (#977)ehmickybefore adoption4b2e316
lines 7–110why, one step back7export const splitLinesSync = (chunk, preserveNewlines, objectMode) => objectMode8 ? chunk.flatMap(item => splitLinesItemSync(item, preserveNewlines))9 : splitLinesItemSync(chunk, preserveNewlines);1011const splitLinesItemSync = (chunk, preserveNewlines) => {12 const {transform, final} = initializeSplitLines(preserveNewlines, {});13 return [...transform(chunk), ...final()];14};1516const initializeSplitLines = (preserveNewlines, state) => {17 state.previousChunks = '';18 return {19 transform: splitGenerator.bind(undefined, state, preserveNewlines),20 final: linesFinal.bind(undefined, state),21 };22};2324// This imperative logic is much faster than using `String.split()` and uses very low memory.25const splitGenerator = function * (state, preserveNewlines, chunk) {26 if (typeof chunk !== 'string') {27 yield chunk;28 return;29 }3031 let {previousChunks} = state;32 let start = -1;3334 for (let end = 0; end < chunk.length; end += 1) {35 if (chunk[end] === '\n') {36 const newlineLength = getNewlineLength(chunk, end, preserveNewlines, state);37 let line = chunk.slice(start + 1, end + 1 - newlineLength);3839 if (previousChunks.length > 0) {40 line = concatString(previousChunks, line);41 previousChunks = '';42 }4344 yield line;45 start = end;46 }47 }4849 if (start !== chunk.length - 1) {50 previousChunks = concatString(previousChunks, chunk.slice(start + 1));51 }5253 state.previousChunks = previousChunks;54};5556const getNewlineLength = (chunk, end, preserveNewlines, state) => {57 if (preserveNewlines) {58 return 0;59 }6061 state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';62 return state.isWindowsNewline ? 2 : 1;63};6465const linesFinal = function * ({previousChunks}) {66 if (previousChunks.length > 0) {67 yield previousChunks;68 }69};7071// Unless `preserveNewlines: true` is used, we strip the newline of each line.72// This re-adds them after the user `transform` code has run.73export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode74 ? undefined75 : {transform: appendNewlineGenerator.bind(undefined, state)};7677const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {78 const {unixNewline, windowsNewline, LF, concatBytes} = typeof chunk === 'string' ? linesStringInfo : linesUint8ArrayInfo;7980 if (chunk.at(-1) === LF) {81 yield chunk;82 return;83 }8485 const newline = isWindowsNewline ? windowsNewline : unixNewline;86 yield concatBytes(chunk, newline);87};8889const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;9091const linesStringInfo = {92 windowsNewline: '\r\n',93 unixNewline: '\n',94 LF: '\n',95 concatBytes: concatString,96};9798const concatUint8Array = (firstChunk, secondChunk) => {99 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);100 chunk.set(firstChunk, 0);101 chunk.set(secondChunk, firstChunk.length);102 return chunk;103};104105const linesUint8ArrayInfo = {106 windowsNewline: new Uint8Array([0x0D, 0x0A]),107 unixNewline: new Uint8Array([0x0A]),108 LF: 0x0A,109 concatBytes: concatUint8Array,110};