split.js
This file
Path
lib/transform/split.js
126 lines · 4.1 KiB · JavaScript
Last changed by
Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 1–2why, one step back1import {getStringEncoding} from '../arguments/encoding-option.js';2100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 3–36why, one step back3// Split chunks line-wise for generators passed to the `std*` options4export const getSplitLinesGenerator = (binary, preserveNewlines, skipped, state) => binary || skipped5 ? undefined6 : initializeSplitLines(preserveNewlines, state);78// Same but for synchronous methods9export const splitLinesSync = (chunk, preserveNewlines, objectMode) => objectMode10 ? chunk.flatMap(item => splitLinesItemSync(item, preserveNewlines))11 : splitLinesItemSync(chunk, preserveNewlines);1213const splitLinesItemSync = (chunk, preserveNewlines) => {14 const {transform, final} = initializeSplitLines(preserveNewlines, {});15 return [...transform(chunk), ...final()];16};1718const initializeSplitLines = (preserveNewlines, state) => {19 state.previousChunks = '';20 return {21 transform: splitGenerator.bind(undefined, state, preserveNewlines),22 final: linesFinal.bind(undefined, state),23 };24};2526// This imperative logic is much faster than using `String.split()` and uses very low memory.27const splitGenerator = function * (state, preserveNewlines, chunk) {28 if (typeof chunk !== 'string') {29 yield chunk;30 return;31 }3233 let {previousChunks} = state;34 let start = -1;3536 for (let end = 0; end < chunk.length; end += 1) {Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 37–39why, one step back37 if (chunk[end] !== '\n') {38 continue;39 }100% test coverage (#1018)ehmickybefore adoption9f65b86
line 40why, one step back40Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 41–43why, one step back41 // The `\r` of a `\r\n` sequence is stripped further below, once `previousChunks` has been prepended.42 // Otherwise, a `\r\n` sequence split across two chunks would not be detected.43 let line = chunk.slice(start + 1, preserveNewlines ? end + 1 : end);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 44why, one step back44Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
lines 45–47why, one step back45 if (previousChunks.length > 0) {46 line = concatString(previousChunks, line);47 previousChunks = '';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 48why, one step back48 }Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 49why, one step back49Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 50why, one step back50 yield preserveNewlines ? line : stripCarriageReturn(line, state);Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 51why, one step back51 start = end;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–60why, one step back52 }5354 if (start !== chunk.length - 1) {55 previousChunks = concatString(previousChunks, chunk.slice(start + 1));56 }5758 state.previousChunks = previousChunks;59};60Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 61–63why, one step back61const stripCarriageReturn = (line, state) => {62 state.isWindowsNewline = line.endsWith('\r');63 return state.isWindowsNewline ? line.slice(0, -1) : line;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 64–73why, one step back64};6566const linesFinal = function * ({previousChunks}) {67 if (previousChunks.length > 0) {68 yield previousChunks;69 }70};7172// Unless `preserveNewlines: true` is used, we strip the newline of each line.73// This re-adds them after the user `transform` code has run.Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 74–75why, one step back74// The re-added newline must be encoded with the `encoding` option when the chunk is bytes: a UTF-8 newline would misalign `utf16le` bytes.75export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state, encoding}) => binary || preserveNewlines || readableObjectMode100% test coverage (#1018)ehmickybefore adoption9f65b86
line 76why, one step back76 ? undefinedFix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 77why, one step back77 : {transform: appendNewlineGenerator.bind(undefined, state, getStringEncoding(binary, encoding))};100% test coverage (#1018)ehmickybefore adoption9f65b86
line 78why, one step back78Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 79–82why, one step back79const appendNewlineGenerator = function * ({isWindowsNewline = false}, stringEncoding, chunk) {80 const {unixNewline, windowsNewline, concat} = typeof chunk === 'string'81 ? linesStringInfo82 : LINES_BYTES_INFO[stringEncoding];100% test coverage (#1018)ehmickybefore adoption9f65b86
line 83why, one step back83Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 84why, one step back84 if (endsWithNewline(chunk, unixNewline)) {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 85–89why, one step back85 yield chunk;86 return;87 }8889 const newline = isWindowsNewline ? windowsNewline : unixNewline;Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 90why, one step back90 yield concat(chunk, newline);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 91–92why, one step back91};92Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 93–97why, one step back93const endsWithNewline = (chunk, unixNewline) => typeof chunk === 'string'94 ? chunk.endsWith(unixNewline)95 : chunk.length >= unixNewline.length96 && chunk.subarray(-unixNewline.length).every((byte, index) => byte === unixNewline[index]);97100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 98–102why, one step back98const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;99100const linesStringInfo = {101 windowsNewline: '\r\n',102 unixNewline: '\n',Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 103why, one step back103 concat: concatString,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 104–112why, one step back104};105106const concatUint8Array = (firstChunk, secondChunk) => {107 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);108 chunk.set(firstChunk, 0);109 chunk.set(secondChunk, firstChunk.length);110 return chunk;111};112Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 113–125why, one step back113// Only indexed with text encodings, since the `binary` option and binary encodings disable this generator114const LINES_BYTES_INFO = {115 __proto__: null,116 utf8: {117 windowsNewline: new Uint8Array([0x0D, 0x0A]),118 unixNewline: new Uint8Array([0x0A]),119 concat: concatUint8Array,120 },121 utf16le: {122 windowsNewline: new Uint8Array([0x0D, 0x00, 0x0A, 0x00]),123 unixNewline: new Uint8Array([0x0A, 0x00]),124 concat: concatUint8Array,125 },100% test coverage (#1018)ehmickybefore adoption9f65b86
line 126why, one step back126};