@ts-common/iterator
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -12,20 +12,47 @@ import { Tuple2 } from "@ts-common/tuple"; | ||
} | ||
export declare const iterable: <T>(createIterator: () => Iterator<T>) => Iterable<T>; | ||
export interface IterableEx<T> extends Iterable<T> { | ||
readonly entries: () => IterableEx<Entry<T>>; | ||
readonly map: <R>(func: (v: T, i: number) => R) => IterableEx<R>; | ||
readonly flatMap: <R>(func: (v: T, i: number) => Iterable<R>) => IterableEx<R>; | ||
readonly filter: (func: (v: T, i: number) => boolean) => IterableEx<T>; | ||
readonly filterMap: <R>(func: (v: T, i: number) => R | undefined) => IterableEx<R>; | ||
readonly forEach: (func: (v: T, i: number) => void) => void; | ||
readonly drop: (n?: number) => IterableEx<T>; | ||
readonly concat: (...input: Array<Iterable<T> | undefined>) => IterableEx<T>; | ||
readonly takeWhile: (func: (v: T, i: number) => boolean) => IterableEx<T>; | ||
readonly take: (n?: number) => IterableEx<T>; | ||
readonly findEntry: (func: (v: T, i: number) => boolean) => Entry<T> | undefined; | ||
readonly find: (func: (v: T, i: number) => boolean) => T | undefined; | ||
readonly fold: <A>(func: (a: A, b: T, i: number) => A, init: A) => A; | ||
readonly reduce: (func: (a: T, b: T, i: number) => T) => T | undefined; | ||
readonly last: () => T | undefined; | ||
readonly some: (func: (v: T, i: number) => boolean) => boolean; | ||
readonly every: (func: (v: T, i: number) => boolean) => boolean; | ||
readonly zip: (...inputs: Array<Iterable<T> | undefined>) => IterableEx<ReadonlyArray<T>>; | ||
readonly isEqual: <B>(b: Iterable<B> | undefined, e?: (ai: T, bi: B) => boolean) => boolean; | ||
readonly toArray: () => ReadonlyArray<T>; | ||
readonly reverse: () => ReadonlyArray<T>; | ||
readonly isEmpty: () => boolean; | ||
readonly uniq: (key?: (v: T) => unknown) => IterableEx<T>; | ||
} | ||
export declare const iterable: <T>(createIterator: () => Iterator<T>) => IterableEx<T>; | ||
export declare type Entry<T> = Tuple2<number, T>; | ||
export declare const entry: <T>(key: number, value: T) => Entry<T>; | ||
export declare const entries: <T>(input: Iterable<T> | undefined) => Iterable<Tuple2<number, T>>; | ||
export declare const map: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => T) => Iterable<T>; | ||
export declare const drop: <T>(input: Iterable<T> | undefined, n?: number) => Iterable<T>; | ||
export declare const flatten: <T>(input: Iterable<Iterable<T> | undefined> | undefined) => Iterable<T>; | ||
export declare const concat: <T>(...input: (Iterable<T> | undefined)[]) => Iterable<T>; | ||
export declare const takeWhile: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => Iterable<T>; | ||
export declare const take: <T>(input: Iterable<T> | undefined, n?: number) => Iterable<T>; | ||
export declare const ENTRY_KEY = 0; | ||
export declare const ENTRY_VALUE = 1; | ||
export declare const entries: <T>(input: Iterable<T> | undefined) => IterableEx<Tuple2<number, T>>; | ||
export declare const map: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => T) => IterableEx<T>; | ||
export declare const drop: <T>(input: Iterable<T> | undefined, n?: number) => IterableEx<T>; | ||
export declare const flatten: <T>(input: Iterable<Iterable<T> | undefined> | undefined) => IterableEx<T>; | ||
export declare const concat: <T>(...input: (Iterable<T> | undefined)[]) => IterableEx<T>; | ||
export declare const takeWhile: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => IterableEx<T>; | ||
export declare const take: <T>(input: Iterable<T> | undefined, n?: number) => IterableEx<T>; | ||
export declare const findEntry: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => Tuple2<number, T> | undefined; | ||
export declare const find: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => T | undefined; | ||
export declare const flatMap: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => Iterable<T>) => Iterable<T>; | ||
export declare const flatMap: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => Iterable<T>) => IterableEx<T>; | ||
export declare const optionalToArray: <T>(v: T | undefined) => ReadonlyArray<T>; | ||
export declare const filterMap: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => T | undefined) => Iterable<T>; | ||
export declare const filter: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => Iterable<T>; | ||
export declare const generate: <T>(func: (i: number) => T, count?: number | undefined) => Iterable<T>; | ||
export declare const repeat: <T>(v: T, count?: number | undefined) => Iterable<T>; | ||
export declare const filterMap: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => T | undefined) => IterableEx<T>; | ||
export declare const filter: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => IterableEx<T>; | ||
export declare const generate: <T>(func: (i: number) => T, count?: number | undefined) => IterableEx<T>; | ||
export declare const repeat: <T>(v: T, count?: number | undefined) => IterableEx<T>; | ||
export declare const fold: <T, A>(input: Iterable<T> | undefined, func: (a: A, b: T, i: number) => A, init: A) => A; | ||
@@ -40,3 +67,3 @@ export declare const reduce: <T>(input: Iterable<T> | undefined, func: (a: T, b: T, i: number) => T) => T | undefined; | ||
export declare const max: (input: Iterable<number> | undefined) => number; | ||
export declare const zip: <T>(...inputs: (Iterable<T> | undefined)[]) => Iterable<ReadonlyArray<T>>; | ||
export declare const zip: <T>(...inputs: (Iterable<T> | undefined)[]) => IterableEx<ReadonlyArray<T>>; | ||
export declare const isStrictEqual: (a: unknown, b: unknown) => boolean; | ||
@@ -49,4 +76,5 @@ export declare const isEqual: <A, B>(a: Iterable<A> | undefined, b: Iterable<B> | undefined, e?: (ai: A, bi: B) => boolean) => boolean; | ||
export declare const join: (i: Iterable<string> | undefined, separator: string) => string; | ||
export declare const dropRight: <T>(i: ReadonlyArray<T> | undefined, n?: number) => Iterable<T>; | ||
export declare const uniq: <T>(i: Iterable<T>, key?: (v: T) => unknown) => Iterable<T>; | ||
export declare const empty: <T>() => IterableEx<T>; | ||
export declare const dropRight: <T>(i: ReadonlyArray<T> | undefined, n?: number) => IterableEx<T>; | ||
export declare const uniq: <T>(i: Iterable<T>, key?: (v: T) => unknown) => IterableEx<T>; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tuple_1 = require("@ts-common/tuple"); | ||
exports.iterable = (createIterator) => ({ [Symbol.iterator]: createIterator }); | ||
// tslint:disable-next-line:no-class | ||
class IterableImpl { | ||
constructor(createIterator) { | ||
// tslint:disable-next-line:no-expression-statement | ||
this[Symbol.iterator] = createIterator; | ||
} | ||
// tslint:disable-next-line:readonly-array | ||
concat(...input) { return exports.concat(this, ...input); } | ||
drop(n) { return exports.drop(this, n); } | ||
entries() { return exports.entries(this); } | ||
every(func) { return exports.every(this, func); } | ||
filter(func) { return exports.filter(this, func); } | ||
filterMap(func) { return exports.filterMap(this, func); } | ||
find(func) { return exports.find(this, func); } | ||
findEntry(func) { return exports.findEntry(this, func); } | ||
flatMap(func) { return exports.flatMap(this, func); } | ||
fold(func, init) { return exports.fold(this, func, init); } | ||
forEach(func) { return exports.forEach(this, func); } | ||
isEmpty() { return exports.isEmpty(this); } | ||
isEqual(b, e) { return exports.isEqual(this, b, e); } | ||
last() { return exports.last(this); } | ||
map(func) { return exports.map(this, func); } | ||
reduce(func) { return exports.reduce(this, func); } | ||
reverse() { return exports.reverse(this); } | ||
some(func) { return exports.some(this, func); } | ||
take(n) { return exports.take(this, n); } | ||
takeWhile(func) { return exports.takeWhile(this, func); } | ||
toArray() { return exports.toArray(this); } | ||
uniq(key) { return exports.uniq(this, key); } | ||
// tslint:disable-next-line:readonly-array | ||
zip(...inputs) { return exports.zip(this, ...inputs); } | ||
} | ||
exports.iterable = (createIterator) => new IterableImpl(createIterator); | ||
exports.entry = tuple_1.tuple2; | ||
const ENTRY_KEY = 0; | ||
const ENTRY_VALUE = 1; | ||
exports.ENTRY_KEY = 0; | ||
exports.ENTRY_VALUE = 1; | ||
exports.entries = (input) => exports.iterable(function* () { | ||
@@ -58,3 +90,3 @@ // tslint:disable-next-line:no-if-statement | ||
// tslint:disable-next-line:no-if-statement | ||
if (func(e[ENTRY_VALUE], e[ENTRY_KEY])) { | ||
if (func(e[exports.ENTRY_VALUE], e[exports.ENTRY_KEY])) { | ||
return e; | ||
@@ -67,3 +99,3 @@ } | ||
const e = exports.findEntry(input, func); | ||
return e === undefined ? undefined : e[ENTRY_VALUE]; | ||
return e === undefined ? undefined : e[exports.ENTRY_VALUE]; | ||
}; | ||
@@ -80,3 +112,3 @@ exports.flatMap = (input, func) => exports.flatten(exports.map(input, func)); | ||
}); | ||
exports.generate = (func, count) => exports.map(exports.takeWhile(infinite(), (_, i) => i !== count), (_, i) => func(i)); | ||
exports.generate = (func, count) => infinite().takeWhile((_, i) => i !== count).map((_, i) => func(i)); | ||
exports.repeat = (v, count) => exports.generate(() => v, count); | ||
@@ -156,3 +188,5 @@ exports.fold = (input, func, init) => { | ||
}; | ||
exports.dropRight = (i, n = 1) => i === undefined ? [] : exports.take(i, i.length - n); | ||
// tslint:disable-next-line:no-empty | ||
exports.empty = () => exports.iterable(function* () { }); | ||
exports.dropRight = (i, n = 1) => i === undefined ? exports.empty() : exports.take(i, i.length - n); | ||
exports.uniq = (i, key = v => v) => exports.iterable(function* () { | ||
@@ -159,0 +193,0 @@ const set = new Set(); |
{ | ||
"name": "@ts-common/iterator", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Iterator library for JavaScript and TypeScript", | ||
@@ -49,12 +49,11 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.5", | ||
"chai": "^4.2.0", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^11.9.3", | ||
"mocha": "^5.2.0", | ||
"mocha-junit-reporter": "^1.18.0", | ||
"mocha-multi-reporters": "^1.1.7", | ||
"nyc": "^13.1.0", | ||
"nyc": "^13.2.0", | ||
"tslint": "^5.12.1", | ||
"tslint-immutable": "^5.1.2", | ||
"typescript": "^3.3.1" | ||
"tslint-immutable": "^5.2.0", | ||
"typescript": "^3.3.3" | ||
}, | ||
@@ -61,0 +60,0 @@ "dependencies": { |
107
src/index.ts
@@ -16,5 +16,67 @@ import { Tuple2, tuple2 } from "@ts-common/tuple" | ||
export const iterable = <T>(createIterator: () => Iterator<T>): Iterable<T> => | ||
({ [Symbol.iterator]: createIterator }) | ||
export interface IterableEx<T> extends Iterable<T> { | ||
readonly entries: () => IterableEx<Entry<T>> | ||
readonly map: <R>(func: (v: T, i: number) => R) => IterableEx<R> | ||
readonly flatMap: <R>(func: (v: T, i: number) => Iterable<R>) => IterableEx<R> | ||
readonly filter: (func: (v: T, i: number) => boolean) => IterableEx<T> | ||
readonly filterMap: <R>(func: (v: T, i: number) => R|undefined) => IterableEx<R> | ||
readonly forEach: (func: (v: T, i: number) => void) => void | ||
readonly drop: (n?: number) => IterableEx<T> | ||
// tslint:disable-next-line:readonly-array | ||
readonly concat: (...input: Array<Iterable<T>|undefined>) => IterableEx<T> | ||
readonly takeWhile: (func: (v: T, i: number) => boolean) => IterableEx<T> | ||
readonly take: (n?: number) => IterableEx<T> | ||
readonly findEntry: (func: (v: T, i: number) => boolean) => Entry<T>|undefined | ||
readonly find: (func: (v: T, i: number) => boolean) => T|undefined | ||
readonly fold: <A>(func: (a: A, b: T, i: number) => A, init: A) => A | ||
readonly reduce: (func: (a: T, b: T, i: number) => T) => T|undefined | ||
readonly last: () => T|undefined | ||
readonly some: (func: (v: T, i: number) => boolean) => boolean | ||
readonly every: (func: (v: T, i: number) => boolean) => boolean | ||
// tslint:disable-next-line:readonly-array | ||
readonly zip: (...inputs: Array<Iterable<T>|undefined>) => IterableEx<ReadonlyArray<T>> | ||
readonly isEqual: <B>(b: Iterable<B>|undefined, e?: (ai: T, bi: B) => boolean) => boolean | ||
readonly toArray: () => ReadonlyArray<T> | ||
readonly reverse: () => ReadonlyArray<T> | ||
readonly isEmpty: () => boolean | ||
readonly uniq: (key?: (v: T) => unknown) => IterableEx<T> | ||
} | ||
// tslint:disable-next-line:no-class | ||
class IterableImpl<T> implements IterableEx<T> { | ||
public readonly [Symbol.iterator]: () => Iterator<T> | ||
constructor(createIterator: () => Iterator<T>) { | ||
// tslint:disable-next-line:no-expression-statement | ||
this[Symbol.iterator] = createIterator | ||
} | ||
// tslint:disable-next-line:readonly-array | ||
public concat(...input: Array<Iterable<T>|undefined>) { return concat(this, ...input) } | ||
public drop(n?: number) { return drop(this, n) } | ||
public entries() { return entries(this) } | ||
public every(func: (v: T, i: number) => boolean) { return every(this, func) } | ||
public filter(func: (v: T, i: number) => boolean) { return filter(this, func) } | ||
public filterMap<R>(func: (v: T, i: number) => R|undefined) { return filterMap(this, func) } | ||
public find(func: (v: T, i: number) => boolean) { return find(this, func) } | ||
public findEntry(func: (v: T, i: number) => boolean) { return findEntry(this, func) } | ||
public flatMap<R>(func: (v: T, i: number) => Iterable<R>) { return flatMap(this, func) } | ||
public fold<A>(func: (a: A, b: T, i: number) => A, init: A) { return fold(this, func, init) } | ||
public forEach(func: (v: T, i: number) => void) { return forEach(this, func) } | ||
public isEmpty() { return isEmpty(this) } | ||
public isEqual<B>(b: Iterable<B>|undefined, e?: (ai: T, bi: B) => boolean) { return isEqual(this, b, e) } | ||
public last() { return last(this) } | ||
public map<R>(func: (v: T, i: number) => R) { return map(this, func) } | ||
public reduce(func: (a: T, b: T, i: number) => T) { return reduce(this, func) } | ||
public reverse() { return reverse(this) } | ||
public some(func: (v: T, i: number) => boolean) { return some(this, func) } | ||
public take(n?: number) { return take(this, n) } | ||
public takeWhile(func: (v: T, i: number) => boolean) { return takeWhile(this, func) } | ||
public toArray() { return toArray(this) } | ||
public uniq(key?: (v: T) => unknown) { return uniq(this, key) } | ||
// tslint:disable-next-line:readonly-array | ||
public zip(...inputs: Array<Iterable<T>|undefined>) { return zip(this, ...inputs) } | ||
} | ||
export const iterable = <T>(createIterator: () => Iterator<T>): IterableEx<T> => | ||
new IterableImpl(createIterator) | ||
export type Entry<T> = Tuple2<number, T> | ||
@@ -24,6 +86,6 @@ | ||
const ENTRY_KEY = 0 | ||
const ENTRY_VALUE = 1 | ||
export const ENTRY_KEY = 0 | ||
export const ENTRY_VALUE = 1 | ||
export const entries = <T>(input: Iterable<T>|undefined): Iterable<Entry<T>> => | ||
export const entries = <T>(input: Iterable<T>|undefined): IterableEx<Entry<T>> => | ||
iterable(function *(): Iterator<Entry<T>> { | ||
@@ -46,3 +108,3 @@ // tslint:disable-next-line:no-if-statement | ||
func: (v: I, i: number) => T, | ||
): Iterable<T> => | ||
): IterableEx<T> => | ||
iterable(function *(): Iterator<T> { | ||
@@ -55,6 +117,6 @@ /* tslint:disable-next-line:no-loop-statement */ | ||
export const drop = <T>(input: Iterable<T>|undefined, n: number = 1): Iterable<T> => | ||
export const drop = <T>(input: Iterable<T>|undefined, n: number = 1): IterableEx<T> => | ||
filter(input, (_, i) => n <= i) | ||
export const flatten = <T>(input: Iterable<Iterable<T>|undefined>|undefined): Iterable<T> => | ||
export const flatten = <T>(input: Iterable<Iterable<T>|undefined>|undefined): IterableEx<T> => | ||
iterable(function *(): Iterator<T> { | ||
@@ -75,3 +137,3 @@ // tslint:disable-next-line:no-if-statement | ||
// tslint:disable-next-line:readonly-array | ||
export const concat = <T>(...input: Array<Iterable<T>|undefined>): Iterable<T> => | ||
export const concat = <T>(...input: Array<Iterable<T>|undefined>): IterableEx<T> => | ||
flatten(input) | ||
@@ -82,3 +144,3 @@ | ||
func: (v: T, i: number) => boolean, | ||
): Iterable<T> => | ||
): IterableEx<T> => | ||
iterable(function *(): Iterator<T> { | ||
@@ -123,3 +185,3 @@ /* tslint:disable-next-line:no-loop-statement */ | ||
func: (v: I, i: number) => Iterable<T>, | ||
): Iterable<T> => | ||
): IterableEx<T> => | ||
flatten(map(input, func)) | ||
@@ -133,3 +195,3 @@ | ||
func: (v: I, i: number) => T|undefined, | ||
): Iterable<T> => | ||
): IterableEx<T> => | ||
flatMap(input, (v, i) => optionalToArray(func(v, i))) | ||
@@ -140,6 +202,6 @@ | ||
func: (v: T, i: number) => boolean, | ||
): Iterable<T> => | ||
): IterableEx<T> => | ||
flatMap(input, (v, i) => func(v, i) ? [v] : []) | ||
const infinite = (): Iterable<void> => | ||
const infinite = (): IterableEx<void> => | ||
iterable(function *(): Iterator<void> { | ||
@@ -150,6 +212,6 @@ /* tslint:disable-next-line:no-loop-statement */ | ||
export const generate = <T>(func: (i: number) => T, count?: number): Iterable<T> => | ||
map(takeWhile(infinite(), (_, i) => i !== count), (_, i) => func(i)) | ||
export const generate = <T>(func: (i: number) => T, count?: number): IterableEx<T> => | ||
infinite().takeWhile((_, i) => i !== count).map((_, i) => func(i)) | ||
export const repeat = <T>(v: T, count?: number): Iterable<T> => | ||
export const repeat = <T>(v: T, count?: number): IterableEx<T> => | ||
generate(() => v, count) | ||
@@ -210,3 +272,3 @@ | ||
/* tslint:disable-next-line:readonly-array */ | ||
export const zip = <T>(...inputs: Array<Iterable<T>|undefined>): Iterable<ReadonlyArray<T>> => | ||
export const zip = <T>(...inputs: Array<Iterable<T>|undefined>): IterableEx<ReadonlyArray<T>> => | ||
iterable(function *(): Iterator<ReadonlyArray<T>> { | ||
@@ -283,6 +345,9 @@ const iterators = inputs.map( | ||
export const dropRight = <T>(i: ReadonlyArray<T>|undefined, n: number = 1): Iterable<T> => | ||
i === undefined ? [] : take(i, i.length - n) | ||
// tslint:disable-next-line:no-empty | ||
export const empty = <T>() => iterable(function *(): Iterator<T> { }) | ||
export const uniq = <T>(i: Iterable<T>, key: (v: T) => unknown = v => v): Iterable<T> => | ||
export const dropRight = <T>(i: ReadonlyArray<T>|undefined, n: number = 1): IterableEx<T> => | ||
i === undefined ? empty() : take(i, i.length - n) | ||
export const uniq = <T>(i: Iterable<T>, key: (v: T) => unknown = v => v): IterableEx<T> => | ||
iterable(function *() { | ||
@@ -289,0 +354,0 @@ const set = new Set<unknown>() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54547
9
584