From e93ca5046d0c2de9227c1a16e3affe6680c5eb74 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 19:38:48 -0500 Subject: [PATCH 1/6] Added typings for stream-chain 2.0. --- types/stream-chain/index.d.ts | 32 +++++ types/stream-chain/stream-chain-tests.ts | 158 +++++++++++++++++++++++ types/stream-chain/tsconfig.json | 15 +++ types/stream-chain/tslint.json | 1 + 4 files changed, 206 insertions(+) create mode 100644 types/stream-chain/index.d.ts create mode 100644 types/stream-chain/stream-chain-tests.ts create mode 100644 types/stream-chain/tsconfig.json create mode 100644 types/stream-chain/tslint.json diff --git a/types/stream-chain/index.d.ts b/types/stream-chain/index.d.ts new file mode 100644 index 0000000000..a95e21892f --- /dev/null +++ b/types/stream-chain/index.d.ts @@ -0,0 +1,32 @@ +// Type definitions for stream-chain 2.0 +// Project: https://github.com/uhop/stream-chain#readme +// Definitions by: Eugene Lazutkin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +import { Readable, Writable, Duplex, Transform, DuplexOptions } from 'stream'; + +export = Chain; + +interface ITransformFunction { + (chunk: any, encoding?: string): any; +} + +declare type Stream = Readable | Writable | Duplex | Transform; +declare type StreamItem = Stream | ITransformFunction; + +declare class Chain extends Duplex { + constructor(fns: StreamItem[], options?: Chain.ChainOptions); + + input: Stream; + output: Stream; + streams: Stream[]; +} + +declare namespace Chain { + interface ChainOptions extends DuplexOptions { + skipEvents?: boolean; + } + + function chain(fns: StreamItem[], options?: ChainOptions): Chain; +} diff --git a/types/stream-chain/stream-chain-tests.ts b/types/stream-chain/stream-chain-tests.ts new file mode 100644 index 0000000000..8447179b49 --- /dev/null +++ b/types/stream-chain/stream-chain-tests.ts @@ -0,0 +1,158 @@ +import * as Chain from 'stream-chain'; +import { Readable, Writable, Transform } from 'stream'; + +const streamFromArray = (array: number[]): Readable => { + let index = 0; + return new Readable({ + objectMode: true, + read(): void { + this.push(index < array.length ? array[index++] : null); + } + }); +}; + +const streamToArray = (array: number[]): Writable => + new Writable({ + objectMode: true, + write(chunk, encoding, callback) { + array.push(chunk); + callback(null); + } + }); + +{ + // generic function + + const chain = new Chain([(x: number) => 2 * x]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // generator + + const chain = new Chain([ + function*(x: number): IterableIterator { + const n = +x; + yield 2 * n - 1; + return 2 * n; + } + ]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // async function + + const chain = new Chain([async (x: number) => Promise.resolve(x)]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // function that returns an array of values + + const chain = new Chain([(x: number) => [x, x + 1]]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // chain of functions + + const chain = new Chain([(x: number) => x * x, (x: number) => 2 * x + 1]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // Transform + a function + + const chain = new Chain([ + new Transform({ + objectMode: true, + transform(x: number, _, callback) { + callback(undefined, x * x); + } + }), + (x: number) => 2 * x + 1 + ]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // factory + + const chain = Chain.chain([(x: number) => 2 * x]); + const out1: number[] = []; + const out2: number[] = []; + + streamFromArray([1, 2, 3]) + .pipe(chain) + .pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // include Readable + + const chain = new Chain([streamFromArray([1, 2, 3]), (x: number) => 2 * x]); + const out1: number[] = []; + const out2: number[] = []; + + chain.pipe(streamToArray(out1)); + chain.on('data', value => out2.push(+value)); +} + +{ + // include Writable + + const out1: number[] = []; + const chain = new Chain([(x: number) => 2 * x], streamToArray(out1)); + const out2: number[] = []; + + streamFromArray([1, 2, 3]).pipe(chain); + chain.on('data', value => out2.push(+value)); +} + +{ + // include Readable and Writable + + const out1: number[] = []; + const chain = new Chain([streamFromArray([1, 2, 3]), (x: number) => 2 * x], streamToArray(out1)); + const out2: number[] = []; + + chain.on('data', value => out2.push(+value)); +} diff --git a/types/stream-chain/tsconfig.json b/types/stream-chain/tsconfig.json new file mode 100644 index 0000000000..cee2462320 --- /dev/null +++ b/types/stream-chain/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "stream-chain-tests.ts"] +} diff --git a/types/stream-chain/tslint.json b/types/stream-chain/tslint.json new file mode 100644 index 0000000000..4e88071852 --- /dev/null +++ b/types/stream-chain/tslint.json @@ -0,0 +1 @@ +{"extends": "dtslint/dt.json"} From 7cc9924be796f5f720453060ecd20a9a8828fc79 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 19:39:10 -0500 Subject: [PATCH 2/6] Added typings for stream-json 1.0. --- types/stream-json/index.d.ts | 480 +++++++++++++++++++++++++ types/stream-json/stream-json-tests.ts | 329 +++++++++++++++++ types/stream-json/tsconfig.json | 15 + types/stream-json/tslint.json | 1 + 4 files changed, 825 insertions(+) create mode 100644 types/stream-json/index.d.ts create mode 100644 types/stream-json/stream-json-tests.ts create mode 100644 types/stream-json/tsconfig.json create mode 100644 types/stream-json/tslint.json diff --git a/types/stream-json/index.d.ts b/types/stream-json/index.d.ts new file mode 100644 index 0000000000..910cb17c17 --- /dev/null +++ b/types/stream-json/index.d.ts @@ -0,0 +1,480 @@ +// Type definitions for stream-json 1.0 +// Project: http://github.com/uhop/stream-json +// Definitions by: Eugene Lazutkin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'stream-json' { + import * as Parser from 'stream-json/Parser'; + + export = make; + + function make(options?: Parser.ParserOptions): Parser; + + type ParserClass = Parser; + type ParserType = typeof Parser; + + namespace make { + type Parser = ParserClass; + const Parser: ParserType; + const parser: (options?: Parser.ParserOptions) => Parser; + } +} + +declare module 'stream-json/Assembler' { + import { EventEmitter } from 'events'; + import { Readable } from 'stream'; + + export = Assembler; + + type Token = { + name: string; + value?: string; + }; + + class Assembler extends EventEmitter { + constructor(); + + connectTo(stream: Readable): this; + consume(chunk: Token): this; + dropToLevel(level: number): this; + + current: any; + key: string | null; + stack: Array; + readonly done: boolean; + + readonly depth: number; + readonly path: string; + + // events + addListener(event: 'done', listener: (asm: Assembler) => void): this; + on(event: 'done', listener: (asm: Assembler) => void): this; + once(event: 'done', listener: (asm: Assembler) => void): this; + prependListener(event: 'done', listener: (asm: Assembler) => void): this; + prependOnceListener(event: 'done', listener: (asm: Assembler) => void): this; + removeListener(event: 'done', listener: (asm: Assembler) => void): this; + } + + namespace Assembler { + function connectTo(stream: Readable): Assembler; + } +} + +declare module 'stream-json/Emitter' { + import { Writable, WritableOptions } from 'stream'; + + export = Emitter; + + class Emitter extends Writable { + constructor(options?: WritableOptions); + } + + namespace Emitter { + function make(options?: WritableOptions): Emitter; + + namespace make { + type Constructor = Emitter; + const Constructor: typeof Emitter; + } + + function emitter(options?: WritableOptions): Emitter; + + namespace emitter { + type Constructor = Emitter; + const Constructor: typeof Emitter; + } + } +} + +declare module 'stream-json/Parser' { + import { Transform, TransformOptions } from 'stream'; + + export = Parser; + + class Parser extends Transform { + constructor(options?: Parser.ParserOptions); + } + + namespace Parser { + interface ParserOptions extends TransformOptions { + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } + + function make(options?: ParserOptions): Parser; + + namespace make { + type Constructor = Parser; + const Constructor: typeof Parser; + } + + function parser(options?: ParserOptions): Parser; + + namespace parser { + type Constructor = Parser; + const Constructor: typeof Parser; + } + } +} + +declare module 'stream-json/Stringer' { + import { Transform, TransformOptions } from 'stream'; + + export = Stringer; + + class Stringer extends Transform { + constructor(options?: Stringer.StringerOptions); + } + + namespace Stringer { + interface StringerOptions extends TransformOptions { + useValues?: boolean; + useKeyValues?: boolean; + useStringValues?: boolean; + useNumberValues?: boolean; + } + + function make(options?: StringerOptions): Stringer; + + namespace make { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + + function stringer(options?: StringerOptions): Stringer; + + namespace stringer { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + } +} + +declare module 'stream-json/filters/FilterBase' { + import { Transform, TransformOptions } from 'stream'; + + export = FilterBase; + + class FilterBase extends Transform { + constructor(options: FilterBase.FilterOptions); + } + + namespace FilterBase { + type Stack = ReadonlyArray; + type Token = { readonly name: string; readonly value?: ReadonlyArray }; + + interface IFilterFunction { + (stack: Stack, token: Token): boolean; + } + + interface IReplacementFunction { + (stack: Stack, token: Token): Token[]; + } + + interface FilterOptions extends TransformOptions { + filter: string | RegExp | IFilterFunction; + once?: boolean; + pathSeparator?: string; + streamValues?: boolean; + streamKeys?: boolean; + replacement?: ReadonlyArray | IReplacementFunction; + allowEmptyReplacement?: boolean; + } + } +} + +declare module 'stream-json/filters/Pick' { + import * as Chain from 'stream-chain'; + import * as FilterBase from 'stream-json/filters/FilterBase'; + + export = Pick; + + class Pick extends FilterBase { + constructor(options: FilterBase.FilterOptions); + } + + namespace Pick { + function make(options: FilterBase.FilterOptions): Pick; + + namespace make { + type Constructor = Pick; + const Constructor: typeof Pick; + } + + function pick(options: FilterBase.FilterOptions): Pick; + + namespace pick { + type Constructor = Pick; + const Constructor: typeof Pick; + } + + function withParser(options: FilterBase.FilterOptions): Chain; + } +} + +declare module 'stream-json/filters/Replace' { + import * as Chain from 'stream-chain'; + import * as FilterBase from 'stream-json/filters/FilterBase'; + + export = Replace; + + class Replace extends FilterBase { + constructor(options: FilterBase.FilterOptions); + } + + namespace Replace { + function make(options: FilterBase.FilterOptions): Replace; + + namespace make { + type Constructor = Replace; + const Constructor: typeof Replace; + } + + function replace(options: FilterBase.FilterOptions): Replace; + + namespace replace { + type Constructor = Replace; + const Constructor: typeof Replace; + } + + function withParser(options: FilterBase.FilterOptions): Chain; + } +} + +declare module 'stream-json/filters/Ignore' { + import * as Chain from 'stream-chain'; + import * as FilterBase from 'stream-json/filters/FilterBase'; + + export = Ignore; + + class Ignore extends FilterBase { + constructor(options: FilterBase.FilterOptions); + } + + namespace Ignore { + function make(options: FilterBase.FilterOptions): Ignore; + + namespace make { + type Constructor = Ignore; + const Constructor: typeof Ignore; + } + + function ignore(options: FilterBase.FilterOptions): Ignore; + + namespace ignore { + type Constructor = Ignore; + const Constructor: typeof Ignore; + } + + function withParser(options: FilterBase.FilterOptions): Chain; + } +} + +declare module 'stream-json/filters/Filter' { + import * as Chain from 'stream-chain'; + import * as FilterBase from 'stream-json/filters/FilterBase'; + + export = Filter; + + class Filter extends FilterBase { + constructor(options: FilterBase.FilterOptions); + } + + namespace Filter { + function make(options: FilterBase.FilterOptions): Filter; + + namespace make { + type Constructor = Filter; + const Constructor: typeof Filter; + } + + function filter(options: FilterBase.FilterOptions): Filter; + + namespace filter { + type Constructor = Filter; + const Constructor: typeof Filter; + } + + function withParser(options: FilterBase.FilterOptions): Chain; + } +} + +declare module 'stream-json/streamers/StreamBase' { + import { Transform, TransformOptions } from 'stream'; + import * as Assembler from 'stream-json/Assembler'; + + export = StreamBase; + + class StreamBase extends Transform { + constructor(options?: StreamBase.StreamOptions); + } + + namespace StreamBase { + interface IObjectFilterFunction { + (asm: Readonly): boolean | undefined; + } + + interface StreamOptions extends TransformOptions { + objectFilter?: IObjectFilterFunction; + includeUndecided?: boolean; + } + } +} + +declare module 'stream-json/streamers/StreamArray' { + import * as Chain from 'stream-chain'; + import * as StreamBase from 'stream-json/streamers/StreamBase'; + + export = StreamArray; + + class StreamArray extends StreamBase { + constructor(options?: StreamBase.StreamOptions); + } + + namespace StreamArray { + function make(options?: StreamBase.StreamOptions): StreamArray; + + namespace make { + type Constructor = StreamArray; + const Constructor: typeof StreamArray; + } + + function streamArray(options?: StreamBase.StreamOptions): StreamArray; + + namespace streamArray { + type Constructor = StreamArray; + const Constructor: typeof StreamArray; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; + } +} + +declare module 'stream-json/streamers/StreamObject' { + import * as Chain from 'stream-chain'; + import * as StreamBase from 'stream-json/streamers/StreamBase'; + + export = StreamObject; + + class StreamObject extends StreamBase { + constructor(options?: StreamBase.StreamOptions); + } + + namespace StreamObject { + function make(options?: StreamBase.StreamOptions): StreamObject; + + namespace make { + type Constructor = StreamObject; + const Constructor: typeof StreamObject; + } + + function streamObject(options?: StreamBase.StreamOptions): StreamObject; + + namespace streamObject { + type Constructor = StreamObject; + const Constructor: typeof StreamObject; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; + } +} + +declare module 'stream-json/streamers/StreamValues' { + import * as Chain from 'stream-chain'; + import * as StreamBase from 'stream-json/streamers/StreamBase'; + + export = StreamValues; + + class StreamValues extends StreamBase { + constructor(options?: StreamBase.StreamOptions); + } + + namespace StreamValues { + function make(options?: StreamBase.StreamOptions): StreamValues; + + namespace make { + type Constructor = StreamValues; + const Constructor: typeof StreamValues; + } + + function streamValues(options?: StreamBase.StreamOptions): StreamValues; + + namespace streamValues { + type Constructor = StreamValues; + const Constructor: typeof StreamValues; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; + } +} + +declare module 'stream-json/utils/emit' { + import { Writable, Duplex, Transform } from 'stream'; + + export = emit; + + function emit(stream: Stream): Stream; + + namespace emit { + + } +} + +declare module 'stream-json/utils/withParser' { + import { Writable, WritableOptions, Duplex, DuplexOptions, Transform, TransformOptions } from 'stream'; + import * as Chain from 'stream-chain'; + import * as Parser from 'stream-json/Parser'; + import * as FilterBase from 'stream-json/filters/FilterBase'; + import * as StreamBase from 'stream-json/streamers/StreamBase'; + + export = withParser; + + function withParser( + fn: (options: FilterBase.FilterOptions) => Stream, + options?: withParser.FilterOptions + ): Chain; + + function withParser( + fn: (options?: StreamBase.StreamOptions) => Stream, + options?: withParser.StreamOptions + ): Chain; + + function withParser(fn: (options?: TransformOptions) => Transform, options?: Parser.ParserOptions): Chain; + + namespace withParser { + interface FilterOptions extends FilterBase.FilterOptions { + // copied from ParserOptions + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } + + interface StreamOptions extends StreamBase.StreamOptions { + // copied from ParserOptions + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } + } +} diff --git a/types/stream-json/stream-json-tests.ts b/types/stream-json/stream-json-tests.ts new file mode 100644 index 0000000000..587e01d6ae --- /dev/null +++ b/types/stream-json/stream-json-tests.ts @@ -0,0 +1,329 @@ +import { Transform, TransformOptions } from 'stream'; + +import * as make from 'stream-json'; + +import * as Assembler from 'stream-json/Assembler'; +import * as Emitter from 'stream-json/Emitter'; +import * as Parser from 'stream-json/Parser'; +import * as Stringer from 'stream-json/Stringer'; + +import * as FilterBase from 'stream-json/filters/FilterBase'; +import * as Pick from 'stream-json/filters/Pick'; +import * as Replace from 'stream-json/filters/Replace'; +import * as Ignore from 'stream-json/filters/Ignore'; +import * as Filter from 'stream-json/filters/Filter'; + +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 emit from 'stream-json/utils/emit'; +import * as withParser from 'stream-json/utils/withParser'; + +const used = (array: any[]) => array.forEach(value => console.log(!!value)); + +{ + // creating parser with the main module + + const p1 = make(); + const p2 = new make.Parser(); + const p3 = make.parser(); + + const p4 = make({}); + const p5 = new make.Parser({}); + const p6 = make.parser({}); + + const p7: make.Parser = make(); + const p8: make.Parser = new make.Parser(); + const p9: make.Parser = make.parser(); + + used([p1, p2, p3, p4, p5, p6, p7, p8, p9]); +} + +{ + // Assembler tests + + const parser: Parser = new Parser({ streamValues: false }); + const asm: Assembler = Assembler.connectTo(parser); + + asm.consume({ name: 'startObject' }); + asm.dropToLevel(0); + + parser.on('keyValue', (value: string) => + console.log(value, asm.key, asm.stack.length, asm.done, asm.depth, asm.path) + ); + asm.on('done', asm => console.log(JSON.stringify(asm.current))); +} + +{ + // Emitter tests + + const parser: Parser = new Parser(); + const e1: Emitter = new Emitter(); + const e2: Emitter = Emitter.make({}); + const e3: Emitter = Emitter.emitter(); + const e4: Emitter.make.Constructor = Emitter.make({}); + const e5: Emitter.emitter.Constructor = Emitter.emitter({}); + + parser.pipe(e1); + parser.pipe(e2); + parser.pipe(e3); + parser.pipe(e4); + parser.pipe(e5); + + e1.on('startArray', () => console.log('array')); +} + +{ + // Parser tests + + const p1: Parser = new Parser({ packValues: false }); + const p2: Parser = Parser.make({ jsonStreaming: true }); + const p3: Parser = Parser.parser({ streamValues: false }); + const p4: Parser.make.Constructor = Parser.make({ packValues: false, packKeys: true }); + const p5: Parser.parser.Constructor = Parser.parser({ packValues: false, packKeys: true, streamKeys: false }); + + used([p1, p2, p3, p4, p5]); +} + +{ + // Stringer tests + + const s1: Stringer = new Stringer(); + const s2: Stringer = Stringer.make({ useValues: true }); + const s3: Stringer = Stringer.stringer({ useKeyValues: true }); + const s4: Stringer.make.Constructor = Stringer.make(); + const s5: Stringer.stringer.Constructor = Stringer.stringer(); + + used([s1, s2, s3, s4, s5]); +} + +{ + // Pick tests + + const parser: Parser = new Parser(); + + const f1: Pick = new Pick({ filter: 'data' }); + const f2: Pick = Pick.make({ filter: 'data' }); + const f3: Pick = Pick.pick({ filter: 'data' }); + const f4: Pick.make.Constructor = Pick.make({ filter: 'data' }); + const f5: Pick.pick.Constructor = Pick.pick({ filter: 'data' }); + + used([f1, f2, f3, f4, f5]); + + parser + .pipe(new Pick({ filter: 'data' })) + .pipe(Pick.make({ filter: /\bvalues\b/i })) + .pipe(Pick.pick({ filter: (stack: FilterBase.Stack, token: FilterBase.Token) => token.name === 'startArray' })); + + Pick.withParser({ filter: 'data' }); +} + +{ + // Replace tests + + const parser: Parser = new Parser(); + + const f1: Replace = new Replace({ filter: 'data' }); + const f2: Replace = Replace.make({ filter: 'data' }); + const f3: Replace = Replace.replace({ filter: 'data' }); + const f4: Replace.make.Constructor = Replace.make({ filter: 'data' }); + const f5: Replace.replace.Constructor = Replace.replace({ filter: 'data' }); + + used([f1, f2, f3, f4, f5]); + + parser + .pipe(new Replace({ filter: 'total', replacement: [{ name: 'trueValue' }] })) + .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' } + ] + }) + ); + + Replace.withParser({ filter: '_meta' }); +} + +{ + // Ignore tests + + const parser: Parser = new Parser({ streamValues: false }); + + const f1: Ignore = new Ignore({ filter: 'data' }); + const f2: Ignore = Ignore.make({ filter: 'data' }); + const f3: Ignore = Ignore.ignore({ filter: 'data' }); + const f4: Ignore.make.Constructor = Ignore.make({ filter: 'data' }); + const f5: Ignore.ignore.Constructor = Ignore.ignore({ filter: 'data' }); + + used([f1, f2, f3, f4, f5]); + + parser + .pipe(new Ignore({ filter: 'total' })) + .pipe(Ignore.make({ filter: /\b_\w*\b/i })) + .pipe(Ignore.ignore({ filter: (stack: FilterBase.Stack, token: FilterBase.Token) => stack.length > 2 })); + + Ignore.withParser({ filter: '_meta' }); +} + +{ + // Filter tests + + const parser: Parser = new Parser({ streamValues: false }); + + const f1: Filter = new Filter({ filter: 'data' }); + const f2: Filter = Filter.make({ filter: 'data' }); + const f3: Filter = Filter.filter({ filter: 'data' }); + const f4: Filter.make.Constructor = Filter.make({ filter: 'data' }); + const f5: Filter.filter.Constructor = Filter.filter({ filter: 'data' }); + + used([f1, f2, f3, f4, f5]); + + parser + .pipe(new Filter({ filter: 'total' })) + .pipe(Filter.make({ filter: /\b_\w*\b/i })) + .pipe(Filter.filter({ filter: (stack: FilterBase.Stack, token: FilterBase.Token) => stack.length > 2 })); + + Filter.withParser({ filter: '_meta' }); +} + +{ + // StreamArray tests + + const parser: Parser = new Parser(); + + const s1: StreamArray = new StreamArray(); + const s2: StreamArray = StreamArray.make(); + const s3: StreamArray = StreamArray.streamArray(); + const s4: StreamArray.make.Constructor = StreamArray.make(); + const s5: StreamArray.streamArray.Constructor = StreamArray.streamArray(); + + used([s1, s2, s3, s4, s5]); + + parser.pipe( + new StreamArray({ + includeUndecided: true, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe( + StreamArray.make({ + includeUndecided: false, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe(StreamArray.streamArray()); + + StreamArray.withParser(); +} + +{ + // StreamObject tests + + const parser: Parser = new Parser(); + + const s1: StreamObject = new StreamObject(); + const s2: StreamObject = StreamObject.make(); + const s3: StreamObject = StreamObject.streamObject(); + const s4: StreamObject.make.Constructor = StreamObject.make(); + const s5: StreamObject.streamObject.Constructor = StreamObject.streamObject(); + + used([s1, s2, s3, s4, s5]); + + parser.pipe( + new StreamObject({ + includeUndecided: true, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe( + StreamObject.make({ + includeUndecided: false, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe(StreamObject.streamObject()); + + StreamObject.withParser(); +} + +{ + // StreamValues tests + + const parser: Parser = new Parser(); + + const s1: StreamValues = new StreamValues(); + const s2: StreamValues = StreamValues.make(); + const s3: StreamValues = StreamValues.streamValues(); + const s4: StreamValues.make.Constructor = StreamValues.make(); + const s5: StreamValues.streamValues.Constructor = StreamValues.streamValues(); + + used([s1, s2, s3, s4, s5]); + + parser.pipe( + new StreamValues({ + includeUndecided: true, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe( + StreamValues.make({ + includeUndecided: false, + objectFilter: (asm: Readonly) => { + if (asm.current) { + if (asm.current.action === 'accept') return true; + if (asm.current.action === 'reject') return false; + } + } + }) + ); + parser.pipe(StreamValues.streamValues()); + + StreamValues.withParser(); +} + +{ + // emit() tests + + const parser: Parser = emit(new Parser()); + parser.on('keyValue', () => console.log('key')); +} + +{ + // withParser() tests + + withParser(Pick.make, { filter: 'data' }); + withParser(Pick.pick, { filter: 'data', packValues: false }); + + withParser(StreamArray.streamArray, { objectFilter: (asm: Assembler) => asm.current }); + withParser(StreamArray.make, { objectFilter: (asm: Assembler) => asm.current, packValues: false }); + + withParser((options?: TransformOptions) => new Transform(options), { streamValues: false }); +} diff --git a/types/stream-json/tsconfig.json b/types/stream-json/tsconfig.json new file mode 100644 index 0000000000..37ea09ac3f --- /dev/null +++ b/types/stream-json/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "stream-json-tests.ts"] +} diff --git a/types/stream-json/tslint.json b/types/stream-json/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/stream-json/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 9e07aea59828b48504c1e7ded7280f5aeb5eef57 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 19:39:32 -0500 Subject: [PATCH 3/6] Added typings for stream-csv-as-json 1.0. --- types/stream-csv-as-json/index.d.ts | 128 ++++++++++++++++++ .../stream-csv-as-json-tests.ts | 68 ++++++++++ types/stream-csv-as-json/tsconfig.json | 15 ++ types/stream-csv-as-json/tslint.json | 1 + 4 files changed, 212 insertions(+) create mode 100644 types/stream-csv-as-json/index.d.ts create mode 100644 types/stream-csv-as-json/stream-csv-as-json-tests.ts create mode 100644 types/stream-csv-as-json/tsconfig.json create mode 100644 types/stream-csv-as-json/tslint.json diff --git a/types/stream-csv-as-json/index.d.ts b/types/stream-csv-as-json/index.d.ts new file mode 100644 index 0000000000..8f9fb4d0ce --- /dev/null +++ b/types/stream-csv-as-json/index.d.ts @@ -0,0 +1,128 @@ +// Type definitions for stream-csv-as-json 1.0 +// Project: https://github.com/uhop/stream-csv-as-json#readme +// Definitions by: Eugene Lazutkin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'stream-csv-as-json' { + import * as Parser from 'stream-csv-as-json/Parser'; + + export = make; + + function make(options?: Parser.ParserOptions): Parser; + + type ParserClass = Parser; + type ParserType = typeof Parser; + + namespace make { + type Parser = ParserClass; + const Parser: ParserType; + const parser: (options?: Parser.ParserOptions) => Parser; + } +} + +declare module 'stream-csv-as-json/AsObjects' { + import { TransformOptions, Transform } from 'stream'; + import * as Chain from 'stream-chain'; + + export = AsObjects; + + class AsObjects extends Transform { + constructor(options?: AsObjects.AsObjectOptions); + } + + namespace AsObjects { + interface AsObjectOptions extends TransformOptions { + packValues?: boolean; + packStrings?: boolean; + streamValues?: boolean; + streamStrings?: boolean; + useValues?: boolean; + useStringValues?: boolean; + fieldPrefix?: string; + } + + function make(options?: AsObjects.AsObjectOptions): AsObjects; + + namespace make { + type Constructor = AsObjects; + const Constructor: typeof AsObjects; + } + + function asObjects(options?: AsObjects.AsObjectOptions): AsObjects; + + namespace asObjects { + type Constructor = AsObjects; + const Constructor: typeof AsObjects; + } + + function withParser(options?: AsObjects.AsObjectOptions): Chain; + } +} + +declare module 'stream-csv-as-json/Parser' { + import { Transform, TransformOptions } from 'stream'; + + export = Parser; + + class Parser extends Transform { + constructor(options?: Parser.ParserOptions); + } + + namespace Parser { + interface ParserOptions extends TransformOptions { + packValues?: boolean; + packStrings?: boolean; + streamValues?: boolean; + streamStrings?: boolean; + separator?: string; + } + + function make(options?: ParserOptions): Parser; + + namespace make { + type Constructor = Parser; + const Constructor: typeof Parser; + } + + function parser(options?: ParserOptions): Parser; + + namespace parser { + type Constructor = Parser; + const Constructor: typeof Parser; + } + } +} + +declare module 'stream-csv-as-json/Stringer' { + import { Transform, TransformOptions } from 'stream'; + + export = Stringer; + + class Stringer extends Transform { + constructor(options?: Stringer.StringerOptions); + } + + namespace Stringer { + interface StringerOptions extends TransformOptions { + useValues?: boolean; + useStringValues?: boolean; + separator?: string; + } + + function make(options?: StringerOptions): Stringer; + + namespace make { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + + function stringer(options?: StringerOptions): Stringer; + + namespace stringer { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + } +} diff --git a/types/stream-csv-as-json/stream-csv-as-json-tests.ts b/types/stream-csv-as-json/stream-csv-as-json-tests.ts new file mode 100644 index 0000000000..6007d19205 --- /dev/null +++ b/types/stream-csv-as-json/stream-csv-as-json-tests.ts @@ -0,0 +1,68 @@ +import * as make from 'stream-csv-as-json'; +import * as Parser from 'stream-csv-as-json/Parser'; +import * as Stringer from 'stream-csv-as-json/Stringer'; +import * as AsObjects from 'stream-csv-as-json/AsObjects'; + +const used = (array: any[]) => array.forEach(value => console.log(!!value)); + +{ + // creating parser with the main module + + const p1 = make(); + const p2 = new make.Parser(); + const p3 = make.parser(); + + const p4 = make({}); + const p5 = new make.Parser({}); + const p6 = make.parser({}); + + const p7: make.Parser = make(); + const p8: make.Parser = new make.Parser(); + const p9: make.Parser = make.parser(); + + used([p1, p2, p3, p4, p5, p6, p7, p8, p9]); +} + +{ + // Parser tests + + const p1: Parser = new Parser({ packValues: false }); + const p2: Parser = Parser.make({ separator: '/' }); + const p3: Parser = Parser.parser({ streamValues: false }); + const p4: Parser.make.Constructor = Parser.make({ packValues: false, packStrings: true }); + const p5: Parser.parser.Constructor = Parser.parser({ packValues: false, packStrings: true, streamStrings: false }); + + used([p1, p2, p3, p4, p5]); +} + +{ + // Stringer tests + + const s1: Stringer = new Stringer(); + const s2: Stringer = Stringer.make({ useValues: true }); + const s3: Stringer = Stringer.stringer({ useStringValues: true }); + const s4: Stringer.make.Constructor = Stringer.make(); + const s5: Stringer.stringer.Constructor = Stringer.stringer(); + + used([s1, s2, s3, s4, s5]); +} + +{ + // StreamValues tests + + const parser: Parser = new Parser(); + + const s1: AsObjects = new AsObjects(); + const s2: AsObjects = AsObjects.make(); + const s3: AsObjects = AsObjects.asObjects(); + const s4: AsObjects.make.Constructor = AsObjects.make(); + const s5: AsObjects.asObjects.Constructor = AsObjects.asObjects(); + + used([s1, s2, s3, s4, s5]); + + parser.pipe(new AsObjects({ fieldPrefix: 'x' })); + parser.pipe(AsObjects.make({ useStringValues: true })); + parser.pipe(AsObjects.asObjects()); + + AsObjects.withParser(); +} diff --git a/types/stream-csv-as-json/tsconfig.json b/types/stream-csv-as-json/tsconfig.json new file mode 100644 index 0000000000..d705f46cca --- /dev/null +++ b/types/stream-csv-as-json/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "stream-csv-as-json-tests.ts"] +} diff --git a/types/stream-csv-as-json/tslint.json b/types/stream-csv-as-json/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/stream-csv-as-json/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 35225abea46b89ab43efaf14d2588b3f0bdab87d Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 22:42:21 -0500 Subject: [PATCH 4/6] stream-chain: split modules, followed lint suggestions. --- types/stream-chain/index.d.ts | 8 +++----- types/stream-chain/stream-chain-tests.ts | 16 +++++++++------- types/stream-chain/tsconfig.json | 3 ++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/types/stream-chain/index.d.ts b/types/stream-chain/index.d.ts index a95e21892f..e893538c77 100644 --- a/types/stream-chain/index.d.ts +++ b/types/stream-chain/index.d.ts @@ -8,12 +8,10 @@ import { Readable, Writable, Duplex, Transform, DuplexOptions } from 'stream'; export = Chain; -interface ITransformFunction { - (chunk: any, encoding?: string): any; -} +type TransformFunction = (chunk: any, encoding?: string) => any; -declare type Stream = Readable | Writable | Duplex | Transform; -declare type StreamItem = Stream | ITransformFunction; +type Stream = Readable | Writable | Duplex | Transform; +type StreamItem = Stream | TransformFunction; declare class Chain extends Duplex { constructor(fns: StreamItem[], options?: Chain.ChainOptions); diff --git a/types/stream-chain/stream-chain-tests.ts b/types/stream-chain/stream-chain-tests.ts index 8447179b49..282b88c7da 100644 --- a/types/stream-chain/stream-chain-tests.ts +++ b/types/stream-chain/stream-chain-tests.ts @@ -37,11 +37,11 @@ const streamToArray = (array: number[]): Writable => // generator const chain = new Chain([ - function*(x: number): IterableIterator { - const n = +x; - yield 2 * n - 1; - return 2 * n; - } + // function*(x: number): IterableIterator { + // const n = +x; + // yield 2 * n - 1; + // return 2 * n; + // } ]); const out1: number[] = []; const out2: number[] = []; @@ -55,7 +55,9 @@ const streamToArray = (array: number[]): Writable => { // async function - const chain = new Chain([async (x: number) => Promise.resolve(x)]); + const chain = new Chain([ + // async (x: number) => Promise.resolve(x) + ]); const out1: number[] = []; const out2: number[] = []; @@ -97,7 +99,7 @@ const streamToArray = (array: number[]): Writable => const chain = new Chain([ new Transform({ objectMode: true, - transform(x: number, _, callback) { + transform(x: number, _: string | undefined, callback: (error: Error | undefined, chunk: any) => void) { callback(undefined, x * x); } }), diff --git a/types/stream-chain/tsconfig.json b/types/stream-chain/tsconfig.json index cee2462320..65c7438077 100644 --- a/types/stream-chain/tsconfig.json +++ b/types/stream-chain/tsconfig.json @@ -9,7 +9,8 @@ "typeRoots": ["../"], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true }, "files": ["index.d.ts", "stream-chain-tests.ts"] } From 583db23c985091070bef1e90faf8f697a5604b46 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 22:42:52 -0500 Subject: [PATCH 5/6] stream-json: split modules, followed lint suggestions. --- types/stream-json/Assembler.d.ts | 37 ++ types/stream-json/Emitter.d.ts | 23 + types/stream-json/Parser.d.ts | 35 ++ types/stream-json/Stringer.d.ts | 30 ++ types/stream-json/filters/Filter.d.ts | 26 + types/stream-json/filters/FilterBase.d.ts | 29 ++ types/stream-json/filters/Ignore.d.ts | 26 + types/stream-json/filters/Pick.d.ts | 26 + types/stream-json/filters/Replace.d.ts | 26 + types/stream-json/index.d.ts | 487 +----------------- types/stream-json/stream-json-tests.ts | 14 +- types/stream-json/streamers/StreamArray.d.ts | 26 + types/stream-json/streamers/StreamBase.d.ts | 17 + types/stream-json/streamers/StreamObject.d.ts | 26 + types/stream-json/streamers/StreamValues.d.ts | 26 + types/stream-json/tsconfig.json | 3 +- types/stream-json/utils/emit.d.ts | 7 + types/stream-json/utils/withParser.d.ts | 47 ++ 18 files changed, 438 insertions(+), 473 deletions(-) create mode 100644 types/stream-json/Assembler.d.ts create mode 100644 types/stream-json/Emitter.d.ts create mode 100644 types/stream-json/Parser.d.ts create mode 100644 types/stream-json/Stringer.d.ts create mode 100644 types/stream-json/filters/Filter.d.ts create mode 100644 types/stream-json/filters/FilterBase.d.ts create mode 100644 types/stream-json/filters/Ignore.d.ts create mode 100644 types/stream-json/filters/Pick.d.ts create mode 100644 types/stream-json/filters/Replace.d.ts create mode 100644 types/stream-json/streamers/StreamArray.d.ts create mode 100644 types/stream-json/streamers/StreamBase.d.ts create mode 100644 types/stream-json/streamers/StreamObject.d.ts create mode 100644 types/stream-json/streamers/StreamValues.d.ts create mode 100644 types/stream-json/utils/emit.d.ts create mode 100644 types/stream-json/utils/withParser.d.ts diff --git a/types/stream-json/Assembler.d.ts b/types/stream-json/Assembler.d.ts new file mode 100644 index 0000000000..52282d7beb --- /dev/null +++ b/types/stream-json/Assembler.d.ts @@ -0,0 +1,37 @@ +import { EventEmitter } from 'events'; +import { Readable } from 'stream'; + +export = Assembler; + +interface Token { + name: string; + value?: string; +} + +declare class Assembler extends EventEmitter { + constructor(); + + connectTo(stream: Readable): this; + consume(chunk: Token): this; + dropToLevel(level: number): this; + + current: any; + key: string | null; + stack: Array; + readonly done: boolean; + + readonly depth: number; + readonly path: string; + + // events + addListener(event: 'done', listener: (asm: Assembler) => void): this; + on(event: 'done', listener: (asm: Assembler) => void): this; + once(event: 'done', listener: (asm: Assembler) => void): this; + prependListener(event: 'done', listener: (asm: Assembler) => void): this; + prependOnceListener(event: 'done', listener: (asm: Assembler) => void): this; + removeListener(event: 'done', listener: (asm: Assembler) => void): this; +} + +declare namespace Assembler { + function connectTo(stream: Readable): Assembler; +} diff --git a/types/stream-json/Emitter.d.ts b/types/stream-json/Emitter.d.ts new file mode 100644 index 0000000000..7d0a3138c2 --- /dev/null +++ b/types/stream-json/Emitter.d.ts @@ -0,0 +1,23 @@ +import { Writable, WritableOptions } from 'stream'; + +export = Emitter; + +declare class Emitter extends Writable { + constructor(options?: WritableOptions); +} + +declare namespace Emitter { + function make(options?: WritableOptions): Emitter; + + namespace make { + type Constructor = Emitter; + const Constructor: typeof Emitter; + } + + function emitter(options?: WritableOptions): Emitter; + + namespace emitter { + type Constructor = Emitter; + const Constructor: typeof Emitter; + } +} diff --git a/types/stream-json/Parser.d.ts b/types/stream-json/Parser.d.ts new file mode 100644 index 0000000000..5fc689936e --- /dev/null +++ b/types/stream-json/Parser.d.ts @@ -0,0 +1,35 @@ +import { Transform, TransformOptions } from 'stream'; + +export = Parser; + +declare class Parser extends Transform { + constructor(options?: Parser.ParserOptions); +} + +declare namespace Parser { + interface ParserOptions extends TransformOptions { + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } + + function make(options?: ParserOptions): Parser; + + namespace make { + type Constructor = Parser; + const Constructor: typeof Parser; + } + + function parser(options?: ParserOptions): Parser; + + namespace parser { + type Constructor = Parser; + const Constructor: typeof Parser; + } +} diff --git a/types/stream-json/Stringer.d.ts b/types/stream-json/Stringer.d.ts new file mode 100644 index 0000000000..6ada77a37a --- /dev/null +++ b/types/stream-json/Stringer.d.ts @@ -0,0 +1,30 @@ +import { Transform, TransformOptions } from 'stream'; + +export = Stringer; + +declare class Stringer extends Transform { + constructor(options?: Stringer.StringerOptions); +} + +declare namespace Stringer { + interface StringerOptions extends TransformOptions { + useValues?: boolean; + useKeyValues?: boolean; + useStringValues?: boolean; + useNumberValues?: boolean; + } + + function make(options?: StringerOptions): Stringer; + + namespace make { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + + function stringer(options?: StringerOptions): Stringer; + + namespace stringer { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } +} diff --git a/types/stream-json/filters/Filter.d.ts b/types/stream-json/filters/Filter.d.ts new file mode 100644 index 0000000000..2a770925ca --- /dev/null +++ b/types/stream-json/filters/Filter.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as FilterBase from './FilterBase'; + +export = Filter; + +declare class Filter extends FilterBase { + constructor(options: FilterBase.FilterOptions); +} + +declare namespace Filter { + function make(options: FilterBase.FilterOptions): Filter; + + namespace make { + type Constructor = Filter; + const Constructor: typeof Filter; + } + + function filter(options: FilterBase.FilterOptions): Filter; + + namespace filter { + type Constructor = Filter; + const Constructor: typeof Filter; + } + + function withParser(options: FilterBase.FilterOptions): Chain; +} diff --git a/types/stream-json/filters/FilterBase.d.ts b/types/stream-json/filters/FilterBase.d.ts new file mode 100644 index 0000000000..43296561d4 --- /dev/null +++ b/types/stream-json/filters/FilterBase.d.ts @@ -0,0 +1,29 @@ +import { Transform, TransformOptions } from 'stream'; + +export = FilterBase; + +declare class FilterBase extends Transform { + constructor(options: FilterBase.FilterOptions); +} + +declare namespace FilterBase { + type Stack = ReadonlyArray; + + interface Token { + readonly name: string; + readonly value?: ReadonlyArray; + } + + type FilterFunction = (stack: Stack, token: Token) => boolean; + type ReplacementFunction = (stack: Stack, token: Token) => Token[]; + + interface FilterOptions extends TransformOptions { + filter: string | RegExp | FilterFunction; + once?: boolean; + pathSeparator?: string; + streamValues?: boolean; + streamKeys?: boolean; + replacement?: ReadonlyArray | ReplacementFunction; + allowEmptyReplacement?: boolean; + } +} diff --git a/types/stream-json/filters/Ignore.d.ts b/types/stream-json/filters/Ignore.d.ts new file mode 100644 index 0000000000..db805549d2 --- /dev/null +++ b/types/stream-json/filters/Ignore.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as FilterBase from './FilterBase'; + +export = Ignore; + +declare class Ignore extends FilterBase { + constructor(options: FilterBase.FilterOptions); +} + +declare namespace Ignore { + function make(options: FilterBase.FilterOptions): Ignore; + + namespace make { + type Constructor = Ignore; + const Constructor: typeof Ignore; + } + + function ignore(options: FilterBase.FilterOptions): Ignore; + + namespace ignore { + type Constructor = Ignore; + const Constructor: typeof Ignore; + } + + function withParser(options: FilterBase.FilterOptions): Chain; +} diff --git a/types/stream-json/filters/Pick.d.ts b/types/stream-json/filters/Pick.d.ts new file mode 100644 index 0000000000..f4504f20f5 --- /dev/null +++ b/types/stream-json/filters/Pick.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as FilterBase from './FilterBase'; + +export = Pick; + +declare class Pick extends FilterBase { + constructor(options: FilterBase.FilterOptions); +} + +declare namespace Pick { + function make(options: FilterBase.FilterOptions): Pick; + + namespace make { + type Constructor = Pick; + const Constructor: typeof Pick; + } + + function pick(options: FilterBase.FilterOptions): Pick; + + namespace pick { + type Constructor = Pick; + const Constructor: typeof Pick; + } + + function withParser(options: FilterBase.FilterOptions): Chain; +} diff --git a/types/stream-json/filters/Replace.d.ts b/types/stream-json/filters/Replace.d.ts new file mode 100644 index 0000000000..9f85b847f5 --- /dev/null +++ b/types/stream-json/filters/Replace.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as FilterBase from './FilterBase'; + +export = Replace; + +declare class Replace extends FilterBase { + constructor(options: FilterBase.FilterOptions); +} + +declare namespace Replace { + function make(options: FilterBase.FilterOptions): Replace; + + namespace make { + type Constructor = Replace; + const Constructor: typeof Replace; + } + + function replace(options: FilterBase.FilterOptions): Replace; + + namespace replace { + type Constructor = Replace; + const Constructor: typeof Replace; + } + + function withParser(options: FilterBase.FilterOptions): Chain; +} diff --git a/types/stream-json/index.d.ts b/types/stream-json/index.d.ts index 910cb17c17..9230408603 100644 --- a/types/stream-json/index.d.ts +++ b/types/stream-json/index.d.ts @@ -5,476 +5,33 @@ /// -declare module 'stream-json' { - import * as Parser from 'stream-json/Parser'; +import * as Assembler from './Assembler'; +import * as Emitter from './Emitter'; +import * as Parser from './Parser'; +import * as Stringer from './Stringer'; - export = make; +import * as FilterBase from './filters/FilterBase'; +import * as Pick from './filters/Pick'; +import * as Replace from './filters/Replace'; +import * as Ignore from './filters/Ignore'; +import * as Filter from './filters/Filter'; - function make(options?: Parser.ParserOptions): Parser; +import * as StreamArray from './streamers/StreamArray'; +import * as StreamObject from './streamers/StreamObject'; +import * as StreamValues from './streamers/StreamValues'; - type ParserClass = Parser; - type ParserType = typeof Parser; +import * as emit from './utils/emit'; +import * as withParser from './utils/withParser'; - namespace make { - type Parser = ParserClass; - const Parser: ParserType; - const parser: (options?: Parser.ParserOptions) => Parser; - } -} +export = make; -declare module 'stream-json/Assembler' { - import { EventEmitter } from 'events'; - import { Readable } from 'stream'; +declare function make(options?: Parser.ParserOptions): Parser; - export = Assembler; +type ParserClass = Parser; +type ParserType = typeof Parser; - type Token = { - name: string; - value?: string; - }; - - class Assembler extends EventEmitter { - constructor(); - - connectTo(stream: Readable): this; - consume(chunk: Token): this; - dropToLevel(level: number): this; - - current: any; - key: string | null; - stack: Array; - readonly done: boolean; - - readonly depth: number; - readonly path: string; - - // events - addListener(event: 'done', listener: (asm: Assembler) => void): this; - on(event: 'done', listener: (asm: Assembler) => void): this; - once(event: 'done', listener: (asm: Assembler) => void): this; - prependListener(event: 'done', listener: (asm: Assembler) => void): this; - prependOnceListener(event: 'done', listener: (asm: Assembler) => void): this; - removeListener(event: 'done', listener: (asm: Assembler) => void): this; - } - - namespace Assembler { - function connectTo(stream: Readable): Assembler; - } -} - -declare module 'stream-json/Emitter' { - import { Writable, WritableOptions } from 'stream'; - - export = Emitter; - - class Emitter extends Writable { - constructor(options?: WritableOptions); - } - - namespace Emitter { - function make(options?: WritableOptions): Emitter; - - namespace make { - type Constructor = Emitter; - const Constructor: typeof Emitter; - } - - function emitter(options?: WritableOptions): Emitter; - - namespace emitter { - type Constructor = Emitter; - const Constructor: typeof Emitter; - } - } -} - -declare module 'stream-json/Parser' { - import { Transform, TransformOptions } from 'stream'; - - export = Parser; - - class Parser extends Transform { - constructor(options?: Parser.ParserOptions); - } - - namespace Parser { - interface ParserOptions extends TransformOptions { - packValues?: boolean; - packKeys?: boolean; - packStrings?: boolean; - packNumbers?: boolean; - streamValues?: boolean; - streamKeys?: boolean; - streamStrings?: boolean; - streamNumbers?: boolean; - jsonStreaming?: boolean; - } - - function make(options?: ParserOptions): Parser; - - namespace make { - type Constructor = Parser; - const Constructor: typeof Parser; - } - - function parser(options?: ParserOptions): Parser; - - namespace parser { - type Constructor = Parser; - const Constructor: typeof Parser; - } - } -} - -declare module 'stream-json/Stringer' { - import { Transform, TransformOptions } from 'stream'; - - export = Stringer; - - class Stringer extends Transform { - constructor(options?: Stringer.StringerOptions); - } - - namespace Stringer { - interface StringerOptions extends TransformOptions { - useValues?: boolean; - useKeyValues?: boolean; - useStringValues?: boolean; - useNumberValues?: boolean; - } - - function make(options?: StringerOptions): Stringer; - - namespace make { - type Constructor = Stringer; - const Constructor: typeof Stringer; - } - - function stringer(options?: StringerOptions): Stringer; - - namespace stringer { - type Constructor = Stringer; - const Constructor: typeof Stringer; - } - } -} - -declare module 'stream-json/filters/FilterBase' { - import { Transform, TransformOptions } from 'stream'; - - export = FilterBase; - - class FilterBase extends Transform { - constructor(options: FilterBase.FilterOptions); - } - - namespace FilterBase { - type Stack = ReadonlyArray; - type Token = { readonly name: string; readonly value?: ReadonlyArray }; - - interface IFilterFunction { - (stack: Stack, token: Token): boolean; - } - - interface IReplacementFunction { - (stack: Stack, token: Token): Token[]; - } - - interface FilterOptions extends TransformOptions { - filter: string | RegExp | IFilterFunction; - once?: boolean; - pathSeparator?: string; - streamValues?: boolean; - streamKeys?: boolean; - replacement?: ReadonlyArray | IReplacementFunction; - allowEmptyReplacement?: boolean; - } - } -} - -declare module 'stream-json/filters/Pick' { - import * as Chain from 'stream-chain'; - import * as FilterBase from 'stream-json/filters/FilterBase'; - - export = Pick; - - class Pick extends FilterBase { - constructor(options: FilterBase.FilterOptions); - } - - namespace Pick { - function make(options: FilterBase.FilterOptions): Pick; - - namespace make { - type Constructor = Pick; - const Constructor: typeof Pick; - } - - function pick(options: FilterBase.FilterOptions): Pick; - - namespace pick { - type Constructor = Pick; - const Constructor: typeof Pick; - } - - function withParser(options: FilterBase.FilterOptions): Chain; - } -} - -declare module 'stream-json/filters/Replace' { - import * as Chain from 'stream-chain'; - import * as FilterBase from 'stream-json/filters/FilterBase'; - - export = Replace; - - class Replace extends FilterBase { - constructor(options: FilterBase.FilterOptions); - } - - namespace Replace { - function make(options: FilterBase.FilterOptions): Replace; - - namespace make { - type Constructor = Replace; - const Constructor: typeof Replace; - } - - function replace(options: FilterBase.FilterOptions): Replace; - - namespace replace { - type Constructor = Replace; - const Constructor: typeof Replace; - } - - function withParser(options: FilterBase.FilterOptions): Chain; - } -} - -declare module 'stream-json/filters/Ignore' { - import * as Chain from 'stream-chain'; - import * as FilterBase from 'stream-json/filters/FilterBase'; - - export = Ignore; - - class Ignore extends FilterBase { - constructor(options: FilterBase.FilterOptions); - } - - namespace Ignore { - function make(options: FilterBase.FilterOptions): Ignore; - - namespace make { - type Constructor = Ignore; - const Constructor: typeof Ignore; - } - - function ignore(options: FilterBase.FilterOptions): Ignore; - - namespace ignore { - type Constructor = Ignore; - const Constructor: typeof Ignore; - } - - function withParser(options: FilterBase.FilterOptions): Chain; - } -} - -declare module 'stream-json/filters/Filter' { - import * as Chain from 'stream-chain'; - import * as FilterBase from 'stream-json/filters/FilterBase'; - - export = Filter; - - class Filter extends FilterBase { - constructor(options: FilterBase.FilterOptions); - } - - namespace Filter { - function make(options: FilterBase.FilterOptions): Filter; - - namespace make { - type Constructor = Filter; - const Constructor: typeof Filter; - } - - function filter(options: FilterBase.FilterOptions): Filter; - - namespace filter { - type Constructor = Filter; - const Constructor: typeof Filter; - } - - function withParser(options: FilterBase.FilterOptions): Chain; - } -} - -declare module 'stream-json/streamers/StreamBase' { - import { Transform, TransformOptions } from 'stream'; - import * as Assembler from 'stream-json/Assembler'; - - export = StreamBase; - - class StreamBase extends Transform { - constructor(options?: StreamBase.StreamOptions); - } - - namespace StreamBase { - interface IObjectFilterFunction { - (asm: Readonly): boolean | undefined; - } - - interface StreamOptions extends TransformOptions { - objectFilter?: IObjectFilterFunction; - includeUndecided?: boolean; - } - } -} - -declare module 'stream-json/streamers/StreamArray' { - import * as Chain from 'stream-chain'; - import * as StreamBase from 'stream-json/streamers/StreamBase'; - - export = StreamArray; - - class StreamArray extends StreamBase { - constructor(options?: StreamBase.StreamOptions); - } - - namespace StreamArray { - function make(options?: StreamBase.StreamOptions): StreamArray; - - namespace make { - type Constructor = StreamArray; - const Constructor: typeof StreamArray; - } - - function streamArray(options?: StreamBase.StreamOptions): StreamArray; - - namespace streamArray { - type Constructor = StreamArray; - const Constructor: typeof StreamArray; - } - - function withParser(options?: StreamBase.StreamOptions): Chain; - } -} - -declare module 'stream-json/streamers/StreamObject' { - import * as Chain from 'stream-chain'; - import * as StreamBase from 'stream-json/streamers/StreamBase'; - - export = StreamObject; - - class StreamObject extends StreamBase { - constructor(options?: StreamBase.StreamOptions); - } - - namespace StreamObject { - function make(options?: StreamBase.StreamOptions): StreamObject; - - namespace make { - type Constructor = StreamObject; - const Constructor: typeof StreamObject; - } - - function streamObject(options?: StreamBase.StreamOptions): StreamObject; - - namespace streamObject { - type Constructor = StreamObject; - const Constructor: typeof StreamObject; - } - - function withParser(options?: StreamBase.StreamOptions): Chain; - } -} - -declare module 'stream-json/streamers/StreamValues' { - import * as Chain from 'stream-chain'; - import * as StreamBase from 'stream-json/streamers/StreamBase'; - - export = StreamValues; - - class StreamValues extends StreamBase { - constructor(options?: StreamBase.StreamOptions); - } - - namespace StreamValues { - function make(options?: StreamBase.StreamOptions): StreamValues; - - namespace make { - type Constructor = StreamValues; - const Constructor: typeof StreamValues; - } - - function streamValues(options?: StreamBase.StreamOptions): StreamValues; - - namespace streamValues { - type Constructor = StreamValues; - const Constructor: typeof StreamValues; - } - - function withParser(options?: StreamBase.StreamOptions): Chain; - } -} - -declare module 'stream-json/utils/emit' { - import { Writable, Duplex, Transform } from 'stream'; - - export = emit; - - function emit(stream: Stream): Stream; - - namespace emit { - - } -} - -declare module 'stream-json/utils/withParser' { - import { Writable, WritableOptions, Duplex, DuplexOptions, Transform, TransformOptions } from 'stream'; - import * as Chain from 'stream-chain'; - import * as Parser from 'stream-json/Parser'; - import * as FilterBase from 'stream-json/filters/FilterBase'; - import * as StreamBase from 'stream-json/streamers/StreamBase'; - - export = withParser; - - function withParser( - fn: (options: FilterBase.FilterOptions) => Stream, - options?: withParser.FilterOptions - ): Chain; - - function withParser( - fn: (options?: StreamBase.StreamOptions) => Stream, - options?: withParser.StreamOptions - ): Chain; - - function withParser(fn: (options?: TransformOptions) => Transform, options?: Parser.ParserOptions): Chain; - - namespace withParser { - interface FilterOptions extends FilterBase.FilterOptions { - // copied from ParserOptions - packValues?: boolean; - packKeys?: boolean; - packStrings?: boolean; - packNumbers?: boolean; - streamValues?: boolean; - streamKeys?: boolean; - streamStrings?: boolean; - streamNumbers?: boolean; - jsonStreaming?: boolean; - } - - interface StreamOptions extends StreamBase.StreamOptions { - // copied from ParserOptions - packValues?: boolean; - packKeys?: boolean; - packStrings?: boolean; - packNumbers?: boolean; - streamValues?: boolean; - streamKeys?: boolean; - streamStrings?: boolean; - streamNumbers?: boolean; - jsonStreaming?: boolean; - } - } +declare namespace make { + type Parser = ParserClass; + const Parser: ParserType; + function parser(options?: Parser.ParserOptions): Parser; } diff --git a/types/stream-json/stream-json-tests.ts b/types/stream-json/stream-json-tests.ts index 587e01d6ae..76e21071f1 100644 --- a/types/stream-json/stream-json-tests.ts +++ b/types/stream-json/stream-json-tests.ts @@ -52,7 +52,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.on('keyValue', (value: string) => console.log(value, asm.key, asm.stack.length, asm.done, asm.depth, asm.path) ); - asm.on('done', asm => console.log(JSON.stringify(asm.current))); + asm.on('done', (asm: Assembler) => console.log(JSON.stringify(asm.current))); } { @@ -205,7 +205,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( new StreamArray({ includeUndecided: true, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; @@ -216,7 +216,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( StreamArray.make({ includeUndecided: false, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; @@ -245,7 +245,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( new StreamObject({ includeUndecided: true, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; @@ -256,7 +256,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( StreamObject.make({ includeUndecided: false, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; @@ -285,7 +285,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( new StreamValues({ includeUndecided: true, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; @@ -296,7 +296,7 @@ const used = (array: any[]) => array.forEach(value => console.log(!!value)); parser.pipe( StreamValues.make({ includeUndecided: false, - objectFilter: (asm: Readonly) => { + objectFilter: (asm: Assembler) => { if (asm.current) { if (asm.current.action === 'accept') return true; if (asm.current.action === 'reject') return false; diff --git a/types/stream-json/streamers/StreamArray.d.ts b/types/stream-json/streamers/StreamArray.d.ts new file mode 100644 index 0000000000..4961e95bf8 --- /dev/null +++ b/types/stream-json/streamers/StreamArray.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as StreamBase from './StreamBase'; + +export = StreamArray; + +declare class StreamArray extends StreamBase { + constructor(options?: StreamBase.StreamOptions); +} + +declare namespace StreamArray { + function make(options?: StreamBase.StreamOptions): StreamArray; + + namespace make { + type Constructor = StreamArray; + const Constructor: typeof StreamArray; + } + + function streamArray(options?: StreamBase.StreamOptions): StreamArray; + + namespace streamArray { + type Constructor = StreamArray; + const Constructor: typeof StreamArray; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; +} diff --git a/types/stream-json/streamers/StreamBase.d.ts b/types/stream-json/streamers/StreamBase.d.ts new file mode 100644 index 0000000000..641694efc4 --- /dev/null +++ b/types/stream-json/streamers/StreamBase.d.ts @@ -0,0 +1,17 @@ +import { Transform, TransformOptions } from 'stream'; +import * as Assembler from '../Assembler'; + +export = StreamBase; + +declare class StreamBase extends Transform { + constructor(options?: StreamBase.StreamOptions); +} + +declare namespace StreamBase { + type ObjectFilterFunction = (asm: Assembler) => boolean | undefined; + + interface StreamOptions extends TransformOptions { + objectFilter?: ObjectFilterFunction; + includeUndecided?: boolean; + } +} diff --git a/types/stream-json/streamers/StreamObject.d.ts b/types/stream-json/streamers/StreamObject.d.ts new file mode 100644 index 0000000000..efaeed75ad --- /dev/null +++ b/types/stream-json/streamers/StreamObject.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as StreamBase from './StreamBase'; + +export = StreamObject; + +declare class StreamObject extends StreamBase { + constructor(options?: StreamBase.StreamOptions); +} + +declare namespace StreamObject { + function make(options?: StreamBase.StreamOptions): StreamObject; + + namespace make { + type Constructor = StreamObject; + const Constructor: typeof StreamObject; + } + + function streamObject(options?: StreamBase.StreamOptions): StreamObject; + + namespace streamObject { + type Constructor = StreamObject; + const Constructor: typeof StreamObject; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; +} diff --git a/types/stream-json/streamers/StreamValues.d.ts b/types/stream-json/streamers/StreamValues.d.ts new file mode 100644 index 0000000000..2ffe1a0719 --- /dev/null +++ b/types/stream-json/streamers/StreamValues.d.ts @@ -0,0 +1,26 @@ +import * as Chain from 'stream-chain'; +import * as StreamBase from './StreamBase'; + +export = StreamValues; + +declare class StreamValues extends StreamBase { + constructor(options?: StreamBase.StreamOptions); +} + +declare namespace StreamValues { + function make(options?: StreamBase.StreamOptions): StreamValues; + + namespace make { + type Constructor = StreamValues; + const Constructor: typeof StreamValues; + } + + function streamValues(options?: StreamBase.StreamOptions): StreamValues; + + namespace streamValues { + type Constructor = StreamValues; + const Constructor: typeof StreamValues; + } + + function withParser(options?: StreamBase.StreamOptions): Chain; +} diff --git a/types/stream-json/tsconfig.json b/types/stream-json/tsconfig.json index 37ea09ac3f..7bc52c45ec 100644 --- a/types/stream-json/tsconfig.json +++ b/types/stream-json/tsconfig.json @@ -9,7 +9,8 @@ "typeRoots": ["../"], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true }, "files": ["index.d.ts", "stream-json-tests.ts"] } diff --git a/types/stream-json/utils/emit.d.ts b/types/stream-json/utils/emit.d.ts new file mode 100644 index 0000000000..82b32f153c --- /dev/null +++ b/types/stream-json/utils/emit.d.ts @@ -0,0 +1,7 @@ +import { Writable, Duplex, Transform } from 'stream'; + +export = emit; + +declare function emit(stream: Stream): Stream; + +declare namespace emit {} diff --git a/types/stream-json/utils/withParser.d.ts b/types/stream-json/utils/withParser.d.ts new file mode 100644 index 0000000000..ef509366df --- /dev/null +++ b/types/stream-json/utils/withParser.d.ts @@ -0,0 +1,47 @@ +import { Writable, WritableOptions, Duplex, DuplexOptions, Transform, TransformOptions } from 'stream'; +import * as Chain from 'stream-chain'; +import * as Parser from '../Parser'; +import * as FilterBase from '../filters/FilterBase'; +import * as StreamBase from '../streamers/StreamBase'; + +export = withParser; + +declare function withParser( + fn: (options: FilterBase.FilterOptions) => FilterBase, + options?: withParser.FilterOptions +): Chain; + +declare function withParser( + fn: (options?: StreamBase.StreamOptions) => StreamBase, + options?: withParser.StreamOptions +): Chain; + +declare function withParser(fn: (options?: TransformOptions) => Transform, options?: Parser.ParserOptions): Chain; + +declare namespace withParser { + interface FilterOptions extends FilterBase.FilterOptions { + // copied from ParserOptions + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } + + interface StreamOptions extends StreamBase.StreamOptions { + // copied from ParserOptions + packValues?: boolean; + packKeys?: boolean; + packStrings?: boolean; + packNumbers?: boolean; + streamValues?: boolean; + streamKeys?: boolean; + streamStrings?: boolean; + streamNumbers?: boolean; + jsonStreaming?: boolean; + } +} From acc527ca1baa1e6041fda62fe2aa6acb40c9e8ca Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Mon, 25 Jun 2018 22:43:09 -0500 Subject: [PATCH 6/6] stream-csv-as-json: split modules, followed lint suggestions. --- types/stream-csv-as-json/AsObjects.d.ts | 38 +++++++ types/stream-csv-as-json/Parser.d.ts | 33 ++++++ types/stream-csv-as-json/Stringer.d.ts | 31 ++++++ types/stream-csv-as-json/index.d.ts | 129 ++---------------------- types/stream-csv-as-json/tsconfig.json | 3 +- 5 files changed, 115 insertions(+), 119 deletions(-) create mode 100644 types/stream-csv-as-json/AsObjects.d.ts create mode 100644 types/stream-csv-as-json/Parser.d.ts create mode 100644 types/stream-csv-as-json/Stringer.d.ts diff --git a/types/stream-csv-as-json/AsObjects.d.ts b/types/stream-csv-as-json/AsObjects.d.ts new file mode 100644 index 0000000000..8c0ab31ab6 --- /dev/null +++ b/types/stream-csv-as-json/AsObjects.d.ts @@ -0,0 +1,38 @@ +/// + +import { TransformOptions, Transform } from 'stream'; +import * as Chain from 'stream-chain'; + +export = AsObjects; + +declare class AsObjects extends Transform { + constructor(options?: AsObjects.AsObjectOptions); +} + +declare namespace AsObjects { + interface AsObjectOptions extends TransformOptions { + packValues?: boolean; + packStrings?: boolean; + streamValues?: boolean; + streamStrings?: boolean; + useValues?: boolean; + useStringValues?: boolean; + fieldPrefix?: string; + } + + function make(options?: AsObjectOptions): AsObjects; + + namespace make { + type Constructor = AsObjects; + const Constructor: typeof AsObjects; + } + + function asObjects(options?: AsObjectOptions): AsObjects; + + namespace asObjects { + type Constructor = AsObjects; + const Constructor: typeof AsObjects; + } + + function withParser(options?: AsObjectOptions): Chain; +} diff --git a/types/stream-csv-as-json/Parser.d.ts b/types/stream-csv-as-json/Parser.d.ts new file mode 100644 index 0000000000..1274765edd --- /dev/null +++ b/types/stream-csv-as-json/Parser.d.ts @@ -0,0 +1,33 @@ +/// + +import { Transform, TransformOptions } from 'stream'; + +export = Parser; + +declare class Parser extends Transform { + constructor(options?: Parser.ParserOptions); +} + +declare namespace Parser { + interface ParserOptions extends TransformOptions { + packValues?: boolean; + packStrings?: boolean; + streamValues?: boolean; + streamStrings?: boolean; + separator?: string; + } + + function make(options?: ParserOptions): Parser; + + namespace make { + type Constructor = Parser; + const Constructor: typeof Parser; + } + + function parser(options?: ParserOptions): Parser; + + namespace parser { + type Constructor = Parser; + const Constructor: typeof Parser; + } +} diff --git a/types/stream-csv-as-json/Stringer.d.ts b/types/stream-csv-as-json/Stringer.d.ts new file mode 100644 index 0000000000..52629f96b9 --- /dev/null +++ b/types/stream-csv-as-json/Stringer.d.ts @@ -0,0 +1,31 @@ +/// + +import { Transform, TransformOptions } from 'stream'; + +export = Stringer; + +declare class Stringer extends Transform { + constructor(options?: Stringer.StringerOptions); +} + +declare namespace Stringer { + interface StringerOptions extends TransformOptions { + useValues?: boolean; + useStringValues?: boolean; + separator?: string; + } + + function make(options?: StringerOptions): Stringer; + + namespace make { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } + + function stringer(options?: StringerOptions): Stringer; + + namespace stringer { + type Constructor = Stringer; + const Constructor: typeof Stringer; + } +} diff --git a/types/stream-csv-as-json/index.d.ts b/types/stream-csv-as-json/index.d.ts index 8f9fb4d0ce..1dca341577 100644 --- a/types/stream-csv-as-json/index.d.ts +++ b/types/stream-csv-as-json/index.d.ts @@ -3,126 +3,19 @@ // Definitions by: Eugene Lazutkin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// +import * as AsObjects from './AsObjects'; +import * as Parser from './Parser'; +import * as Stringer from './Stringer'; -declare module 'stream-csv-as-json' { - import * as Parser from 'stream-csv-as-json/Parser'; +export = make; - export = make; +declare function make(options?: Parser.ParserOptions): Parser; - function make(options?: Parser.ParserOptions): Parser; +type ParserClass = Parser; +type ParserType = typeof Parser; - type ParserClass = Parser; - type ParserType = typeof Parser; - - namespace make { - type Parser = ParserClass; - const Parser: ParserType; - const parser: (options?: Parser.ParserOptions) => Parser; - } -} - -declare module 'stream-csv-as-json/AsObjects' { - import { TransformOptions, Transform } from 'stream'; - import * as Chain from 'stream-chain'; - - export = AsObjects; - - class AsObjects extends Transform { - constructor(options?: AsObjects.AsObjectOptions); - } - - namespace AsObjects { - interface AsObjectOptions extends TransformOptions { - packValues?: boolean; - packStrings?: boolean; - streamValues?: boolean; - streamStrings?: boolean; - useValues?: boolean; - useStringValues?: boolean; - fieldPrefix?: string; - } - - function make(options?: AsObjects.AsObjectOptions): AsObjects; - - namespace make { - type Constructor = AsObjects; - const Constructor: typeof AsObjects; - } - - function asObjects(options?: AsObjects.AsObjectOptions): AsObjects; - - namespace asObjects { - type Constructor = AsObjects; - const Constructor: typeof AsObjects; - } - - function withParser(options?: AsObjects.AsObjectOptions): Chain; - } -} - -declare module 'stream-csv-as-json/Parser' { - import { Transform, TransformOptions } from 'stream'; - - export = Parser; - - class Parser extends Transform { - constructor(options?: Parser.ParserOptions); - } - - namespace Parser { - interface ParserOptions extends TransformOptions { - packValues?: boolean; - packStrings?: boolean; - streamValues?: boolean; - streamStrings?: boolean; - separator?: string; - } - - function make(options?: ParserOptions): Parser; - - namespace make { - type Constructor = Parser; - const Constructor: typeof Parser; - } - - function parser(options?: ParserOptions): Parser; - - namespace parser { - type Constructor = Parser; - const Constructor: typeof Parser; - } - } -} - -declare module 'stream-csv-as-json/Stringer' { - import { Transform, TransformOptions } from 'stream'; - - export = Stringer; - - class Stringer extends Transform { - constructor(options?: Stringer.StringerOptions); - } - - namespace Stringer { - interface StringerOptions extends TransformOptions { - useValues?: boolean; - useStringValues?: boolean; - separator?: string; - } - - function make(options?: StringerOptions): Stringer; - - namespace make { - type Constructor = Stringer; - const Constructor: typeof Stringer; - } - - function stringer(options?: StringerOptions): Stringer; - - namespace stringer { - type Constructor = Stringer; - const Constructor: typeof Stringer; - } - } +declare namespace make { + type Parser = ParserClass; + const Parser: ParserType; + function parser(options?: Parser.ParserOptions): Parser; } diff --git a/types/stream-csv-as-json/tsconfig.json b/types/stream-csv-as-json/tsconfig.json index d705f46cca..931caaa728 100644 --- a/types/stream-csv-as-json/tsconfig.json +++ b/types/stream-csv-as-json/tsconfig.json @@ -9,7 +9,8 @@ "typeRoots": ["../"], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true }, "files": ["index.d.ts", "stream-csv-as-json-tests.ts"] }