diff --git a/types/transducers.js/index.d.ts b/types/transducers.js/index.d.ts new file mode 100644 index 0000000000..504496c1fb --- /dev/null +++ b/types/transducers.js/index.d.ts @@ -0,0 +1,213 @@ +// Type definitions for transducers.js 0.3 +// Project: https://github.com/jlongster/transducers.js +// Definitions by: David Philipson +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type Reducer = ( + result: TResult, + input: TInput, +) => TResult; + +export type Transducer = ( + xf: Transformer, +) => Transformer; + +export interface CompletingTransformer { + ["@@transducer/init"](): TResult | void; + ["@@transducer/result"](result: TResult): TCompleteResult; + ["@@transducer/step"]( + result: TResult, + input: TInput, + ): TResult | Reduced; +} + +export type Transformer = CompletingTransformer< + TResult, + TResult, + TInput +>; + +export interface Reduced { + ["@@transducer/reduced"]: true; + ["@@transducer/value"]: T; +} + +export function reduce( + coll: Iterable, + xf: Transformer, + init: TResult, +): TResult; +export function reduce( + coll: Iterable, + xf: CompletingTransformer, + init: TResult, +): TCompleteResult; +// Overloads for object iteration. +export function reduce( + coll: { [key: string]: TInput }, + xf: Transformer, + init: TResult, +): TResult; +export function reduce( + coll: { [key: string]: TInput }, + xf: CompletingTransformer, + init: TResult, +): TCompleteResult; + +export function transformer( + reducer: Reducer, +): Transformer; + +export interface ReducedConstructor { + new (value: T): Reduced; +} + +export const Reduced: ReducedConstructor; + +export function isReduced(x: any): boolean; + +export function iterator(coll: Iterable): IterableIterator; + +export function push(arr: T[], x: T): T[]; + +export function transduce( + coll: Iterable, + xf: Transducer, + f: Reducer, + init: TResult, +): TResult; +export function transduce( + coll: Iterable, + xf: Transducer, + f: CompletingTransformer, + init?: TResult, +): TCompleteResult; +// Overloads for object iteration. +export function transduce( + coll: { [key: string]: TInput }, + xf: Transducer<[string, TInput], TOutput>, + f: Reducer, + init: TResult, +): TResult; +export function transduce( + coll: { [key: string]: TInput }, + xf: Transducer<[string, TInput], TOutput>, + f: CompletingTransformer, + init?: TResult, +): TCompleteResult; + +export function seq( + coll: TInput[], + xf: Transducer, +): TOutput[]; +export function seq( + coll: Iterable, + xf: Transducer, +): IterableIterator; +export function seq( + coll: { [key: string]: TInput }, + xf: Transducer<[string, TInput], [string, TOutput]>, +): { [key: string]: TOutput }; + +export function toArray( + coll: Iterable, + xf?: Transducer, +): TOutput[]; +export function toArray( + coll: { [key: string]: TInput }, + xf?: Transducer<[string, TInput], TOutput>, +): TOutput[]; + +export function toIter( + coll: Iterable, + xf?: Transducer, +): IterableIterator; +export function toIter( + coll: { [key: string]: TInput }, + xf?: Transducer<[string, TInput], TOutput>, +): IterableIterator; + +export function toObj( + coll: Iterable, + xf?: Transducer, +): { [key: string]: TOutput }; +export function toObj( + coll: { [key: string]: TInput }, + xf: Transducer<[string, TInput], [string, TOutput]>, +): { [key: string]: TOutput }; + +export function into( + to: TOutput[], + xf: Transducer, + from: Iterable, +): TOutput[]; +export function into( + to: string, + xf: Transducer, + from: Iterable, +): string; +export function into( + to: { [key: string]: TOutput }, + xf: Transducer, + from: Iterable, +): { [key: string]: TOutput }; + +export function compose(...fs: Array<(x: any) => any>): (x: any) => any; + +export function map( + f: (x: TInput) => TOutput, +): Transducer; + +export function filter( + pred: (x: TInput) => boolean, +): Transducer; + +export function remove( + pred: (x: TInput) => boolean, +): Transducer; + +export function cat( + f: Transformer, +): Transformer>; + +export function mapcat( + f: (x: TInput) => Iterable, +): Transducer; + +export function keep(): Transducer; + +export function dedupe(): Transducer; + +export function take(n: number): Transducer; + +export function takeWhile( + pred: (x: TInput) => boolean, +): Transducer; + +export function takeNth(n: number): Transducer; + +export function drop(n: number): Transducer; + +export function dropWhile( + pred: (x: TInput) => boolean, +): Transducer; + +export function partition(n: number): Transducer; + +export function partitionBy( + f: (x: TInput) => any, +): Transducer; + +export function interpose(sep: TInput): Transducer; + +export function repeat(n: number): Transducer; + +export function range(n: number): number[]; + +export interface LazyTransformerConstructor { + new (xf: Transducer, coll: Iterable< + TInput + >): IterableIterator; +} + +export const LazyTransformer: LazyTransformerConstructor; diff --git a/types/transducers.js/transducers.js-tests.ts b/types/transducers.js/transducers.js-tests.ts new file mode 100644 index 0000000000..e7a64d1432 --- /dev/null +++ b/types/transducers.js/transducers.js-tests.ts @@ -0,0 +1,37 @@ +import * as t from "transducers.js"; + +const stringAppendFn: t.Reducer = (acc, x) => acc + x; +const stringAppendTransformer = t.transformer(stringAppendFn); +const stringAppendThenLengthTransformer: t.CompletingTransformer< + string, + number, + number +> = { + ["@@transducer/init"]: () => "", + ["@@transducer/result"]: (s: string) => s.length, + ["@@transducer/step"]: stringAppendFn, +}; +const toNumberTransducer: t.Transducer = t.map(s => +s); +const transducedString1: string = t.transduce( + ["1", "2"], + toNumberTransducer, + stringAppendFn, + "", +); +const transducedString2: string = t.transduce( + ["1", "2"], + toNumberTransducer, + stringAppendTransformer, +); + +const mapcatted: number[] = t.into([], t.mapcat((s: string) => [1, 2, 3, 4]), [ + "a", + "b", +]); + +const partitionedIter: Iterator = t.toIter( + [1, 2, 3, 4, 5], + t.partition(2), +); + +const reduced: t.Reduced = new t.Reduced("a"); diff --git a/types/transducers.js/tsconfig.json b/types/transducers.js/tsconfig.json new file mode 100644 index 0000000000..9d2e6f5b96 --- /dev/null +++ b/types/transducers.js/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "transducers.js-tests.ts" + ] +} diff --git a/types/transducers.js/tslint.json b/types/transducers.js/tslint.json new file mode 100644 index 0000000000..d88586e5bd --- /dev/null +++ b/types/transducers.js/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}