From 46bfa5ff65684eefdca414134d957ffe8ae34e89 Mon Sep 17 00:00:00 2001 From: John Dengis Date: Mon, 22 Jun 2020 11:46:57 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#45634=20[stream-js?= =?UTF-8?q?on]=20-=20Add=20types=20missing=20from=20v1.5.0,=20Disassembler?= =?UTF-8?q?,=20Batch=20and=20Verifier=20by=20@jadengis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add types missing from v1.5.0 * update version number in header * add make array to Stringer --- types/stream-json/Disassembler.d.ts | 40 ++++++++++++ types/stream-json/Stringer.d.ts | 1 + types/stream-json/index.d.ts | 5 +- types/stream-json/stream-json-tests.ts | 84 ++++++++++++++++++++------ types/stream-json/utils/Batch.d.ts | 34 +++++++++++ types/stream-json/utils/Verifier.d.ts | 27 +++++++++ 6 files changed, 171 insertions(+), 20 deletions(-) create mode 100644 types/stream-json/Disassembler.d.ts create mode 100644 types/stream-json/utils/Batch.d.ts create mode 100644 types/stream-json/utils/Verifier.d.ts diff --git a/types/stream-json/Disassembler.d.ts b/types/stream-json/Disassembler.d.ts new file mode 100644 index 0000000000..e8c546e0fa --- /dev/null +++ b/types/stream-json/Disassembler.d.ts @@ -0,0 +1,40 @@ +import { Transform, TransformOptions } from 'stream'; + +export = Disassembler; + +declare class Disassembler extends Transform { + constructor(options?: Disassembler.DisassemblerOptions); +} + +declare namespace Disassembler { + type ReplacerFunction = (val1: any, val2: any) => any; + + type ReplaceArray = Array; + + interface DisassemblerOptions extends TransformOptions { + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + replacer?: ReplacerFunction | ReplaceArray; + } + + function make(options?: DisassemblerOptions): Disassembler; + + namespace make { + type Constructor = Disassembler; + const Constructor: typeof Disassembler; + } + + function disassembler(options?: DisassemblerOptions): Disassembler; + + namespace disassembler { + type Constructor = Disassembler; + const Constructor: typeof Disassembler; + } +} diff --git a/types/stream-json/Stringer.d.ts b/types/stream-json/Stringer.d.ts index 6ada77a37a..282a4cf37b 100644 --- a/types/stream-json/Stringer.d.ts +++ b/types/stream-json/Stringer.d.ts @@ -12,6 +12,7 @@ declare namespace Stringer { useKeyValues?: boolean; useStringValues?: boolean; useNumberValues?: boolean; + makeArray?: boolean; } function make(options?: StringerOptions): Stringer; diff --git a/types/stream-json/index.d.ts b/types/stream-json/index.d.ts index 9230408603..254483314b 100644 --- a/types/stream-json/index.d.ts +++ b/types/stream-json/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for stream-json 1.0 +// Type definitions for stream-json 1.5 // Project: http://github.com/uhop/stream-json // Definitions by: Eugene Lazutkin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,6 +6,7 @@ /// import * as Assembler from './Assembler'; +import * as Disassembler from './Disassembler'; import * as Emitter from './Emitter'; import * as Parser from './Parser'; import * as Stringer from './Stringer'; @@ -20,6 +21,8 @@ import * as StreamArray from './streamers/StreamArray'; import * as StreamObject from './streamers/StreamObject'; import * as StreamValues from './streamers/StreamValues'; +import * as Batch from "./utils/Batch"; +import * as Verifier from "./utils/Verifier"; import * as emit from './utils/emit'; import * as withParser from './utils/withParser'; diff --git a/types/stream-json/stream-json-tests.ts b/types/stream-json/stream-json-tests.ts index 721ef4e286..0af626aba6 100644 --- a/types/stream-json/stream-json-tests.ts +++ b/types/stream-json/stream-json-tests.ts @@ -3,6 +3,7 @@ import { Transform, TransformOptions } from 'stream'; import * as make from 'stream-json'; import * as Assembler from 'stream-json/Assembler'; +import * as Disassembler from 'stream-json/Disassembler'; import * as Emitter from 'stream-json/Emitter'; import * as Parser from 'stream-json/Parser'; import * as Stringer from 'stream-json/Stringer'; @@ -17,6 +18,8 @@ import * as StreamArray from 'stream-json/streamers/StreamArray'; import * as StreamObject from 'stream-json/streamers/StreamObject'; import * as StreamValues from 'stream-json/streamers/StreamValues'; +import * as Batch from 'stream-json/utils/Batch'; +import * as Verifier from 'stream-json/utils/Verifier'; import * as emit from 'stream-json/utils/emit'; import * as withParser from 'stream-json/utils/withParser'; @@ -50,7 +53,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); asm.dropToLevel(0); parser.on('keyValue', (value: string) => - console.log(value, asm.key, asm.stack.length, asm.done, asm.depth, asm.path) + console.log(value, asm.key, asm.stack.length, asm.done, asm.depth, asm.path), ); asm.on('done', (asm: Assembler) => console.log(JSON.stringify(asm.current))); } @@ -86,6 +89,24 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); used([p1, p2, p3, p4, p5]); } +{ + // Disassembler tests + + const d1: Disassembler = new Disassembler({ packValues: false }); + const d2: Disassembler = Disassembler.make({ + jsonStreaming: true, + replacer: (acc, next) => `${acc}${next}`, + }); + const d3: Disassembler = Disassembler.disassembler({ + streamValues: false, + replacer: ['foo', 'bar'], + }); + const d4: Disassembler.make.Constructor = Disassembler.make(); + const d5: Disassembler.disassembler.Constructor = Disassembler.disassembler(); + + used([d1, d2, d3, d4, d5]); +} + { // Stringer tests @@ -94,8 +115,9 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); const s3: Stringer = Stringer.stringer({ useKeyValues: true }); const s4: Stringer.make.Constructor = Stringer.make(); const s5: Stringer.stringer.Constructor = Stringer.stringer(); + const s6: Stringer = Stringer.stringer({ makeArray: true }); - used([s1, s2, s3, s4, s5]); + used([s1, s2, s3, s4, s5, s6]); } { @@ -142,17 +164,17 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); { name: 'numberChunk', value: '0' }, { name: 'endNumber' }, { name: 'numberValue', value: '0' }, - ] - }) + ], + }), ) .pipe(Replace.make({ filter: /\b_\w*\b/i, allowEmptyReplacement: true })) .pipe( Replace.replace({ filter: (stack: FilterBase.Stack, token: FilterBase.Token) => stack.length > 2, replacement: (stack: FilterBase.Stack, token: FilterBase.Token) => [ - { name: token.name === 'startArray' ? 'trueValue' : 'falseValue' } - ] - }) + { name: token.name === 'startArray' ? 'trueValue' : 'falseValue' }, + ], + }), ); Replace.withParser({ filter: '_meta' }); @@ -221,8 +243,8 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe( StreamArray.make({ @@ -232,8 +254,8 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe(StreamArray.streamArray()); @@ -261,8 +283,8 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe( StreamObject.make({ @@ -272,8 +294,8 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe(StreamObject.streamObject()); @@ -301,8 +323,8 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe( StreamValues.make({ @@ -312,14 +334,38 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; } - } - }) + }, + }), ); parser.pipe(StreamValues.streamValues()); StreamValues.withParser(); } +{ + // Batch tests + + const b1: Batch = new Batch(); + const b2: Batch = Batch.make({ batchSize: 1000 }); + const b3: Batch = Batch.batch({ batchSize: 100 }); + const b4: Batch.make.Constructor = Batch.make(); + const b5: Batch.batch.Constructor = Batch.batch(); + + used([b1, b2, b3, b4, b5]); +} + +{ + // Verifier tests + + const v1: Verifier = new Verifier(); + const v2: Verifier = Verifier.make({ jsonStreaming: true }); + const v3: Verifier = Verifier.parser({ jsonStreaming: false }); + const v4: Verifier.make.Constructor = Verifier.make(); + const v5: Verifier.parser.Constructor = Verifier.parser(); + + used([v1, v2, v3, v4, v5]); +} + { // emit() tests diff --git a/types/stream-json/utils/Batch.d.ts b/types/stream-json/utils/Batch.d.ts new file mode 100644 index 0000000000..ae001dd64e --- /dev/null +++ b/types/stream-json/utils/Batch.d.ts @@ -0,0 +1,34 @@ +import * as Chain from 'stream-chain'; +import { Transform, TransformOptions } from 'stream'; + +export = Batch; + +declare class Batch extends Transform { + constructor(options?: Batch.BatchOptions); +} + +declare namespace Batch { + interface BatchOptions extends TransformOptions { + /** + * The size of each batch. Defaults to 1000. Ignored + * if not a positive number. + */ + batchSize?: number; + } + + function make(options?: BatchOptions): Batch; + + namespace make { + type Constructor = Batch; + const Constructor: typeof Batch; + } + + function batch(options?: BatchOptions): Batch; + + namespace batch { + type Constructor = Batch; + const Constructor: typeof Batch; + } + + function withParser(options?: BatchOptions): Chain; +} diff --git a/types/stream-json/utils/Verifier.d.ts b/types/stream-json/utils/Verifier.d.ts new file mode 100644 index 0000000000..2117aeaaaf --- /dev/null +++ b/types/stream-json/utils/Verifier.d.ts @@ -0,0 +1,27 @@ +import { Writable, WritableOptions } from 'stream'; + +export = Verifier; + +declare class Verifier extends Writable { + constructor(options?: Verifier.VerifierOptions); +} + +declare namespace Verifier { + interface VerifierOptions extends WritableOptions { + jsonStreaming?: boolean; + } + + function make(options?: VerifierOptions): Verifier; + + namespace make { + type Constructor = Verifier; + const Constructor: typeof Verifier; + } + + function parser(options?: VerifierOptions): Verifier; + + namespace parser { + type Constructor = Verifier; + const Constructor: typeof Verifier; + } +}