Skip to content

Commit

Permalink
Added embedded docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Sep 17, 2024
1 parent d09f788 commit 4cad01c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/jsonl/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ import {Buffer} from 'node:buffer';

export = parser;

/**
* The JSONL parser output.
*/
interface OutputItem {
/** The key: a sequential number starting from 0. */
key: number;
/** The parsed value. */
value: any;
}

/**
* The reviver function prototype required by `JSON.parse()`.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
*/
type Reviver = (this: unknown, key: string, value: unknown) => unknown;

/**
* The JSONL parser as a streamable generator.
* @param reviver an optional reviver function (see {@link Reviver})
* @returns an asynchronous generator
* @remark parsers JSON lines items returning them as {@link OutputItem}.
*/
declare function parser(
reviver?: Reviver
): (x: string | Buffer) => AsyncGenerator<OutputItem, void, unknown>;
12 changes: 12 additions & 0 deletions src/jsonl/parserStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import {TypedDuplex} from '../typed-streams';

export = parserStream;

/**
* Options for the parser stream based on `DuplexOptions` with some additional properties.
*/
interface ParserOptions extends DuplexOptions {
/**
* An optional reviver function suitable for `JSON.parse()`.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
*/
reviver?: (this: unknown, key: string, value: unknown) => unknown;
}

/**
* Returns a JSONL parser as a duplex stream.
* @param options options for the parser stream (see {@link ParserOptions})
* @returns a duplex stream
*/
declare function parserStream<T = any>(
options?: ParserOptions
): TypedDuplex<string | Uint8Array, T>;
23 changes: 23 additions & 0 deletions src/jsonl/stringerStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@ import {TypedTransform} from '../typed-streams';

export = stringer;

/**
* Options for the stringer stream used to control the output.
*/
interface StringerOptions {
/** The prefix string. It is prepended to the output. Defaults to `""`. */
prefix?: string;
/** The suffix string. It is appended to the output. Defaults to `""`. */
suffix?: string;
/** The separator string used between items. Defaults to `"\n"`. */
separator?: string;
/**
* The empty value string. It is used when no values were streamed. Defaults to `prefix + suffix`.
* See {@link StringerOptions.prefix} and {@link StringerOptions.suffix}.
*/
emptyValue?: string;
/**
* The optional replacer function used by `JSON.stringify()`.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
*/
replacer?: (this: unknown, key: string, value: unknown) => unknown;
/**
* The optional space string or number used by `JSON.stringify()`.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
*/
space?: string | number;
}

/**
* Returns a JSONL stringer as a duplex stream.
* @param options options for the stringer stream (see {@link StringerOptions})
* @returns a duplex stream
*/
declare function stringer<T>(options?: any): TypedTransform<T, string>;

0 comments on commit 4cad01c

Please sign in to comment.