The after method returns the item that comes after the first item that matches predicateFn.
If the collection is empty or the predicateFn does not match or matches the last item then null is returned.
The afterOr method returns the item that comes after the first item that matches predicateFn.
If the collection is empty or the predicateFn does not match or matches the last item then defaultValue is returned.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.afterOr(-1, item => item === 2);
// 3
}
You can pass a function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.afterOr(() => -1, item => item === 4);
// -1
}
You can pass an async function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.afterOr(async () => -1, item => item === 4);
// -1
}
You can pass a Task as default value.
import type { IAsyncCollection, ICache } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>, cache: ICache<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.afterOr(cache.get("a"), item => item > 10);
// -1
}
The afterOrFail method returns the item that comes after the first item that matches predicateFn.
If the collection is empty or the predicateFn does not match or matches the last item then an error is thrown.
The append method adds iterable to the end of the collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.append([-1, -2])
.toArray();
// [1, 2, 3, 4, 5, -1, -2,]
}
The before method returns the item that comes before the first item that matches predicateFn.
If the predicateFn does not match or matches the first item then null is returned.
The beforeOr method returns the item that comes before the first item that matches predicateFn.
If the collection is empty or the predicateFn does not match or matches the first item then defaultValue is returned.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.beforeOr(-1, item => item === 2);
// 1
}
You can pass a function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.beforeOr(() => -1, item => item === 1);
// -1
}
You can pass an async function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.beforeOr(async () => -1, item => item === 1);
// -1
}
You can pass a Task as default value.
import type { IAsyncCollection, ICache } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>, cache: ICache<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.beforeOr(cache.get("a"), item => item > 10);
// -1
}
The beforeOrFail method returns the item that comes before the first item that matches predicateFn.
If the collection is empty or the predicateFn does not match or matches the first item then an error is thrown.
The change method changes only the items that passes predicateFn using mapFn.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.change(item => item % 2 === 0, item => item * 2)
.toArray();
// [1, 4, 3, 8, 5]
}
The chunk method breaks the collection into multiple, smaller collections of size chunkSize.
If chunkSize is not divisible with total number of items then the last chunk will contain the remaining items.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6, 7])
.chunk(4)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2, 3, 4], [5, 6, 7]]
}
The chunkWhile method breaks the collection into multiple, smaller collections based on the evaluation of predicateFn.
The chunk variable passed to the predicateFn may be used to inspect the previous item.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append("AABBCCCD")
.chunkWhile((value, index, chunk) => {
return value === chunk.last();
})
.map(chunk => chunk.toArray())
.toArray();
// [["A", "A"], ["B", "B"], ["C", "C", "C"], ["D"]]
}
The collapse method collapses a collection of iterables into a single, flat collection.
The copy method returns a copy of the collection.
The count method returns the total number of items in the collection that passes predicateFn.
The countBy method counts the occurrences of values in the collection by selectFn.
By default the equality check occurs on the item.
OptionalselectFn: AsyncMap<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append(["a", "a", "a", "b", "b", "c"])
.countBy()
.map(([key, collection]) => [key, collection.toArray()])
.toArray();
// [
// ["a", 3],
// ["b", 2],
// ["c", 1]
// ]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["alice@gmail.com", "bob@yahoo.com", "carlos@gmail.com"])
.countBy(item => item.split("@")[1])
.toArray();
// [
// ["gmail.com", 2],
// ["yahoo.com", 1]
// ]
}
The crossJoin method cross joins the collection's values among iterables, returning a Cartesian product with all possible permutations.
import { ICollection } from "@daiso-tech/core/collection/contracts";
async function(): Promise<void> {
await collection
.append([1, 2])
.cross(["a", "b"])
.toArray();
// [
// [1, "a"],
// [1, "b"],
// [2, "a"],
// [2, "b"],
// ]
}
import { ICollection } from "@daiso-tech/core/collection/contracts";
async function(): Promise<void> {
await collection
.append([1, 2])
.cross(["a", "b"])
.cross(["I", "II"])
.toArray();
// [
// [1, "a", "I"],
// [1, "a", "II"],
// [1, "b", "I"],
// [1, "b", "II"],
// [2, "a", "I"],
// [2, "a", "II"],
// [2, "b", "I"],
// [2, "b", "II"],
// ]
}
The difference method will return the values in the original collection that are not present in iterable.
By default the equality check occurs on the item.
OptionalselectFn: AsyncMap<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 2, 3, 4, 5])
.difference([2, 4, 6, 8])
.toArray();
// [1, 3, 5]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
type Phone = {
name: string;
brand: string;
type: string;
};
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<Phone>): Promise<void> {
await collection
.append([
{ 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" },
])
.difference(
[
{ name: "Apple Watch", brand: "Apple", type: "watch" },
],
(product) => product.type
)
.toArray();
// [
// { name: "iPhone 6", brand: "Apple", type: "phone" },
// { name: "iPhone 5", brand: "Apple", type: "phone" },
// { name: "Galaxy S6", brand: "Samsung", type: "phone" },
// ]
}
The entries returns an IAsyncCollection of key, value pairs for every entry in the collection.
The every method determines whether all items in the collection matches predicateFn.
The filter method filters the collection using predicateFn, keeping only those items that pass predicateFn.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6])
.filter(item => 2 < item && item < 5)
.toArray();
// [3, 4]
}
The first method returns the first item in the collection that passes predicateFn.
By default it will get the first item. If the collection is empty or no items passes predicateFn than null i returned.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.first();
// 1
}
The firstOr method returns the first item in the collection that passes predicateFn
By default it will get the first item. If the collection is empty or no items passes predicateFn than defaultValue.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOr(-1);
// 1
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOr(-1, item => item > 2);
// 3
}
You can pass a function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOr(() => -1, item => item > 10);
// -1
}
You can pass an async function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOr(async () => -1, item => item > 10);
// -1
}
You can pass a Task as default value.
import type { IAsyncCollection, ICache } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>, cache: ICache<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOr(cache.get("a"), item => item > 10);
// -1
}
The firstOrFail method returns the first item in the collection that passes predicateFn.
By default it will get the first item. If the collection is empty or no items passes predicateFn than error is thrown.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.firstOrFail();
// 1
}
The flatMap method returns a new array formed by applying mapFn to each item of the array, and then collapses the result by one level.
It is identical to a map method followed by a collapse method.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string[]>): Promise<void> {
await collection
.append([["a", "b"], ["c", "d"]])
.flatMap(item => [item.length, ...item])
.toArray();
// [2, "a", "b", 2, "c", "d"]
}
The forEach method iterates through all items in the collection.
The get method returns the item by index. If the item is not found null will returned.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
collection = collection.append([1, 4, 2, 8, -2]);
// Will be 2
await collection.get(2);
// Will be null
await collection.get(5);
}
The getOrFail method returns the item by index. If the item is not found an error will be thrown.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
collection = collection.append([1, 4, 2, 8, -2]);
// Will be 2
await collection.getOrFail(2);
// An error will thrown
await collection.getOrFail(5);
}
The groupBy method groups the collection's items by selectFn.
By default the equality check occurs on the item.
OptionalselectFn: AsyncMap<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "a", "a", "b", "b", "c"])
.groupBy()
.map(([key, collection]) => [key, collection.toArray()])
.toArray();
// [
// [
// "a",
// ["a", "a", "a"]
// ],
// [
// "b",
// ["b", "b"]
// ],
// [
// "c",
// ["c"]
// ]
// ]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["alice@gmail.com", "bob@yahoo.com", "carlos@gmail.com"])
.groupBy(item => item.split("@")[1])
.map(([key, collection]) => [key, collection.toArray()])
.toArray();
// [
// [
// "gmail.com",
// ["alice@gmail.com", "carlos@gmail.com"]
// ],
// [
// "yahoo.com",
// ["bob@yahoo.com"]
// ]
// ]
}
The insertAfter method adds iterable after the first item that matches predicateFn.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 2, 3, 4, 5])
.insertAfter(item => item === 2, [-1, 20])
.toArray();
// [1, 2, -1, 20, 2, 3, 4, 5]
}
The insertBefore method adds iterable before the first item that matches predicateFn.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 2, 3, 4, 5])
.insertBefore(item => item === 2, [-1, 20])
.toArray();
// [1, -1, 20, 2, 2, 3, 4, 5]
}
The keys method returns an IAsyncCollection of keys in the collection.
The last method returns the last item in the collection that passes predicateFn.
By default it will get the last item. If the collection is empty or no items passes predicateFn than null i returned.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.last();
// 4
}
The lastOr method returns the last item in the collection that passes predicateFn.
By default it will get the last item. If the collection is empty or no items passes predicateFn than defaultValue.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOr(-1);
// 4
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOr(-1, item => item < 4);
// 3
}
You can pass a function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOr(() => -1, item => item > 10);
// -1
}
You can pass an async function as default value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOr(async () => -1, item => item > 10);
// -1
}
You can pass a Task as default value.
import type { IAsyncCollection, ICache } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>, cache: ICache<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOr(cache.get("a"), item => item > 10);
// -1
}
The lastOrFail method returns the last item in the collection that passes predicateFn.
By default it will get the last item. If the collection is empty or no items passes predicateFn than error is thrown.
OptionalpredicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.lastOrFail();
// 4
}
The map method iterates through the collection and passes each item to mapFn.
The mapFn is free to modify the item and return it, thus forming a new collection of modified items.
The nth method creates a new collection consisting of every n-th item.
The padEnd method pads this collection with fillItems until the resulting collection size reaches maxLength.
The padding is applied from the end of this collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append("abc")
.padEnd(10, "foo")
.join("");
// "abcfoofoof"
}
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append("abc")
.padEnd(6, "123465")
.join("");
// "abc123"
}
The padStart method pads this collection with fillItems until the resulting collection size reaches maxLength.
The padding is applied from the start of this collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append("abc")
.padStart(10, "foo")
.join("");
// "foofoofabc"
}
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append("abc")
.padStart(6, "123465")
.join("");
// "123abc"
}
The page method returns a new collection containing the items that would be present on page with custom pageSize.
The partition method is used to separate items that pass predicateFn from those that do not.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6])
.partition(item => item < 3)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2], [3, 4, 5, 6]]
}
The percentage method may be used to quickly determine the percentage of items in the collection that pass predicateFn.
The pipe method passes the orignal collection to callback and returns the result from callback.
This method is useful when you want compose multiple smaller functions.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
function toNbrs<TInput>(
collection: IAsyncCollection<TInput>,
): IAsyncCollection<number> {
return collection
.map((item) => Number(item))
.reject((nbr) => Number.isNaN(nbr));
}
function nbrToStr(collection: IAsyncCollection<number>): number[] {
return collection.repeat(2).toArray();
}
await collection
.append([1, "2", "a", 1, 3, {}])
.pipe(toNbrs)
.then(nbrToStr);
// [ 1, 2, 1, 3 ]
}
The prepend method adds iterable to the beginning of the collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.prepend([-1, 20])
.toArray();
// [-1, 20, 1, 2, 3, 4, 5]
}
The reduce method executes reduceFn function on each item of the array, passing in the return value from the calculation on the preceding item.
The final result of running the reducer across all items of the array is a single value.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append([1, 2, 3])
.reduce((sum, item) => sum + item);
// 6
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c"])
.entries()
.reduce(
(record, [key, value]) => ({
...record,
[key]: value
}),
{} as Record<number, string>
);
// { 0: "a", 1: "b", 2: "c" }
}
The reject method filters the collection using predicateFn, keeping only those items that not pass predicateFn.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6])
.reject(item => 2 < item && item < 5)
.toArray();
// [1, 2, 5, 6]
}
The repeat method will repeat the original collection amount times.
The reverse method will reverse the order of the collection.
The reversing of the collection will be applied in chunks that are the size of chunkSize.
OptionalchunkSize: numberThe searchFirst return the index of the first item that matches predicateFn.
The searchLast return the index of the last item that matches predicateFn.
The set method changes a item by i>indexusingvalue`.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.set(1, -1)
.toArray();
// [1, -1, 3, 4, 5]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.set(1, (prevValue) => prevValue - 2)
.toArray();
// [1, 0, 3, 4, 5]
}
The shuffle method randomly shuffles the items in the collection. You can provide a custom Math.random function by passing in mathRandom.
OptionalmathRandom: () => numberThe skip method skips the first offset items.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.skip(4)
.toArray();
// [5, 6, 7, 8, 9, 10]
}
The skipUntil method skips items until predicateFn returns true.
The skipWhile method skips items until predicateFn returns false.
The slice method creates porition of the original collection selected from start and end
where start and end (end not included) represent the index of items in the collection.
Optionalstart: numberOptionalend: numberimport type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c", "d", "e", "f"])
.slice(3)
.toArray();
// ["d", "e", "f"]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c", "d", "e", "f"])
.slice(undefined, 2)
.toArray();
// ["a", "b"]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c", "d", "e", "f"])
.slice(2, 5)
.toArray();
// ["c", "d", "e"]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c", "d", "e", "f"])
.slice(-2)
.toArray();
// ["e", "f"]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["a", "b", "c", "d", "e", "f"])
.slice(undefined, -2)
.toArray();
// ["a", "b", "c", "d"]
}
The sliding method returns a new collection of chunks representing a "sliding window" view of the items in the collection.
Optionalstep: numberimport type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.sliding(2)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2], [2, 3], [3, 4], [4, 5]]
}
The sole method returns the first item in the collection that passes predicateFn, but only if predicateFn matches exactly one item.
If no items matches or multiple items are found an error will be thrown.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.sole(item => item === 4);
// 4
}
The some method determines whether at least one item in the collection matches predicateFn.
The sort method sorts the collection. You can provide a comparator function.
Optionalcomparator: Comparator<TInput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
type Person = {
name: string;
age: number;
};
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<Person>): Promise<void> {
await collection
.append([-1, 2, 4, 3])
.sort()
.toArray();
// [-1, 2, 3, 4]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([
{ name: "Anders", age: 30 },
{ name: "Joe", age: 20 },
{ name: "Hasan", age: 25 },
{ name: "Linda", age: 19 }
])
.sort(({ age: ageA }, { age: ageB }) => ageA - ageB)
.toArray();
// [
// { name: "Linda", age: 19 }
// { name: "Joe", age: 20 },
// { name: "Hasan", age: 25 },
// { name: "Anders", age: 30 },
// ]
}
The split method breaks a collection evenly into chunkAmount of chunks.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5])
.split(3)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2], [3, 4], [5]]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6])
.split(3)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2], [3, 4], [5, 6]]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6, 7])
.split(3)
.map(chunk => chunk.toArray())
.toArray();
// [[1, 2, 7], [3, 4], [5, 6]]
The take method takes the first limit items.
The takeUntil method takes items until predicateFn returns true.
The takeWhile method takes items until predicateFn returns false.
The tap method passes a copy of the original collection to callback, allowing you to do something with the items while not affecting the original collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4, 5, 6])
.tap(collection => {
collection
.filter(value => value % 2 === 0)
.forEach(value => console.log(value))
})
.toArray();
// [1, 2, 3, 4, 5, 6]
}
The toRecord method converts the collection to a new Record | Record.
An error will be thrown if item is not a tuple of size 2 where the first element is a string or a number.
The unique method removes all duplicate values from the collection by selectFn.
By default the equality check occurs on the item.
OptionalselectFn: AsyncMap<TInput, IAsyncCollection<TInput>, TOutput>import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
type Phone = {
name: string;
brand: string;
type: string;
};
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<Phone>): Promise<void> {
await collection
.append([1, 1, 2, 2, 3, 4, 2])
.unique()
.toArray();
// [1, 2, 3, 4]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([
{ 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" },
])
.unique(item => item.brand)
.toArray();
// [
// { name: "iPhone 6", brand: "Apple", type: "phone" },
// { name: "Galaxy S6", brand: "Samsung", type: "phone" },
// ]
}
The validate method filters all items that matches the schema and transforms them afterwards.
The schema can be any standard schema compliant object.
The when method will execute callback when condition evaluates to true.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.when(true, collection => collection.append([-3]))
.when(false, collection => collection.append([20]))
.toArray();
// [1, 2, 3, 4, -3]
}
The whenEmpty method will execute callback when the collection is empty.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([])
.whenEmpty(collection => collection.append([-3]))
.toArray();
// [-3]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1])
.whenEmpty(collection => collection.append([-3]))
.toArray();
// [1]
}
The whenNot method will execute callback when condition evaluates to false.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1, 2, 3, 4])
.whenNot(true, collection => collection.append([-3]))
.whenNot(false, collection => collection.append([20]))
.toArray();
// [1, 2, 3, 4, 20]
}
The whenNotEmpty method will execute callback when the collection is not empty.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([])
.whenNotEmpty(collection => collection.append([-3])).toArray();
// []
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<number>): Promise<void> {
await collection
.append([1])
.whenNotEmpty(collection => collection.append([-3]))
.toArray();
// [1, -3]
}
The zip method merges together the values of iterable with the values of the collection at their corresponding index.
The returned collection has size of the shortest collection.
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["Chair", "Desk"])
.zip([100, 200])
.toArray();
// [["Chair", 100], ["Desk", 200]]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["Chair", "Desk", "Couch"])
.zip([100, 200])
.toArray();
// [["Chair", 100], ["Desk", 200]]
}
import type { IAsyncCollection } from "@daiso-tech/core/collection/contracts";
// Asume the inputed collection is empty.
async function main(collection: IAsyncCollection<string>): Promise<void> {
await collection
.append(["Chair", "Desk"])
.zip([100, 200, 300])
.toArray();
// [["Chair", 100], ["Desk", 200]]
}
The
IAsyncCollectioncontract offers a fluent and efficient approach to working with AsyncIterable objects.IAsyncCollectionis immutable.IMPORT_PATH:
"@daiso-tech/core/collection/contracts"