split.js
This file
Path
lib/transform/split.js
112 lines · 3.2 KiB · JavaScript
Last changed by
Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
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–34why, 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) {Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 35–37why, one step back35 if (chunk[end] !== '\n') {36 continue;37 }Rename files (#977)ehmickybefore adoption4b2e316
line 38why, one step back38Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 39–40why, one step back39 const newlineLength = getNewlineLength(chunk, end, preserveNewlines, state);40 let line = chunk.slice(start + 1, end + 1 - newlineLength);Rename files (#977)ehmickybefore adoption4b2e316
line 41why, one step back41Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 42–44why, one step back42 if (previousChunks.length > 0) {43 line = concatString(previousChunks, line);44 previousChunks = '';Rename files (#977)ehmickybefore adoption4b2e316
line 45why, one step back45 }Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 46–48why, one step back4647 yield line;48 start = end;Rename files (#977)ehmickybefore adoption4b2e316
lines 49–112why, one step back49 }5051 if (start !== chunk.length - 1) {52 previousChunks = concatString(previousChunks, chunk.slice(start + 1));53 }5455 state.previousChunks = previousChunks;56};5758const getNewlineLength = (chunk, end, preserveNewlines, state) => {59 if (preserveNewlines) {60 return 0;61 }6263 state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';64 return state.isWindowsNewline ? 2 : 1;65};6667const linesFinal = function * ({previousChunks}) {68 if (previousChunks.length > 0) {69 yield previousChunks;70 }71};7273// Unless `preserveNewlines: true` is used, we strip the newline of each line.74// This re-adds them after the user `transform` code has run.75export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode76 ? undefined77 : {transform: appendNewlineGenerator.bind(undefined, state)};7879const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {80 const {unixNewline, windowsNewline, LF, concatBytes} = typeof chunk === 'string' ? linesStringInfo : linesUint8ArrayInfo;8182 if (chunk.at(-1) === LF) {83 yield chunk;84 return;85 }8687 const newline = isWindowsNewline ? windowsNewline : unixNewline;88 yield concatBytes(chunk, newline);89};9091const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;9293const linesStringInfo = {94 windowsNewline: '\r\n',95 unixNewline: '\n',96 LF: '\n',97 concatBytes: concatString,98};99100const concatUint8Array = (firstChunk, secondChunk) => {101 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);102 chunk.set(firstChunk, 0);103 chunk.set(secondChunk, firstChunk.length);104 return chunk;105};106107const linesUint8ArrayInfo = {108 windowsNewline: new Uint8Array([0x0D, 0x0A]),109 unixNewline: new Uint8Array([0x0A]),110 LF: 0x0A,111 concatBytes: concatUint8Array,112};