Skip to content
Myr

split.js

This file

Path

lib/stdio/split.js

105 lines · 3.0 KiB · JavaScript

Last changed by

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

History of this file

Take it

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 1why, one step back
1// Split chunks line-wise for generators passed to the `std*` options

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 2–4why, one step back
2export const getSplitLinesGenerator = (binary, preserveNewlines, writableObjectMode, state) => binary || writableObjectMode
3 ? undefined4 : initializeSplitLines(preserveNewlines, state);

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 5why, one step back
5

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 6–11why, one step back
6export const splitLinesSync = (string, preserveNewlines) => {
7 const {transform, final} = initializeSplitLines(preserveNewlines, {});8 return [...transform(string), ...final()];9};1011const initializeSplitLines = (preserveNewlines, state) => {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 12why, one step back
12 state.previousChunks = '';

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 13why, one step back
13 return {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 14why, one step back
14 transform: splitGenerator.bind(undefined, state, preserveNewlines),

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 15–19why, one step back
15 final: linesFinal.bind(undefined, state),
16 };17};1819// 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 20why, one step back
20const splitGenerator = function * (state, preserveNewlines, chunk) {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 21–25why, one step back
21 if (typeof chunk !== 'string') {
22 yield chunk;23 return;24 }25

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 26–29why, one step back
26 let {previousChunks} = state;
27 let start = -1;2829 for (let end = 0; end < chunk.length; end += 1) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 30–31why, one step back
30 if (chunk[end] === '\n') {
31 const newlineLength = getNewlineLength(chunk, end, preserveNewlines, state);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 32why, one step back
32 let line = chunk.slice(start + 1, end + 1 - newlineLength);

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 33–34why, one step back
33
34 if (previousChunks.length > 0) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 35–36why, one step back
35 line = concatString(previousChunks, line);
36 previousChunks = '';

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 37–44why, one step back
37 }
3839 yield line;40 start = end;41 }42 }4344 if (start !== chunk.length - 1) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 45why, one step back
45 previousChunks = concatString(previousChunks, chunk.slice(start + 1));

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 46–50why, one step back
46 }
4748 state.previousChunks = previousChunks;49};50

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 51why, one step back
51const getNewlineLength = (chunk, end, preserveNewlines, state) => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 52–55why, one step back
52 if (preserveNewlines) {
53 return 0;54 }55

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 56why, one step back
56 state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 57–59why, one step back
57 return state.isWindowsNewline ? 2 : 1;
58};59

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 60–63why, one step back
60const linesFinal = function * ({previousChunks}) {
61 if (previousChunks.length > 0) {62 yield previousChunks;63 }

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 64–72why, one step back
64};
6566// Unless `preserveNewlines: true` is used, we strip the newline of each line.67// This re-adds them after the user `transform` code has run.68export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode69 ? undefined70 : {transform: appendNewlineGenerator.bind(undefined, state)};7172const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 73why, one step back
73 const {unixNewline, windowsNewline, LF, concatBytes} = typeof chunk === 'string' ? linesStringInfo : linesUint8ArrayInfo;

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 74–81why, one step back
74
75 if (chunk.at(-1) === LF) {76 yield chunk;77 return;78 }7980 const newline = isWindowsNewline ? windowsNewline : unixNewline;81 yield concatBytes(chunk, newline);

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 82–104why, one step back
82};
8384const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;8586const linesStringInfo = {87 windowsNewline: '\r\n',88 unixNewline: '\n',89 LF: '\n',90 concatBytes: concatString,91};9293const concatUint8Array = (firstChunk, secondChunk) => {94 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);95 chunk.set(firstChunk, 0);96 chunk.set(secondChunk, firstChunk.length);97 return chunk;98};99100const linesUint8ArrayInfo = {101 windowsNewline: new Uint8Array([0x0D, 0x0A]),102 unixNewline: new Uint8Array([0x0A]),103 LF: 0x0A,104 concatBytes: concatUint8Array,

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 105why, one step back
105};