@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
247 lines (246 loc) • 13.2 kB
TypeScript
/**
* @module Collection
*/
import type { EnsureMap, EnsureRecord } from "../../../collection/contracts/_module-exports.js";
import { type Collapse, type Comparator, type PredicateInvokable, type ICollection, type Map, type Modifier, type Tap, type Transform, type Reduce, type ForEach, type CrossJoinResult } from "../../../collection/contracts/_module-exports.js";
import { type Lazyable } from "../../../utilities/_module-exports.js";
/**
* All methods in `ListCollection` are executed eagerly.
*
* IMPORT_PATH: `"@daiso-tech/core/collection"`
* @group Adapters
*/
export declare class ListCollection<TInput = unknown> implements ICollection<TInput> {
/**
* The `concat` static method is a convenient utility for easily concatenating multiple {@link Iterable | `Iterable`}.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* class MyIterable implements Iterable<number> {
* *[Symbol.iterator](): Iterator<number> {
* yield 1;
* yield 2;
* yield 3;
* }
* }
*
* const collection = ListCollection.concat([
* new MyIterable(),
* new Set([1, 2, 3]),
* new Map([["a", 1], ["b", 2]]),
* ["a", "b", "c"]
* ]);
* collection.toArray();
* // [1, 2, 3, 1, 2, 3, ["a", 1], ["b", 2], "a", "b", "c"]
* ```
*/
static concat<TValue>(iterables: Iterable<Iterable<TValue>>): ICollection<TValue>;
/**
* The `difference` static method is used to compute the difference between two {@link Iterable | `Iterable`} instances. By default, the equality check is performed on each item.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = ListCollection.difference(
* [1, 2, 2, 3, 4, 5],
* [2, 4, 6, 8]
* );
* collection.toArray();
* // [1, 3, 5]
* ```
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = ListCollection.difference(
* [
* { name: "iPhone 6", brand: "Apple", type: "phone" },
* { name: "iPhone 5", brand: "Apple", type: "phone" },
* { name: "Apple Watch", brand: "Apple", type: "watch" },
* { name: "Galaxy S6", brand: "Samsung", type: "phone" },
* { name: "Galaxy Gear", brand: "Samsung", type: "watch" },
* ],
* [
* { name: "Apple Watch", brand: "Apple", type: "watch" },
* ],
* (product) => product.type
* );
* collection.toArray();
* // [
* // { name: "iPhone 6", brand: "Apple", type: "phone" },
* // { name: "iPhone 5", brand: "Apple", type: "phone" },
* // { name: "Galaxy S6", brand: "Samsung", type: "phone" },
* // ]
* ```
*/
static difference<TValue, TSelect>(iterableA: Iterable<TValue>, iterableB: Iterable<TValue>, selectFn?: Map<TValue, ICollection<TValue>, TSelect>): ICollection<TValue>;
/**
* The `zip` static method merges together the values of `iterableA` with the values of the `iterableB` at their corresponding index.
* The returned collection has size of the shortest collection.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";;
*
* const collection = ListCollection.zip(["Chair", "Desk"], [100, 200]);
* collection.toArray();
* // [["Chair", 100], ["Desk", 200]]
* ```
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";;
*
* const collection = ListCollection.zip(["Chair", "Desk", "Couch"], [100, 200]);
* collection.toArray();
* // [["Chair", 100], ["Desk", 200]]
* ```
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";;
*
* const collection = ListCollection.zip(["Chair", "Desk"], [100, 200, 300]);
* collection.toArray();
* // [["Chair", 100], ["Desk", 200]]
* ```
*/
static zip<TValueA, TValueB>(iterableA: Iterable<TValueA>, iterableB: Iterable<TValueB>): ICollection<[TValueA, TValueB]>;
static deserialize<TInput>(serializedValue: TInput[]): ICollection<TInput>;
private array;
/**
* The `constructor` takes an {@link Iterable | `Iterable`}.
*
* Works with `Array`.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = new ListCollection([1, 2, 3, 4]);
* ```
*
* Works with `String`.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = new ListCollection("ABCDE");
* ```
*
* Works with `Set`.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = new ListCollection(new Set([1, 2, 2 4]));
* ```
*
* Works with `Map`.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* const collection = new ListCollection(new Map([["a", 1], ["b", 2]]));
* ```
*
* Works with any `Iterable`.
* @example
* ```ts
* import { ListCollection } from "@daiso-tech/core";
*
* class MyIterable implements Iterable<number> {
* *[Symbol.iterator](): Iterator<number> {
* yield 1;
* yield 2;
* yield 3;
* }
* }
* const collection = new ListCollection(new MyIterable());
* ```
*/
constructor(iterable?: Iterable<TInput>);
serialize(): TInput[];
[Symbol.iterator](): Iterator<TInput>;
toIterator(): Iterator<TInput, void>;
entries(): ICollection<[number, TInput]>;
keys(): ICollection<number>;
values(): ICollection<TInput>;
filter<TOutput extends TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): ICollection<TOutput>;
reject<TOutput extends TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): ICollection<Exclude<TInput, TOutput>>;
map<TOutput>(mapFn: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TOutput>;
reduce(reduceFn: Reduce<TInput, ICollection<TInput>, TInput>): TInput;
reduce(reduceFn: Reduce<TInput, ICollection<TInput>, TInput>, initialValue: TInput): TInput;
reduce<TOutput>(reduceFn: Reduce<TInput, ICollection<TInput>, TOutput>, initialValue: TOutput): TOutput;
join(separator?: string): Extract<TInput, string>;
collapse(): ICollection<Collapse<TInput>>;
flatMap<TOutput>(mapFn: Map<TInput, ICollection<TInput>, Iterable<TOutput>>): ICollection<TOutput>;
change<TFilterOutput extends TInput, TMapOutput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<TInput | TFilterOutput | TMapOutput>;
set(index: number, value: TInput | Map<TInput, ICollection<TInput>, TInput>): ICollection<TInput>;
get(index: number): TInput | null;
getOrFail(index: number): TInput;
page(page: number, pageSize: number): ICollection<TInput>;
sum(): Extract<TInput, number>;
average(): Extract<TInput, number>;
median(): Extract<TInput, number>;
min(): Extract<TInput, number>;
max(): Extract<TInput, number>;
percentage(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): number;
some<TOutput extends TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): boolean;
every<TOutput extends TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): boolean;
take(limit: number): ICollection<TInput>;
takeUntil(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<TInput>;
takeWhile(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<TInput>;
skip(offset: number): ICollection<TInput>;
skipUntil(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<TInput>;
skipWhile(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<TInput>;
when<TExtended = TInput>(condition: boolean, callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
whenEmpty<TExtended = TInput>(callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
whenNot<TExtended = TInput>(condition: boolean, callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
whenNotEmpty<TExtended = TInput>(callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
pipe<TOutput = TInput>(callback: Transform<ICollection<TInput>, TOutput>): TOutput;
tap(callback: Tap<ICollection<TInput>>): ICollection<TInput>;
chunk(chunkSize: number): ICollection<ICollection<TInput>>;
chunkWhile(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<ICollection<TInput>>;
split(chunkAmount: number): ICollection<ICollection<TInput>>;
partition(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): ICollection<ICollection<TInput>>;
sliding(chunkSize: number, step?: number): ICollection<ICollection<TInput>>;
groupBy<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<[TOutput, ICollection<TInput>]>;
countBy<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<[TOutput, number]>;
unique<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TInput>;
difference<TOutput = TInput>(iterable: Iterable<TInput>, selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TInput>;
repeat(amount: number): ICollection<TInput>;
padStart<TExtended = TInput>(maxLength: number, fillItems: Iterable<TExtended>): ICollection<TInput | TExtended>;
padEnd<TExtended = TInput>(maxLength: number, fillItems: Iterable<TExtended>): ICollection<TInput | TExtended>;
slice(start?: number, end?: number): ICollection<TInput>;
prepend<TExtended = TInput>(iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
append<TExtended = TInput>(iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
insertBefore<TExtended = TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>, iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
insertAfter<TExtended = TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>, iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
crossJoin<TExtended>(iterable: Iterable<TExtended>): ICollection<CrossJoinResult<TInput, TExtended>>;
zip<TExtended>(iterable: Iterable<TExtended>): ICollection<[TInput, TExtended]>;
sort(comparator?: Comparator<TInput>): ICollection<TInput>;
reverse(_chunkSize?: number): ICollection<TInput>;
shuffle(mathRandom?: () => number): ICollection<TInput>;
first<TOutput extends TInput>(predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput | null;
firstOr<TOutput extends TInput, TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput | TExtended;
firstOrFail<TOutput extends TInput>(predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput;
last<TOutput extends TInput>(predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput | null;
lastOr<TOutput extends TInput, TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput | TExtended;
lastOrFail<TOutput extends TInput>(predicateFn?: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput;
before(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput | null;
beforeOr<TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput | TExtended;
beforeOrFail(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput;
after(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput | null;
afterOr<TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput | TExtended;
afterOrFail(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): TInput;
sole<TOutput extends TInput>(predicateFn: PredicateInvokable<TInput, ICollection<TInput>, TOutput>): TOutput;
nth(step: number): ICollection<TInput>;
count(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): number;
size(): number;
isEmpty(): boolean;
isNotEmpty(): boolean;
searchFirst(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): number;
searchLast(predicateFn: PredicateInvokable<TInput, ICollection<TInput>>): number;
forEach(callback: ForEach<TInput, ICollection<TInput>>): void;
toArray(): TInput[];
toRecord(): EnsureRecord<TInput>;
toMap(): EnsureMap<TInput>;
}