Skip to content

Commit

Permalink
documentation; refactor of line splitting code
Browse files Browse the repository at this point in the history
  • Loading branch information
j50n committed Feb 8, 2022
1 parent 79180fd commit fc92146
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ process and get back a `Uint8Array`.
* @return The text compressed into bytes.
*/
async function gzip(text: string): Promise<Uint8Array> {
/* I am using a string for input and a Uint8Array (bytes) for output. */
const pr: Runner<string, Uint8Array> = runner(
stringInput(),
bytesOutput(),
)();

return await pr.run({ cmd: ["gzip", "-c"] }, text);
return await runner(stringInput(), bytesOutput())().run({
cmd: ["gzip", "-c"],
}, text);
}

console.dir(await gzip("Hello, world."));
Expand Down
19 changes: 16 additions & 3 deletions runners/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,28 @@ export async function* toLines(
): AsyncIterableIterator<string> {
const decoder = new TextDecoder();

for await (const line of toByteLines(buffs)) {
yield decoder.decode(line);
}
}

/**
* Convert an `AsyncIterable<Uint8Array>` into an `AsyncIterable<Uint8Array>`
* split on `lf` and suppressing trailing `cr`.
* @param buffs The iterable bytes.
*/
export async function* toByteLines(
buffs: AsyncIterable<Uint8Array>,
): AsyncIterableIterator<Uint8Array> {
let currentLine: Uint8Array[] = [];

const createLine = (): string => {
const createLine = (): Uint8Array => {
const line = concat(currentLine);
if (line.length > 0 && line[line.length - 1] === 0x0C) {
/* Strip the carriage return. */
return decoder.decode(line.slice(0, line.length - 1), {});
return line.slice(0, line.length - 1);
} else {
return decoder.decode(line);
return line;
}
};

Expand Down

0 comments on commit fc92146

Please sign in to comment.