@anil-labs/collection-js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +251 -0
- package/dist/index.es.js +3211 -0
- package/dist/index.umd.cjs +8 -0
- package/dist/src/async/AsyncCollection.d.ts +57 -0
- package/dist/src/async/concurrent.d.ts +6 -0
- package/dist/src/async/index.d.ts +3 -0
- package/dist/src/collection/Collection.d.ts +274 -0
- package/dist/src/collection/HigherOrderProxy.d.ts +19 -0
- package/dist/src/collection/LazyCollection.d.ts +173 -0
- package/dist/src/collection/index.d.ts +5 -0
- package/dist/src/contracts/Arrayable.d.ts +4 -0
- package/dist/src/contracts/Enumerable.d.ts +17 -0
- package/dist/src/contracts/Jsonable.d.ts +3 -0
- package/dist/src/contracts/Macroable.d.ts +6 -0
- package/dist/src/contracts/index.d.ts +5 -0
- package/dist/src/exceptions/CollectionException.d.ts +3 -0
- package/dist/src/exceptions/ItemNotFoundException.d.ts +4 -0
- package/dist/src/exceptions/MultipleItemsFoundException.d.ts +5 -0
- package/dist/src/exceptions/UnexpectedValueException.d.ts +4 -0
- package/dist/src/exceptions/index.d.ts +4 -0
- package/dist/src/helpers/collect.d.ts +3 -0
- package/dist/src/helpers/index.d.ts +2 -0
- package/dist/src/helpers/lazy.d.ts +3 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/io/csv.d.ts +30 -0
- package/dist/src/io/index.d.ts +5 -0
- package/dist/src/io/jsonl.d.ts +9 -0
- package/dist/src/io/streams.d.ts +27 -0
- package/dist/src/macros/Macroable.d.ts +20 -0
- package/dist/src/macros/index.d.ts +1 -0
- package/dist/src/operations/accessors.d.ts +19 -0
- package/dist/src/operations/aggregations.d.ts +9 -0
- package/dist/src/operations/chunking.d.ts +13 -0
- package/dist/src/operations/combine.d.ts +4 -0
- package/dist/src/operations/compare.d.ts +29 -0
- package/dist/src/operations/conditionals.d.ts +7 -0
- package/dist/src/operations/filters.d.ts +27 -0
- package/dist/src/operations/grouping.d.ts +5 -0
- package/dist/src/operations/index.d.ts +22 -0
- package/dist/src/operations/itertools.d.ts +37 -0
- package/dist/src/operations/joins.d.ts +17 -0
- package/dist/src/operations/mutations.d.ts +40 -0
- package/dist/src/operations/objectOps.d.ts +10 -0
- package/dist/src/operations/random.d.ts +2 -0
- package/dist/src/operations/reducers.d.ts +9 -0
- package/dist/src/operations/sequence.d.ts +4 -0
- package/dist/src/operations/setOps.d.ts +14 -0
- package/dist/src/operations/sliceOps.d.ts +8 -0
- package/dist/src/operations/sorting.d.ts +11 -0
- package/dist/src/operations/stats.d.ts +30 -0
- package/dist/src/operations/strings.d.ts +2 -0
- package/dist/src/operations/transformations.d.ts +9 -0
- package/dist/src/operations/uniqueness.d.ts +3 -0
- package/dist/src/support/arrayWrap.d.ts +6 -0
- package/dist/src/support/dataGet.d.ts +8 -0
- package/dist/src/support/deepClone.d.ts +1 -0
- package/dist/src/support/deepEqual.d.ts +11 -0
- package/dist/src/support/index.d.ts +8 -0
- package/dist/src/support/isObject.d.ts +4 -0
- package/dist/src/support/operatorForWhere.d.ts +4 -0
- package/dist/src/support/types.d.ts +15 -0
- package/dist/src/support/valueRetriever.d.ts +7 -0
- package/package.json +93 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { Enumerable } from '../../contracts/Enumerable';
|
|
2
|
+
import { MacroableTarget } from '../../macros/Macroable';
|
|
3
|
+
import { ClassConstructor, Comparator, Predicate } from '../../support/types';
|
|
4
|
+
import { RetrieverInput } from '../../support/valueRetriever';
|
|
5
|
+
import { Collection } from '../../collection/Collection';
|
|
6
|
+
import * as ops from '@/operations';
|
|
7
|
+
export type LazySource<T> = Iterable<T> | (() => Iterable<T>);
|
|
8
|
+
/**
|
|
9
|
+
* Lazy generator-backed collection. Mirrors Laravel's `LazyCollection`.
|
|
10
|
+
* The same fluent surface as `Collection`, but every method that returns a new
|
|
11
|
+
* collection returns a `LazyCollection` over a generator — values are produced
|
|
12
|
+
* on demand. Mutating methods (push/pop/etc.) are intentionally absent.
|
|
13
|
+
*/
|
|
14
|
+
export declare class LazyCollection<T> implements Enumerable<T> {
|
|
15
|
+
private readonly source;
|
|
16
|
+
constructor(source?: LazySource<T>);
|
|
17
|
+
[Symbol.iterator](): Iterator<T>;
|
|
18
|
+
get [Symbol.toStringTag](): string;
|
|
19
|
+
static make<T>(source?: LazySource<T>): LazyCollection<T>;
|
|
20
|
+
static empty<T>(): LazyCollection<T>;
|
|
21
|
+
static range(start: number, end: number, step?: number): LazyCollection<number>;
|
|
22
|
+
static times<T>(count: number, factory: (n: number) => T): LazyCollection<T>;
|
|
23
|
+
static wrap<T>(value: T | T[] | Iterable<T> | LazyCollection<T> | null | undefined): LazyCollection<T>;
|
|
24
|
+
static unwrap<T>(value: T | T[] | LazyCollection<T>): T | T[];
|
|
25
|
+
all(): T[];
|
|
26
|
+
toArray(): T[];
|
|
27
|
+
toJson(): string;
|
|
28
|
+
toJSON(): T[];
|
|
29
|
+
toMap<K, V>(keyFn: (item: T, i: number) => K, valueFn: (item: T, i: number) => V): Map<K, V>;
|
|
30
|
+
toSet(): Set<T>;
|
|
31
|
+
collect(): Collection<T>;
|
|
32
|
+
count(): number;
|
|
33
|
+
isEmpty(): boolean;
|
|
34
|
+
isNotEmpty(): boolean;
|
|
35
|
+
countBy(by?: RetrieverInput<T>): Record<string, number>;
|
|
36
|
+
first(predicate?: Predicate<T>): T | undefined;
|
|
37
|
+
firstOrFail(predicate?: Predicate<T>): T;
|
|
38
|
+
firstWhere(key: string, ...rest: readonly unknown[]): T | undefined;
|
|
39
|
+
last(predicate?: Predicate<T>): T | undefined;
|
|
40
|
+
sole(predicate?: Predicate<T> | string, expected?: unknown): T;
|
|
41
|
+
random(): T | undefined;
|
|
42
|
+
contains(target: unknown, ...rest: readonly unknown[]): boolean;
|
|
43
|
+
containsStrict(target: unknown, ...rest: readonly unknown[]): boolean;
|
|
44
|
+
doesntContain(target: unknown, ...rest: readonly unknown[]): boolean;
|
|
45
|
+
every(predicate: Predicate<T>): boolean;
|
|
46
|
+
some(predicate: Predicate<T>): boolean;
|
|
47
|
+
search(target: T | Predicate<T>, strict?: boolean): number | false;
|
|
48
|
+
has(key: string | readonly string[]): boolean;
|
|
49
|
+
sum(by?: RetrieverInput<T, number>): number;
|
|
50
|
+
average(by?: RetrieverInput<T, number>): number;
|
|
51
|
+
avg(by?: RetrieverInput<T, number>): number;
|
|
52
|
+
max<R = number>(by?: RetrieverInput<T, R>): R | undefined;
|
|
53
|
+
min<R = number>(by?: RetrieverInput<T, R>): R | undefined;
|
|
54
|
+
median(by?: RetrieverInput<T, number>): number | undefined;
|
|
55
|
+
mode(by?: RetrieverInput<T>): unknown[] | undefined;
|
|
56
|
+
percentage(predicate: Predicate<T>, precision?: number): number;
|
|
57
|
+
filter(predicate?: Predicate<T>): LazyCollection<T>;
|
|
58
|
+
reject(predicate: Predicate<T>): LazyCollection<T>;
|
|
59
|
+
where(key: string, ...rest: readonly unknown[]): LazyCollection<T>;
|
|
60
|
+
whereStrict(key: string, value: unknown): LazyCollection<T>;
|
|
61
|
+
whereIn(key: string, values: readonly unknown[]): LazyCollection<T>;
|
|
62
|
+
whereInStrict(key: string, values: readonly unknown[]): LazyCollection<T>;
|
|
63
|
+
whereNotIn(key: string, values: readonly unknown[]): LazyCollection<T>;
|
|
64
|
+
whereNotInStrict(key: string, values: readonly unknown[]): LazyCollection<T>;
|
|
65
|
+
/** Shared engine for the four whereIn/whereNotIn variants. */
|
|
66
|
+
private whereInFilter;
|
|
67
|
+
whereBetween(key: string, range: readonly [unknown, unknown]): LazyCollection<T>;
|
|
68
|
+
whereNotBetween(key: string, range: readonly [unknown, unknown]): LazyCollection<T>;
|
|
69
|
+
whereNull(key: string): LazyCollection<T>;
|
|
70
|
+
whereNotNull(key: string): LazyCollection<T>;
|
|
71
|
+
/** Lazy SQL-`LIKE` filter (`%` = any run, `_` = single char). See {@link Collection.whereLike}. */
|
|
72
|
+
whereLike(key: string, pattern: string, caseSensitive?: boolean): LazyCollection<T>;
|
|
73
|
+
/** Inverse of {@link whereLike}. */
|
|
74
|
+
whereNotLike(key: string, pattern: string, caseSensitive?: boolean): LazyCollection<T>;
|
|
75
|
+
whereInstanceOf<R>(Ctor: ClassConstructor<R> | (abstract new (...args: never[]) => R)): LazyCollection<R>;
|
|
76
|
+
map<R>(fn: (item: T, index: number) => R): LazyCollection<R>;
|
|
77
|
+
mapInto<R>(Ctor: ClassConstructor<R, [T]>): LazyCollection<R>;
|
|
78
|
+
mapSpread<R>(fn: (...args: T extends readonly unknown[] ? T : never) => R): LazyCollection<R>;
|
|
79
|
+
mapWithKeys<K extends PropertyKey, V>(fn: (item: T, index: number) => readonly [K, V]): Record<K, V>;
|
|
80
|
+
mapToGroups<K extends PropertyKey, V>(fn: (item: T, index: number) => readonly [K, V]): Record<K, V[]>;
|
|
81
|
+
flatMap<R>(fn: (item: T, index: number) => R | readonly R[]): LazyCollection<R>;
|
|
82
|
+
flatten(depth?: number): LazyCollection<unknown>;
|
|
83
|
+
collapse<U>(this: LazyCollection<readonly U[] | U>): LazyCollection<U>;
|
|
84
|
+
pluck(key: string): LazyCollection<unknown>;
|
|
85
|
+
pluck(key: string, keyBy: string): Record<string, unknown>;
|
|
86
|
+
take(count: number): LazyCollection<T>;
|
|
87
|
+
takeUntil(target: T | Predicate<T>): LazyCollection<T>;
|
|
88
|
+
takeWhile(predicate: Predicate<T>): LazyCollection<T>;
|
|
89
|
+
skip(count: number): LazyCollection<T>;
|
|
90
|
+
skipUntil(target: T | Predicate<T>): LazyCollection<T>;
|
|
91
|
+
skipWhile(predicate: Predicate<T>): LazyCollection<T>;
|
|
92
|
+
slice(start: number, length?: number): LazyCollection<T>;
|
|
93
|
+
forPage(page: number, perPage: number): LazyCollection<T>;
|
|
94
|
+
nth(step: number, offset?: number): LazyCollection<T>;
|
|
95
|
+
chunk(size: number): LazyCollection<LazyCollection<T>>;
|
|
96
|
+
chunkWhile(predicate: (item: T, key: number, chunk: readonly T[]) => boolean): LazyCollection<LazyCollection<T>>;
|
|
97
|
+
partition(predicate: Predicate<T>): [LazyCollection<T>, LazyCollection<T>];
|
|
98
|
+
groupBy(by: RetrieverInput<T, PropertyKey | readonly PropertyKey[]>): Record<PropertyKey, T[]>;
|
|
99
|
+
keyBy(by: RetrieverInput<T, PropertyKey>): Record<PropertyKey, T>;
|
|
100
|
+
split(groups: number): LazyCollection<LazyCollection<T>>;
|
|
101
|
+
splitIn(groups: number): LazyCollection<LazyCollection<T>>;
|
|
102
|
+
sort(comparator?: Comparator<T>): LazyCollection<T>;
|
|
103
|
+
sortDesc(): LazyCollection<T>;
|
|
104
|
+
sortBy(spec: ops.SortBySpec<T> | readonly ops.SortBySpec<T>[]): LazyCollection<T>;
|
|
105
|
+
sortByDesc(spec: ops.SortBySpec<T>): LazyCollection<T>;
|
|
106
|
+
sortKeys(): LazyCollection<T>;
|
|
107
|
+
sortKeysDesc(): LazyCollection<T>;
|
|
108
|
+
reverse(): LazyCollection<T>;
|
|
109
|
+
shuffle(random?: () => number): LazyCollection<T>;
|
|
110
|
+
values(): LazyCollection<T>;
|
|
111
|
+
keys(): LazyCollection<string>;
|
|
112
|
+
diff(other: readonly T[] | LazyCollection<T> | Collection<T>): LazyCollection<T>;
|
|
113
|
+
diffAssoc(other: readonly Partial<T>[] | LazyCollection<Partial<T>>): LazyCollection<T>;
|
|
114
|
+
diffKeys(otherKeys: readonly string[]): LazyCollection<T>;
|
|
115
|
+
intersect(other: readonly T[] | LazyCollection<T> | Collection<T>): LazyCollection<T>;
|
|
116
|
+
intersectAssoc(other: readonly Partial<T>[] | LazyCollection<Partial<T>>): LazyCollection<T>;
|
|
117
|
+
intersectByKeys(keys: readonly string[]): LazyCollection<T>;
|
|
118
|
+
union(other: readonly T[] | LazyCollection<T> | Collection<T>): LazyCollection<T>;
|
|
119
|
+
crossJoin<U>(...others: readonly (readonly U[])[]): LazyCollection<(T | U)[]>;
|
|
120
|
+
unique(by?: RetrieverInput<T>): LazyCollection<T>;
|
|
121
|
+
uniqueStrict(by?: RetrieverInput<T>): LazyCollection<T>;
|
|
122
|
+
duplicates(by?: RetrieverInput<T>): Record<number, T>;
|
|
123
|
+
duplicatesStrict(by?: RetrieverInput<T>): Record<number, T>;
|
|
124
|
+
only(keys: readonly string[]): LazyCollection<Partial<T>>;
|
|
125
|
+
except(keys: readonly string[]): LazyCollection<Partial<T>>;
|
|
126
|
+
flip(): LazyCollection<Record<string, number>>;
|
|
127
|
+
pad(size: number, value: T): LazyCollection<T>;
|
|
128
|
+
multiply(factor: number): LazyCollection<T>;
|
|
129
|
+
combine<V>(values: readonly V[] | LazyCollection<V> | Collection<V>): Record<string, V>;
|
|
130
|
+
zip<U>(values: readonly U[] | LazyCollection<U> | Collection<U>): LazyCollection<[T, U | undefined]>;
|
|
131
|
+
concat<U>(other: readonly U[] | LazyCollection<U> | Collection<U>): LazyCollection<T | U>;
|
|
132
|
+
merge<U>(...sources: readonly (readonly U[] | LazyCollection<U> | Collection<U>)[]): LazyCollection<T | U>;
|
|
133
|
+
each(callback: (item: T, index: number) => void | false): this;
|
|
134
|
+
/** Lazy: only fires as items are pulled out of subsequent operators. */
|
|
135
|
+
tapEach(callback: (item: T, index: number) => void): LazyCollection<T>;
|
|
136
|
+
reduce<R>(fn: (carry: R, item: T, index: number) => R, initial: R): R;
|
|
137
|
+
pipe<R>(callback: (collection: this) => R): R;
|
|
138
|
+
pipeInto<R>(Ctor: ClassConstructor<R, [this]>): R;
|
|
139
|
+
pipeThrough(pipes: readonly ((value: unknown) => unknown)[]): unknown;
|
|
140
|
+
tap(callback: (collection: this) => void): this;
|
|
141
|
+
implode(glueOrFormatter: string | ((item: T, index: number) => string), keyOrSeparator?: string): string;
|
|
142
|
+
join(glue: string, finalGlue?: string): string;
|
|
143
|
+
when(condition: boolean | ((c: this) => boolean), callback: (c: this, value: boolean) => this | void, fallback?: (c: this, value: boolean) => this | void): this;
|
|
144
|
+
unless(condition: boolean | ((c: this) => boolean), callback: (c: this, value: boolean) => this | void, fallback?: (c: this, value: boolean) => this | void): this;
|
|
145
|
+
whenEmpty(cb: (c: this) => this | void, fb?: (c: this) => this | void): this;
|
|
146
|
+
whenNotEmpty(cb: (c: this) => this | void, fb?: (c: this) => this | void): this;
|
|
147
|
+
unlessEmpty(cb: (c: this) => this | void): this;
|
|
148
|
+
unlessNotEmpty(cb: (c: this) => this | void): this;
|
|
149
|
+
/**
|
|
150
|
+
* Stop enumerating once `deadline` is reached. `deadline` may be a Date or
|
|
151
|
+
* an absolute epoch-millis number.
|
|
152
|
+
*/
|
|
153
|
+
takeUntilTimeout(deadline: Date | number): LazyCollection<T>;
|
|
154
|
+
/**
|
|
155
|
+
* Async throttle — yields one value every `seconds` seconds. Only useful in
|
|
156
|
+
* an `for await` context: returns an async iterable rather than a sync one.
|
|
157
|
+
*/
|
|
158
|
+
throttle(seconds: number): AsyncIterable<T>;
|
|
159
|
+
/**
|
|
160
|
+
* Memoize values that have already been pulled. Subsequent iterations replay
|
|
161
|
+
* them from cache rather than re-running the source generator.
|
|
162
|
+
*/
|
|
163
|
+
remember(): LazyCollection<T>;
|
|
164
|
+
/**
|
|
165
|
+
* Invoke `callback` every `interval` (a Date or epoch-ms duration) while
|
|
166
|
+
* enumerating. Useful for extending locks / posting heartbeats.
|
|
167
|
+
*/
|
|
168
|
+
withHeartbeat(intervalMs: number, callback: () => void): LazyCollection<T>;
|
|
169
|
+
static macro: MacroableTarget['macro'];
|
|
170
|
+
static hasMacro: MacroableTarget['hasMacro'];
|
|
171
|
+
static flushMacros: MacroableTarget['flushMacros'];
|
|
172
|
+
private materialise;
|
|
173
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Collection } from '../../collection/Collection';
|
|
2
|
+
export { LazyCollection } from '../../collection/LazyCollection';
|
|
3
|
+
export type { LazySource } from '../../collection/LazyCollection';
|
|
4
|
+
export { createHigherOrderProxy, HIGHER_ORDER_TARGETS } from '../../collection/HigherOrderProxy';
|
|
5
|
+
export type { HigherOrderTarget } from '../../collection/HigherOrderProxy';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Arrayable } from '../../contracts/Arrayable';
|
|
2
|
+
import { Jsonable } from '../../contracts/Jsonable';
|
|
3
|
+
/**
|
|
4
|
+
* Shared contract between Collection<T> and LazyCollection<T>.
|
|
5
|
+
* Methods that must exist on both forms — mirrors Laravel's
|
|
6
|
+
* `Illuminate\Support\Enumerable` interface.
|
|
7
|
+
*
|
|
8
|
+
* Methods that mutate the underlying source (push/pop/shift/etc.) are
|
|
9
|
+
* intentionally excluded — they only exist on the eager Collection.
|
|
10
|
+
*/
|
|
11
|
+
export interface Enumerable<T> extends Iterable<T>, Arrayable<T>, Jsonable {
|
|
12
|
+
count(): number;
|
|
13
|
+
isEmpty(): boolean;
|
|
14
|
+
isNotEmpty(): boolean;
|
|
15
|
+
toArray(): T[];
|
|
16
|
+
toJson(): string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { Arrayable } from '../../contracts/Arrayable';
|
|
2
|
+
export { isArrayable } from '../../contracts/Arrayable';
|
|
3
|
+
export type { Jsonable } from '../../contracts/Jsonable';
|
|
4
|
+
export type { Enumerable } from '../../contracts/Enumerable';
|
|
5
|
+
export type { MacroFn, MacroableStatic } from '../../contracts/Macroable';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CollectionException } from '../../exceptions/CollectionException';
|
|
2
|
+
export { ItemNotFoundException } from '../../exceptions/ItemNotFoundException';
|
|
3
|
+
export { MultipleItemsFoundException } from '../../exceptions/MultipleItemsFoundException';
|
|
4
|
+
export { UnexpectedValueException } from '../../exceptions/UnexpectedValueException';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { collect } from '../helpers/collect';
|
|
2
|
+
export { Collection } from '../collection/Collection';
|
|
3
|
+
export { LazyCollection } from '../collection/LazyCollection';
|
|
4
|
+
export { AsyncCollection } from '../async/AsyncCollection';
|
|
5
|
+
export type { AsyncSource } from '../async/AsyncCollection';
|
|
6
|
+
export { mapWithConcurrency } from '../async/concurrent';
|
|
7
|
+
export { collect } from '../helpers/collect';
|
|
8
|
+
export { lazy } from '../helpers/lazy';
|
|
9
|
+
export { parseCsv, toCsv } from '../io/csv';
|
|
10
|
+
export type { CsvParseOptions, CsvSerializeOptions } from '../io/csv';
|
|
11
|
+
export { parseJsonl, toJsonl, parseJsonlStream } from '../io/jsonl';
|
|
12
|
+
export { fromReadable, lines } from '../io/streams';
|
|
13
|
+
export type { FromReadableOptions, ReadableLike } from '../io/streams';
|
|
14
|
+
export { createHigherOrderProxy, HIGHER_ORDER_TARGETS } from '../collection/HigherOrderProxy';
|
|
15
|
+
export type { HigherOrderTarget } from '../collection/HigherOrderProxy';
|
|
16
|
+
export { registerMacro, hasMacro, getMacro, flushMacros, applyMacroable } from '../macros/Macroable';
|
|
17
|
+
export type { MacroableTarget } from '../macros/Macroable';
|
|
18
|
+
export type { Arrayable, Jsonable, Enumerable, MacroFn, MacroableStatic } from '../contracts';
|
|
19
|
+
export { isArrayable } from '../contracts';
|
|
20
|
+
export { CollectionException, ItemNotFoundException, MultipleItemsFoundException, UnexpectedValueException } from '../exceptions';
|
|
21
|
+
export type { PlainObject, Scalar, Comparable, Operator, SortDirection, Predicate, Iteratee, Comparator, ClassConstructor } from '../support/types';
|
|
22
|
+
export type { RetrieverInput } from '../support/valueRetriever';
|
|
23
|
+
export type { LazySource } from '../collection/LazyCollection';
|
|
24
|
+
export * as operations from '../operations';
|
|
25
|
+
export { dataGet, dataSet } from '../support/dataGet';
|
|
26
|
+
export { deepEqual, looseEqual } from '../support/deepEqual';
|
|
27
|
+
export { deepClone } from '../support/deepClone';
|
|
28
|
+
export { valueRetriever } from '../support/valueRetriever';
|
|
29
|
+
export { operatorForWhere, isOperator } from '../support/operatorForWhere';
|
|
30
|
+
export { isPlainObject, isObjectLike, isFunction } from '../support/isObject';
|
|
31
|
+
export default collect;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface CsvParseOptions {
|
|
2
|
+
delimiter?: string;
|
|
3
|
+
/** Treat the first row as the header. When true, returns objects keyed by header. */
|
|
4
|
+
header?: boolean;
|
|
5
|
+
/** Quote character — `"` by default. Pass empty string to disable quoting. */
|
|
6
|
+
quote?: string;
|
|
7
|
+
/** When true, all values stay as strings; otherwise simple numbers/bools are coerced. */
|
|
8
|
+
raw?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface CsvSerializeOptions {
|
|
11
|
+
/** Optional explicit column order. When omitted, keys are taken from the first row. */
|
|
12
|
+
columns?: readonly string[];
|
|
13
|
+
delimiter?: string;
|
|
14
|
+
/** EOL sequence — defaults to \n. Use \r\n if Excel compat matters. */
|
|
15
|
+
eol?: string;
|
|
16
|
+
/** Include the header row at the top of the output. Default true when input is objects. */
|
|
17
|
+
header?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* RFC 4180-compatible CSV parser. Handles quoted fields with embedded
|
|
21
|
+
* delimiters, escaped quotes (`""`), and \r\n / \n line endings. Returns
|
|
22
|
+
* either string[][] (when `header` is false) or `Record<string, string>[]`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseCsv(input: string, options?: CsvParseOptions): string[][] | Record<string, string | number | boolean | null>[];
|
|
25
|
+
/**
|
|
26
|
+
* Serialize an array of objects (or rows) to a CSV string. Values that
|
|
27
|
+
* contain the delimiter, a quote, or a newline are quoted; embedded quotes
|
|
28
|
+
* are escaped by doubling.
|
|
29
|
+
*/
|
|
30
|
+
export declare function toCsv(rows: ReadonlyArray<Record<string, unknown>> | ReadonlyArray<readonly unknown[]>, options?: CsvSerializeOptions): string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { parseCsv, toCsv } from '../../io/csv';
|
|
2
|
+
export type { CsvParseOptions, CsvSerializeOptions } from '../../io/csv';
|
|
3
|
+
export { parseJsonl, toJsonl, parseJsonlStream } from '../../io/jsonl';
|
|
4
|
+
export { fromReadable, lines } from '../../io/streams';
|
|
5
|
+
export type { FromReadableOptions, ReadableLike } from '../../io/streams';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse newline-delimited JSON. Empty lines are skipped. Each line must be
|
|
3
|
+
* a complete JSON value; otherwise a SyntaxError is thrown with the line
|
|
4
|
+
* number for diagnostics. The original parse error is attached as `cause`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseJsonl<T = unknown>(input: string): T[];
|
|
7
|
+
export declare function toJsonl<T>(items: ReadonlyArray<T>, eol?: string): string;
|
|
8
|
+
/** Parse a JSONL stream incrementally, yielding one object per line. */
|
|
9
|
+
export declare function parseJsonlStream<T = unknown>(source: AsyncIterable<string>): AsyncGenerator<T>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AsyncCollection } from '../../async/AsyncCollection';
|
|
2
|
+
/**
|
|
3
|
+
* Adapter for any Node `Readable` (or ReadableStream) that implements
|
|
4
|
+
* `AsyncIterable`. Buffers are decoded with UTF-8 by default; binary streams
|
|
5
|
+
* pass `decodeAs: undefined` to keep raw chunks.
|
|
6
|
+
*/
|
|
7
|
+
export interface ReadableLike<T> {
|
|
8
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
9
|
+
}
|
|
10
|
+
export interface FromReadableOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Decode each chunk as text using the given encoding, or pass `false` to
|
|
13
|
+
* keep raw byte chunks (yields stringified buffers — caller likely wants a
|
|
14
|
+
* different signature in that case). Defaults to `'utf-8'`.
|
|
15
|
+
*/
|
|
16
|
+
decodeAs?: 'utf8' | 'utf-8' | 'ascii' | 'latin1' | false;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Wrap a Node `Readable` (or any `AsyncIterable<Buffer | Uint8Array | string>`)
|
|
20
|
+
* into an `AsyncCollection<string>`. Pass `{decodeAs: false}` to skip decoding.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fromReadable(source: ReadableLike<Buffer | Uint8Array | string>, options?: FromReadableOptions): AsyncCollection<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Convert an arbitrary chunk stream into a line-by-line `AsyncCollection<string>`.
|
|
25
|
+
* Handles \n and \r\n; trailing chunk without newline is emitted as the last line.
|
|
26
|
+
*/
|
|
27
|
+
export declare function lines(source: ReadableLike<Buffer | Uint8Array | string>): AsyncCollection<string>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MacroFn } from '../../contracts/Macroable';
|
|
2
|
+
/**
|
|
3
|
+
* A reference to a class. We accept both regular and abstract constructors
|
|
4
|
+
* because some abstract base classes may also be macroable.
|
|
5
|
+
*/
|
|
6
|
+
export type MacroableClass = abstract new (...args: never[]) => unknown;
|
|
7
|
+
export declare function registerMacro(target: MacroableClass, name: string, fn: MacroFn): void;
|
|
8
|
+
export declare function hasMacro(target: MacroableClass, name: string): boolean;
|
|
9
|
+
export declare function getMacro(target: MacroableClass, name: string): MacroFn | undefined;
|
|
10
|
+
export declare function flushMacros(target: MacroableClass): void;
|
|
11
|
+
/**
|
|
12
|
+
* Mix the static `macro`, `hasMacro`, and `flushMacros` methods onto a class.
|
|
13
|
+
* Used by `Collection` and `LazyCollection`. Avoids inheritance gymnastics.
|
|
14
|
+
*/
|
|
15
|
+
export interface MacroableTarget {
|
|
16
|
+
flushMacros(): void;
|
|
17
|
+
hasMacro(name: string): boolean;
|
|
18
|
+
macro(name: string, fn: MacroFn): void;
|
|
19
|
+
}
|
|
20
|
+
export declare function applyMacroable<C extends MacroableClass>(target: C): asserts target is C & MacroableTarget;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { registerMacro, hasMacro, getMacro, flushMacros, applyMacroable, type MacroableTarget } from '../../macros/Macroable';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Operator, Predicate } from '../../support/types';
|
|
2
|
+
export declare function firstOf<T>(items: readonly T[], predicate?: Predicate<T>): T | undefined;
|
|
3
|
+
export declare function firstOrFailOf<T>(items: readonly T[], predicate?: Predicate<T>): T;
|
|
4
|
+
export interface FirstWhereSpec {
|
|
5
|
+
operator: Operator;
|
|
6
|
+
/** Truthy check on the key when both operator and value are absent. */
|
|
7
|
+
truthy: boolean;
|
|
8
|
+
value: unknown;
|
|
9
|
+
}
|
|
10
|
+
export declare function buildFirstWhereSpec(operatorOrValue: Operator | unknown, value: unknown, argCount: number): FirstWhereSpec;
|
|
11
|
+
export declare function firstWhereOf<T>(items: readonly T[], key: keyof T | string, spec: FirstWhereSpec): T | undefined;
|
|
12
|
+
export declare function lastOf<T>(items: readonly T[], predicate?: Predicate<T>): T | undefined;
|
|
13
|
+
export declare function getAt<T>(items: readonly T[], index: number, defaultValue?: T): T | undefined;
|
|
14
|
+
export declare function valueOfFirst<T, R = unknown>(items: readonly T[], key: string): R | undefined;
|
|
15
|
+
export declare function soleOf<T>(items: readonly T[], predicate?: Predicate<T> | keyof T | string, expected?: unknown): T;
|
|
16
|
+
export declare function afterOf<T>(items: readonly T[], target: T | string | Predicate<T>, strict?: boolean): T | undefined;
|
|
17
|
+
export declare function beforeOf<T>(items: readonly T[], target: T | string | Predicate<T>, strict?: boolean): T | undefined;
|
|
18
|
+
export declare function hasKey<T>(items: readonly T[], keys: readonly (keyof T | string)[]): boolean;
|
|
19
|
+
export declare function hasAnyKey<T>(items: readonly T[], keys: readonly (keyof T | string)[]): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RetrieverInput } from '../../support/valueRetriever';
|
|
2
|
+
export declare function sumOf<T>(items: readonly T[], by?: RetrieverInput<T, number>): number;
|
|
3
|
+
export declare function averageOf<T>(items: readonly T[], by?: RetrieverInput<T, number>): number;
|
|
4
|
+
export declare function maxOf<T, R = number>(items: readonly T[], by?: RetrieverInput<T, R>): R | undefined;
|
|
5
|
+
export declare function minOf<T, R = number>(items: readonly T[], by?: RetrieverInput<T, R>): R | undefined;
|
|
6
|
+
export declare function medianOf<T>(items: readonly T[], by?: RetrieverInput<T, number>): number | undefined;
|
|
7
|
+
export declare function modeOf<T>(items: readonly T[], by?: RetrieverInput<T, unknown>): unknown[] | undefined;
|
|
8
|
+
export declare function percentageOf<T>(items: readonly T[], predicate: (item: T, index: number) => boolean, precision?: number): number;
|
|
9
|
+
export declare function countByOf<T>(items: readonly T[], by?: RetrieverInput<T, unknown>): Map<unknown, number>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Predicate } from '../../support/types';
|
|
2
|
+
export declare function chunkOf<T>(items: readonly T[], size: number): T[][];
|
|
3
|
+
/**
|
|
4
|
+
* Group consecutive items that satisfy the predicate. The predicate receives
|
|
5
|
+
* the current item, its index, and the chunk built so far (to mirror Laravel).
|
|
6
|
+
*/
|
|
7
|
+
export declare function chunkWhileOf<T>(items: readonly T[], predicate: (item: T, key: number, chunk: readonly T[]) => boolean): T[][];
|
|
8
|
+
export declare function slidingOf<T>(items: readonly T[], size: number, step?: number): T[][];
|
|
9
|
+
export declare function splitOf<T>(items: readonly T[], groups: number): T[][];
|
|
10
|
+
export declare function splitInOf<T>(items: readonly T[], groups: number): T[][];
|
|
11
|
+
export declare function partitionOf<T>(items: readonly T[], predicate: Predicate<T>): [T[], T[]];
|
|
12
|
+
export declare function forPageOf<T>(items: readonly T[], page: number, perPage: number): T[];
|
|
13
|
+
export declare function nthOf<T>(items: readonly T[], step: number, offset?: number): T[];
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function combineOf<K extends PropertyKey, V>(keys: readonly K[], values: readonly V[]): Record<K, V>;
|
|
2
|
+
export declare function zipOf<T, U>(items: readonly T[], values: readonly U[]): [T, U | undefined][];
|
|
3
|
+
/** Multi-source zip producing tuples of length N. Stops at the shortest input. */
|
|
4
|
+
export declare function zipManyOf<T>(...sources: readonly (readonly T[])[]): T[][];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Predicate } from '../../support/types';
|
|
2
|
+
export type ContainsArg<T> = T | Predicate<T> | Partial<T> | {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type ContainsSpec<T> = {
|
|
6
|
+
kind: 'predicate';
|
|
7
|
+
predicate: Predicate<T>;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'shape';
|
|
10
|
+
shape: Record<string, unknown>;
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'keyValue';
|
|
13
|
+
key: string;
|
|
14
|
+
value: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'value';
|
|
17
|
+
value: unknown;
|
|
18
|
+
};
|
|
19
|
+
/** Loose match unless `strict` is true. */
|
|
20
|
+
export declare function containsOf<T>(items: readonly T[], spec: ContainsSpec<T>, strict?: boolean): boolean;
|
|
21
|
+
export declare function doesntContainOf<T>(items: readonly T[], spec: ContainsSpec<T>, strict?: boolean): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve user-supplied arguments into a normalised ContainsSpec.
|
|
24
|
+
* Used by both Collection.contains/doesntContain and their lazy counterparts.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveContainsSpec<T>(target: ContainsArg<T>, value: unknown, hasValue: boolean): ContainsSpec<T>;
|
|
27
|
+
export declare function everyOf<T>(items: readonly T[], predicate: Predicate<T>): boolean;
|
|
28
|
+
export declare function someOf<T>(items: readonly T[], predicate: Predicate<T>): boolean;
|
|
29
|
+
export declare function searchOf<T>(items: readonly T[], target: T | Predicate<T>, strict?: boolean): number | false;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type WhenCondition<C> = boolean | ((carrier: C) => boolean);
|
|
2
|
+
export declare function resolveCondition<C>(carrier: C, condition: WhenCondition<C>): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Functional `when`: invoke the matching callback and return whatever it
|
|
5
|
+
* returns, or the carrier itself when the callback returns void/undefined.
|
|
6
|
+
*/
|
|
7
|
+
export declare function whenOf<C>(carrier: C, condition: WhenCondition<C>, truthy: (carrier: C, value: boolean) => C | void, falsy?: (carrier: C, value: boolean) => C | void): C;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ClassConstructor, Operator, Predicate } from '../../support/types';
|
|
2
|
+
export declare function filterOf<T>(items: readonly T[], predicate?: Predicate<T>): T[];
|
|
3
|
+
export declare function rejectOf<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
|
4
|
+
export interface WhereSpec {
|
|
5
|
+
operator: Operator;
|
|
6
|
+
strict: boolean;
|
|
7
|
+
truthy: boolean;
|
|
8
|
+
value: unknown;
|
|
9
|
+
}
|
|
10
|
+
export declare function buildWhereSpec(args: readonly unknown[], strict: boolean): WhereSpec;
|
|
11
|
+
export declare function whereOf<T>(items: readonly T[], key: string, spec: WhereSpec): T[];
|
|
12
|
+
export declare function whereInOf<T>(items: readonly T[], key: string, values: readonly unknown[], strict?: boolean): T[];
|
|
13
|
+
export declare function whereNotInOf<T>(items: readonly T[], key: string, values: readonly unknown[], strict?: boolean): T[];
|
|
14
|
+
export declare function whereBetweenOf<T>(items: readonly T[], key: string, range: readonly [unknown, unknown]): T[];
|
|
15
|
+
export declare function whereNotBetweenOf<T>(items: readonly T[], key: string, range: readonly [unknown, unknown]): T[];
|
|
16
|
+
export declare function whereNullOf<T>(items: readonly T[], key: string): T[];
|
|
17
|
+
export declare function whereNotNullOf<T>(items: readonly T[], key: string): T[];
|
|
18
|
+
export declare function whereInstanceOfOf<T, R>(items: readonly T[], Ctor: ClassConstructor<R> | (abstract new (...args: never[]) => R)): R[];
|
|
19
|
+
/**
|
|
20
|
+
* Compile an SQL-`LIKE`-style pattern into an anchored RegExp. `%` matches any
|
|
21
|
+
* run of characters (including none) and `_` matches exactly one; every other
|
|
22
|
+
* character is matched literally. Matching is case-insensitive unless
|
|
23
|
+
* `caseSensitive` is set.
|
|
24
|
+
*/
|
|
25
|
+
export declare function likeToRegExp(pattern: string, caseSensitive?: boolean): RegExp;
|
|
26
|
+
/** Keep items whose `key` value matches an SQL-`LIKE` pattern (or its inverse). */
|
|
27
|
+
export declare function whereLikeOf<T>(items: readonly T[], key: string, pattern: string, caseSensitive?: boolean, negate?: boolean): T[];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { RetrieverInput } from '../../support/valueRetriever';
|
|
2
|
+
export declare function groupByOf<T, K extends PropertyKey = PropertyKey>(items: readonly T[], by: RetrieverInput<T, K | readonly K[]>): Record<K, T[]>;
|
|
3
|
+
/** groupByMany: apply multiple grouping functions left-to-right, producing nested groups. */
|
|
4
|
+
export declare function groupByManyOf<T>(items: readonly T[], groupers: readonly RetrieverInput<T>[]): Record<PropertyKey, unknown>;
|
|
5
|
+
export declare function keyByOf<T, K extends PropertyKey = PropertyKey>(items: readonly T[], by: RetrieverInput<T, K>): Record<K, T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from '../../operations/accessors';
|
|
2
|
+
export * from '../../operations/aggregations';
|
|
3
|
+
export * from '../../operations/chunking';
|
|
4
|
+
export * from '../../operations/compare';
|
|
5
|
+
export * from '../../operations/combine';
|
|
6
|
+
export * from '../../operations/conditionals';
|
|
7
|
+
export * from '../../operations/filters';
|
|
8
|
+
export * from '../../operations/grouping';
|
|
9
|
+
export * from '../../operations/itertools';
|
|
10
|
+
export * from '../../operations/joins';
|
|
11
|
+
export * from '../../operations/mutations';
|
|
12
|
+
export * from '../../operations/objectOps';
|
|
13
|
+
export * from '../../operations/random';
|
|
14
|
+
export * from '../../operations/reducers';
|
|
15
|
+
export * from '../../operations/sequence';
|
|
16
|
+
export * from '../../operations/setOps';
|
|
17
|
+
export * from '../../operations/sliceOps';
|
|
18
|
+
export * from '../../operations/sorting';
|
|
19
|
+
export * from '../../operations/stats';
|
|
20
|
+
export * from '../../operations/strings';
|
|
21
|
+
export * from '../../operations/transformations';
|
|
22
|
+
export * from '../../operations/uniqueness';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduce that emits intermediate accumulator states. Useful for cumulative
|
|
3
|
+
* sums, state machines, and deriving "running" series.
|
|
4
|
+
*/
|
|
5
|
+
export declare function scanOf<T, R>(items: readonly T[], fn: (carry: R, item: T, index: number) => R, initial: R): R[];
|
|
6
|
+
/** Yield consecutive pairs: `[a, b, c, d]` → `[[a,b],[b,c],[c,d]]`. */
|
|
7
|
+
export declare function pairwiseOf<T>(items: readonly T[]): [T, T][];
|
|
8
|
+
/** Tag each item with its index: `[a, b]` → `[[0,a],[1,b]]`. */
|
|
9
|
+
export declare function enumerateOf<T>(items: readonly T[], start?: number): [number, T][];
|
|
10
|
+
/**
|
|
11
|
+
* Repeat the input `n` times. If `n` is `Infinity`, returns a generator that
|
|
12
|
+
* never terminates — caller must `.take()` to bound it.
|
|
13
|
+
*/
|
|
14
|
+
export declare function cycleOf<T>(items: readonly T[], n?: number): Generator<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Round-robin interleave of multiple sources: zips the heads, then the
|
|
17
|
+
* second-positions, etc. Stops at the shortest source.
|
|
18
|
+
*/
|
|
19
|
+
export declare function interleaveOf<T>(...sources: readonly (readonly T[])[]): T[];
|
|
20
|
+
/**
|
|
21
|
+
* Split a single iterable into `n` independent iterables that can each be
|
|
22
|
+
* consumed independently. The shared underlying iterator is buffered as
|
|
23
|
+
* needed — memory grows with the gap between fastest and slowest consumer.
|
|
24
|
+
*/
|
|
25
|
+
export declare function teeOf<T>(source: Iterable<T>, n: number): Generator<T>[];
|
|
26
|
+
/**
|
|
27
|
+
* All r-length permutations (without repetition). For an n-element input
|
|
28
|
+
* with r = n this generates n! sequences. Order matches Python's itertools.
|
|
29
|
+
*/
|
|
30
|
+
export declare function permutationsOf<T>(items: readonly T[], r?: number): Generator<T[]>;
|
|
31
|
+
/**
|
|
32
|
+
* All r-length combinations (without repetition). For r = n this yields a
|
|
33
|
+
* single combination — the input itself.
|
|
34
|
+
*/
|
|
35
|
+
export declare function combinationsOf<T>(items: readonly T[], r: number): Generator<T[]>;
|
|
36
|
+
/** Power set — all 2^n subsets, in increasing-size order. */
|
|
37
|
+
export declare function powerSetOf<T>(items: readonly T[]): Generator<T[]>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RetrieverInput } from '../../support/valueRetriever';
|
|
2
|
+
export type JoinResult<L, R, M> = M extends (left: L, right: R) => infer U ? U : [L, R];
|
|
3
|
+
/**
|
|
4
|
+
* Inner join: emit one row per matching pair on `leftKey` / `rightKey`. If a
|
|
5
|
+
* `merge` function is supplied its return value is emitted; otherwise tuples
|
|
6
|
+
* `[left, right]` are produced.
|
|
7
|
+
*/
|
|
8
|
+
export declare function joinOnOf<L, R, K extends PropertyKey, M = [L, R]>(left: readonly L[], right: readonly R[], leftKey: RetrieverInput<L, K>, rightKey: RetrieverInput<R, K>, merge?: (l: L, r: R) => M): M[];
|
|
9
|
+
/**
|
|
10
|
+
* Left join: each left row appears at least once. When no right match exists,
|
|
11
|
+
* `right` is `undefined` (or supplied to `merge` as undefined).
|
|
12
|
+
*/
|
|
13
|
+
export declare function leftJoinOf<L, R, K extends PropertyKey, M = [L, R | undefined]>(left: readonly L[], right: readonly R[], leftKey: RetrieverInput<L, K>, rightKey: RetrieverInput<R, K>, merge?: (l: L, r: R | undefined) => M): M[];
|
|
14
|
+
/** Right join — symmetrical to leftJoin. */
|
|
15
|
+
export declare function rightJoinOf<L, R, K extends PropertyKey, M = [L | undefined, R]>(left: readonly L[], right: readonly R[], leftKey: RetrieverInput<L, K>, rightKey: RetrieverInput<R, K>, merge?: (l: L | undefined, r: R) => M): M[];
|
|
16
|
+
/** Full outer join — every left and right row appears at least once. */
|
|
17
|
+
export declare function outerJoinOf<L, R, K extends PropertyKey, M = [L | undefined, R | undefined]>(left: readonly L[], right: readonly R[], leftKey: RetrieverInput<L, K>, rightKey: RetrieverInput<R, K>, merge?: (l: L | undefined, r: R | undefined) => M): M[];
|