0.3.9 • Published 4 months ago

alot v0.3.9

Weekly downloads
8
License
MIT
Repository
github
Last release
4 months ago

Alot


Build Status NPM version ts

🌱 Lazy, ⛓️ Async and ⚡ Performance-Optimized Array/Stream methods

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


Methods overview:

map, mapAsync

map <T, TResult> (fn: (x: T, i?: number) => TResult): IAlotStream<TResult>
mapAsync <T, TResult> (fn: (x: T, i?: number) => PromiseLike<TResult>): IAlotStream<TResult>

mapMany, mapManyAsync

map <T, TResult> (fn: (x: T, i?: number) => TResult[]): IAlotStream<TResult>
mapAsync <T, TResult> (fn: (x: T, i?: number) => PromiseLike<TResult[]>): IAlotStream<TResult>

filter, filterAsync

filter <T> (fn: (x: T, i?: number) => boolean): IAlotStream<T>
filterAsync <T> (fn: (x: T, i?: number) => PromiseLike<boolean>): IAlotStream<T>

forEach, forEachAsync

forEach <T> (fn: (x: T, i?: number) => void | any): IAlotStream<T>
forEachAsync <T> (fn: (x: T, i?: number) => void | any): IAlotStream<T>

take, takeWhile, takeWhileAsync

take <T> (count: number): IAlotStream<T>
takeWhile <T> (fn: (x: T) => boolean): IAlotStream<T>
takeWhileAsync <T> (fn: (x: T) => boolean | Promise<boolean>): IAlotStream<T>

skip, skipWhile, skipWhileAsync

skip <T> (count: number): IAlotStream<T>
skipWhile <T> (fn: (x: T, i?: number) => boolean): IAlotStream<T>
skipWhileAsync <T> (fn: (x: T, i?: number) => boolean | Promise<boolean>): IAlotStream<T>

groupBy

groupBy <T, TKey = string > (fn: (x: T) => TKey): IAlotStream< { key: TKey[], values: T[] } >

join

Join elements from collection Inner and collection Outer by the matched Key. Elements with no matches are skipped.

// Inner Left Join
join <TOuter, TResult> (
    inner: TInner[],
    getKeyOuter: (x: TOuter) => string | number,
    getKeyInner: (x: TInner) => string | number,
    joinFn: (a: TOuter, b: TInner) => TResult
): IAlotStream< TResult >

joinOuter

Join elements from collection Inner and collection Outer by the matched Key. Elements with no matches are included as is.

// Outer Full Join
joinOuter <TOuter, TResult> (
    inner: TInner[],
    getKey: (x: TOuter) => string | number,
    getForeignKey: (x: TInner) => string | number,
    joinFn: (a?: TOuter, b?: TInner) => TResult
): IAlotStream< TResult >

concat

Same as Array::concat

// Outer Full Join
concat <TSource> (
    other: TOther[],
): IAlotStream< (TSource | TOther) >

distinctBy

distinctBy <T, TKey = string> (fn: (x: T, i?: number) => TKey): IAlotStream<T>

sortBy, sortByAsync, sortByLocalCompare

sortBy <T> (fn: (x: T, i?: number) => string | number, direction: 'asc' | 'desc' = 'asc'): IAlotStream<T>
sortBy <T> (property: string, direction: 'asc' | 'desc' = 'asc'): IAlotStream<T>
// also nested path are supported 'user.foo.username'

thenBy

thenBy() specifies a secondary sort method that is used to further sort data that has already been sorted with a call to sortBy()

alot(arr).sortBy(x => x.age).thenBy(x => x.name).toArray()

Output Data

toArray

toArray(): T[]

toArrayAsync

toArrayAsync(options: {
    threads?: number
    errors?: 'reject' | 'include' | 'ignore'
} = { threads: 4, errors: 'reject' }): Promise<T[]>

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.

toDictionary toDictionaryAsync

toDictionary<TKey = string | number, TOut = T>(keyFn: (x: T) => TKey, valFn?: (x: T) => TOut ): { [key: string]: TOut }
toDictionaryAsync<TKey = string | number, TOut = T>(keyFn: (x: T) => TKey | Promise<TKey>, valFn?: (x: T) => TOut | Promise<TOut> ): Promise<{ [key: string]: TOut }>

toMap toMapAsync

toDictionary<TKey = any, TOut = T>(keyFn: (x: T) => TKey, valFn?: (x: T) => TOut ): Map<TKey, TOut>
toDictionaryAsync<TKey = any, TOut = T>(keyFn: (x: T) => TKey | Promise<TKey>, valFn?: (x: T) => TOut | Promise<TOut> ): Promise<Map<TKey, TOut>>

first, find (alias), firstAsync, findAsync

first(matcher?: (x: T, i?: number) => boolean): T
find(matcher?: (x: T, i?: number) => boolean): T

firstAsync(matcher?: (x: T, i?: number) => boolean | Promise<boolean>): Promise<T>
find(matcher?: (x: T, i?: number) => boolean | Promise<boolean>): Promise<T>

Aggregation

sum, sumAsync, sumBigInt, sumBigIntAsync

sum      (fn: (x: T, i?: number) => number): number
sumAsync (fn: (x: T, i?: number) => number | Promise<number>): Promise<number>

max, maxAsync, min, minAsync

max <TNumeric> (getVal: (x: T, i?: number) => TNumeric): TNumeric
maxAsync <TNumeric> (getVal: (x: T, i?: number) => TNumeric): Promise<TNumeric>

min <TNumeric> (getVal: (x: T, i?: number) => TOut): TNumeric
minAsync <TNumeric> (getVal: (x: T, i?: number) => TOut): Promise<TNumeric>

Static Methods

fromObject

alot.fromObject(foo: any): Alot<{ key: string, value: string }>

fromRange

alot.fromRange(start: number, endExcluded: number): Alot<number>

Supports ascending, e.g.[-1..10), and descending ranges - [10..-1)


Atma.js Project

0.3.9

4 months ago

0.3.8

4 months ago

0.3.6

6 months ago

0.3.7

6 months ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.3

2 years ago

0.2.96

2 years ago

0.2.94

2 years ago

0.2.99

2 years ago

0.2.98

2 years ago

0.2.97

2 years ago

0.2.93

3 years ago

0.2.92

3 years ago

0.2.91

3 years ago

0.2.90

3 years ago

0.2.89

3 years ago

0.2.88

3 years ago

0.2.87

3 years ago

0.2.86

3 years ago

0.2.84

3 years ago

0.2.83

3 years ago

0.2.82

4 years ago

0.2.81

4 years ago

0.2.80

4 years ago

0.2.79

4 years ago

0.2.78

4 years ago

0.2.77

4 years ago

0.2.76

4 years ago

0.2.72

4 years ago

0.2.75

4 years ago

0.2.71

4 years ago

0.2.70

4 years ago

0.2.69

4 years ago

0.2.68

4 years ago

0.2.67

5 years ago

0.2.66

5 years ago

0.2.65

5 years ago

0.2.64

5 years ago

0.2.63

5 years ago

0.2.62

5 years ago

0.2.61

5 years ago

0.2.60

5 years ago

0.2.59

5 years ago

0.2.58

5 years ago

0.2.57

5 years ago

0.2.56

5 years ago