Skip to content

Commit

Permalink
Added tests for JSONL + related fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Sep 16, 2024
1 parent 20b3123 commit ae7a23a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/jsonl/parser.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/// <reference types="node" />

import {Buffer} from 'node:buffer';

export = parser;

interface OutputItem {
key: number;
value: unknown;
value: any;
}

type Reviver = (this: unknown, key: string, value: unknown) => unknown;

declare function parser(reviver?: Reviver): AsyncIterableIterator<OutputItem>;
declare function parser(
reviver?: Reviver
): (x: string | Buffer) => AsyncGenerator<OutputItem, void, unknown>;
4 changes: 3 additions & 1 deletion src/jsonl/parserStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface ParserOptions extends DuplexOptions {
reviver?: (this: unknown, key: string, value: unknown) => unknown;
}

declare function parserStream<T>(options: ParserOptions): TypedDuplex<string | Uint8Array, T>;
declare function parserStream<T = any>(
options?: ParserOptions
): TypedDuplex<string | Uint8Array, T>;
2 changes: 1 addition & 1 deletion src/jsonl/stringerStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ interface StringerOptions {
space?: string | number;
}

declare function stringer<T>(options: any): TypedTransform<T, string>;
declare function stringer<T>(options?: any): TypedTransform<T, string>;
27 changes: 27 additions & 0 deletions ts-check/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chain, {type Arg0, type Ret, type ChainItem} from 'stream-chain';

import parser from 'stream-chain/jsonl/parser.js';
import parserStream from 'stream-chain/jsonl/parserStream.js';
import stringerStream from 'stream-chain/jsonl/stringerStream.js';

import fs, { createReadStream, createWriteStream } from 'node:fs';

const pipeline1 = chain([
createReadStream('input.json'),
parser(),
({value}: {value: number}) => value + 1,
stringerStream(),
createWriteStream('output.json')
] as const);

void pipeline1;

const pipeline2 = chain([
createReadStream('input.json'),
parserStream(),
({value}: {value: number}) => value + 1,
stringerStream(),
createWriteStream('output.json')
] as const);

void pipeline2;

0 comments on commit ae7a23a

Please sign in to comment.