Collection methods with sync and async features
npm install alot
Array/Stream methods
ts
import alot from 'alot';
const users: IUser[];
// Sync
const userAgeGroups = alot(users)
.distinctBy(x => x.email)
.sortBy(x => x.createdAt)
.groupBy(x => x.age)
.skip(1)
.take(2)
.toArray();
// Async
const userData = await alot(users)
.mapAsync(async user => UserService.loadFooMeta(user.id))
.toArrayAsync({ threads: 4 });
`
----
- 📝 Blog Post 🔗
- 📚 API Documentation 🔗
----
Methods overview:
$3
`ts
map (fn: (x: T, i?: number) => TResult): IAlotStream
`
`ts
mapAsync (fn: (x: T, i?: number) => PromiseLike): IAlotStream
`
$3
`ts
map (fn: (x: T, i?: number) => TResult[]): IAlotStream
`
`ts
mapAsync (fn: (x: T, i?: number) => PromiseLike): IAlotStream
`
$3
Transform the complete stream
`ts
mapFull (fn: (arr: T[]) => TResult): IAlotStream
`
`ts
mapFullAsync (fn: (arr: T[]) => PromiseLike): PromiseLike
`
$3
`ts
filter (fn: (x: T, i?: number) => boolean): IAlotStream
`
`ts
filterAsync (fn: (x: T, i?: number) => PromiseLike): IAlotStream
`
$3
`ts
forEach (fn: (x: T, i?: number) => void | any): IAlotStream
`
`ts
forEachAsync (fn: (x: T, i?: number) => void | any): IAlotStream
`
$3
`ts
take (count: number): IAlotStream
`
`ts
takeWhile (fn: (x: T) => boolean): IAlotStream
takeWhileAsync (fn: (x: T) => boolean | Promise): IAlotStream
`
$3
`ts
skip (count: number): IAlotStream
`
`ts
skipWhile (fn: (x: T, i?: number) => boolean): IAlotStream
skipWhileAsync (fn: (x: T, i?: number) => boolean | Promise): IAlotStream
`
$3
`ts
groupBy (fn: (x: T) => TKey): IAlotStream< { key: TKey[], values: T[] } >
`
$3
> Join elements from collection Inner and collection Outer by the matched Key. Elements with no matches are skipped.
`ts
// Inner Left Join
join (
inner: TInner[],
getKeyOuter: (x: TOuter) => string | number,
getKeyInner: (x: TInner) => string | number,
joinFn: (a: TOuter, b: TInner) => TResult
): IAlotStream< TResult >
`
$3
> Join elements from collection Inner and collection Outer by the matched Key. Elements with no matches are included as is.
`ts
// Outer Full Join
joinOuter (
inner: TInner[],
getKey: (x: TOuter) => string | number,
getForeignKey: (x: TInner) => string | number,
joinFn: (a?: TOuter, b?: TInner) => TResult
): IAlotStream< TResult >
`
$3
> Same as Array::concat
`ts
// Outer Full Join
concat (
other: TOther[],
): IAlotStream< (TSource | TOther) >
`
$3
`ts
distinctBy (fn: (x: T, i?: number) => TKey): IAlotStream
`
$3
`ts
sortBy (fn: (x: T, i?: number) => string | number, direction: 'asc' | 'desc' = 'asc'): IAlotStream
sortBy (property: string, direction: 'asc' | 'desc' = 'asc'): IAlotStream
// also nested path are supported 'user.foo.username'
`
$3
thenBy() specifies a secondary sort method that is used to further sort data that has already been sorted with a call to sortBy()
`ts
alot(arr).sortBy(x => x.age).thenBy(x => x.name).toArray()
`
Output Data
$3
`ts
toArray(): T[]
`
$3
`ts
toArrayAsync(options: {
threads?: number
errors?: 'reject' | 'include' | 'ignore'
} = { threads: 4, errors: 'reject' }): Promise
`
> errors 'reject' - all iterations will be stopped and the task will reject.
> errors 'include' - all iterations will be executed, and any throw or rejection will be in resulted array.
> errors 'ignore' - all iterations will be executed, and any throw or rejection are ignored. The resulted array could contain holes.
$3
`ts
toDictionary(keyFn: (x: T) => TKey, valFn?: (x: T) => TOut ): { [key: string]: TOut }
toDictionaryAsync(keyFn: (x: T) => TKey | Promise, valFn?: (x: T) => TOut | Promise ): Promise<{ [key: string]: TOut }>
`
$3
`ts
toDictionary(keyFn: (x: T) => TKey, valFn?: (x: T) => TOut ): Map
toDictionaryAsync(keyFn: (x: T) => TKey | Promise, valFn?: (x: T) => TOut | Promise ): Promise `
$3
`ts
first(matcher?: (x: T, i?: number) => boolean): T
find(matcher?: (x: T, i?: number) => boolean): T
firstAsync(matcher?: (x: T, i?: number) => boolean | Promise): Promise
find(matcher?: (x: T, i?: number) => boolean | Promise): Promise
`
Aggregation
$3
`ts
sum (fn: (x: T, i?: number) => number): number
sumAsync (fn: (x: T, i?: number) => number | Promise): Promise
`
$3
`ts
max (getVal: (x: T, i?: number) => TNumeric): TNumeric
maxAsync (getVal: (x: T, i?: number) => TNumeric): Promise
min (getVal: (x: T, i?: number) => TOut): TNumeric
minAsync (getVal: (x: T, i?: number) => TOut): Promise
`
Static Methods
#### fromObject
`ts
alot.fromObject(foo: any): Alot<{ key: string, value: string }>
`
#### fromRange
`ts
alot.fromRange(start: number, endExcluded: number): Alot
`
Supports ascending, e.g.[-1..10), and descending ranges - [10..-1)`