Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f092dec point at it.

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 1–2why, one step back
1import {getStringEncoding} from '../arguments/encoding-option.js';
2

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 3–36why, one step back
3// Split chunks line-wise for generators passed to the `std*` options
4export 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 back
37 if (chunk[end] !== '\n') {
38 continue;39 }

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 40why, one step back
40

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 41–43why, one step back
41 // 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);

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 44why, one step back
44

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

lines 45–47why, one step back
45 if (previousChunks.length > 0) {
46 line = concatString(previousChunks, line);47 previousChunks = '';

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 48why, one step back
48 }

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 49why, one step back
49

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 50why, one step back
50 yield preserveNewlines ? line : stripCarriageReturn(line, state);

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 51why, one step back
51 start = end;

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 52–60why, one step back
52 }
5354 if (start !== chunk.length - 1) {55 previousChunks = concatString(previousChunks, chunk.slice(start + 1));56 }5758 state.previousChunks = previousChunks;59};60

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 61–63why, one step back
61const stripCarriageReturn = (line, state) => {
62 state.isWindowsNewline = line.endsWith('\r');63 return state.isWindowsNewline ? line.slice(0, -1) : line;

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 64–73why, one step back
64};
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 back
74// 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 || readableObjectMode

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 76why, one step back
76 ? undefined

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 77why, one step back
77 : {transform: appendNewlineGenerator.bind(undefined, state, getStringEncoding(binary, encoding))};

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 78why, one step back
78

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 79–82why, one step back
79const appendNewlineGenerator = function * ({isWindowsNewline = false}, stringEncoding, chunk) {
80 const {unixNewline, windowsNewline, concat} = typeof chunk === 'string'81 ? linesStringInfo82 : LINES_BYTES_INFO[stringEncoding];

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 83why, one step back
83

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 84why, one step back
84 if (endsWithNewline(chunk, unixNewline)) {

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 85–89why, one step back
85 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 back
90 yield concat(chunk, newline);

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 91–92why, one step back
91};
92

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 93–97why, one step back
93const 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]);97

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 98–102why, one step back
98const 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 back
103 concat: concatString,

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 104–112why, one step back
104};
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};112

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 113–125why, one step back
113// Only indexed with text encodings, since the `binary` option and binary encodings disable this generator
114const 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 },

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 126why, one step back
126};