Skip to content
Myr

lines.js

This file

Path

lib/stdio/lines.js

77 lines · 1.8 KiB · JavaScript

Last changed by

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

History of this file

Take it

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 1–2why, one step back
1import {isUint8Array} from './utils.js';
2

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 3–12why, one step back
3// Split chunks line-wise
4export const getLinesGenerator = (encoding, binary) => {5 if (binary) {6 return;7 }89 return encoding === 'buffer' ? linesUint8ArrayGenerator : linesStringGenerator;10};1112const linesUint8ArrayGenerator = async function * (chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 13–19why, one step back
13 yield * linesGenerator({
14 chunks,15 emptyValue: new Uint8Array(0),16 newline: 0x0A,17 concat: concatUint8Array,18 isValidType: isUint8Array,19 });

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 20–29why, one step back
20};
2122const concatUint8Array = (firstChunk, secondChunk) => {23 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);24 chunk.set(firstChunk, 0);25 chunk.set(secondChunk, firstChunk.length);26 return chunk;27};2829const linesStringGenerator = async function * (chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 30–36why, one step back
30 yield * linesGenerator({
31 chunks,32 emptyValue: '',33 newline: '\n',34 concat: concatString,35 isValidType: isString,36 });

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 37–39why, one step back
37};
3839const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 40why, one step back
40const isString = chunk => typeof chunk === 'string';

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 41–43why, one step back
41
42// This imperative logic is much faster than using `String.split()` and uses very low memory.43// Also, it allows sharing it with `Uint8Array`.

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 44why, one step back
44const linesGenerator = async function * ({chunks, emptyValue, newline, concat, isValidType}) {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 45–47why, one step back
45 let previousChunks = emptyValue;
4647 for await (const chunk of chunks) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 48–52why, one step back
48 if (!isValidType(chunk)) {
49 yield chunk;50 continue;51 }52

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 53–77why, one step back
53 let start = -1;
5455 for (let end = 0; end < chunk.length; end += 1) {56 if (chunk[end] === newline) {57 let line = chunk.slice(start + 1, end + 1);5859 if (previousChunks.length > 0) {60 line = concat(previousChunks, line);61 previousChunks = emptyValue;62 }6364 yield line;65 start = end;66 }67 }6869 if (start !== chunk.length - 1) {70 previousChunks = concat(previousChunks, chunk.slice(start + 1));71 }72 }7374 if (previousChunks.length > 0) {75 yield previousChunks;76 }77};