🤖 Merge PR #45634 [stream-json] - Add types missing from v1.5.0, Disassembler, Batch and Verifier by @jadengis

* add types missing from v1.5.0

* update version number in header

* add make array to Stringer
This commit is contained in:
John Dengis 2020-06-22 11:46:57 -04:00 committed by GitHub
parent 36c47d3564
commit 46bfa5ff65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 171 additions and 20 deletions

40
types/stream-json/Disassembler.d.ts vendored Normal file
View File

@ -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<string | number>;
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;
}
}

View File

@ -12,6 +12,7 @@ declare namespace Stringer {
useKeyValues?: boolean;
useStringValues?: boolean;
useNumberValues?: boolean;
makeArray?: boolean;
}
function make(options?: StringerOptions): Stringer;

View File

@ -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 <https://github.com/uhop>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -6,6 +6,7 @@
/// <reference types="node" />
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';

View File

@ -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

34
types/stream-json/utils/Batch.d.ts vendored Normal file
View File

@ -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;
}

27
types/stream-json/utils/Verifier.d.ts vendored Normal file
View File

@ -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;
}
}