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.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.after(item => item === 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.after(item => item === 4);
// null
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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.afterOr(-1, item => item === 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.afterOr(-1, item => item === 4);
// -1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.afterOr(() => -1, item => item === 4);
// -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.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.afterOrFail(item => item === 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.afterOrFail(item => item === 4);
// error is thrown
The append method adds iterable to the end of the collection.
The average method returns the average of all items in the collection. If the collection includes other than number items an error will be thrown. If the collection is empty an error will also be thrown.
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.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.before(item => item === 2);
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.before(item => item === 1);
// null
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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.beforeOr(-1, item => item === 2);
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.beforeOr(-1, item => item === 1);
// -1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.beforeOr(() => -1, item => item === 1);
// -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.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.beforeOrFail(item => item === 2);
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.beforeOrFail(item => item === 1);
// error is thrown
The change method changes only the items that passes predicateFn using mapFn.
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.
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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection("AABBCCCD");
const chunks = collection.chunkWhile((value, index, chunk) => {
return value === chunk.last();
});
chunks.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 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.
Optional
selectFn: Map<TInput, ICollection<TInput>, TOutput>The crossJoin method cross joins the collection's values among iterables, returning a Cartesian product with all possible permutations.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2]);
const matrix = collection.cross(["a", "b"]);
matrix.toArray();
// [
// [1, "a"],
// [1, "b"],
// [2, "a"],
// [2, "b"],
// ]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2]);
const matrix = collection.cross(["a", "b"]).cross(["I", "II"]);
matrix.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.
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 2, 3, 4, 5]);
const difference = collection.difference([2, 4, 6, 8]);
difference.toArray();
// [1, 3, 5]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([
{ 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" },
]);
const difference = collection.difference(
[
{ name: "Apple Watch", brand: "Apple", type: "watch" },
],
(product) => product.type
);
difference.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 ICollection of key, value pairs for every entry in the collection.
The filter method filters the collection using predicateFn, keeping only those items that pass predicateFn.
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.
Optional
predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.first();
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.first(item => item > 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.first(item => item > 10);
// null
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 .
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOr(-1);
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOr(-1, item => item > 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOr(-1, item => item > 10);
// -1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOr(() => -1, 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.
Optional
predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOrFail();
// 1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOrFail(item => item > 2);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.firstOrFail(item => item > 10);
// throws an error
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.
The forEach method iterates through all items in the collection.
The groupBy method groups the collection's items by selectFn . By default the equality check occurs on the item.
Optional
selectFn: Map<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["a", "a", "a", "b", "b", "c"]);
const group = collection
.groupBy()
.map(([key, collection]) => [key, collection.toArray()])
.toArray();
// [
// [
// "a",
// ["a", "a", "a"]
// ],
// [
// "b",
// ["b", "b"]
// ],
// [
// "c",
// ["c"]
// ]
// ]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["alice@gmail.com", "bob@yahoo.com", "carlos@gmail.com"]);
const group = collection
.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.
The insertBefore method adds iterable before the first item that matches predicateFn.
The join method joins the collection's items with separator . An error will be thrown when if a none string item is encounterd.
Optional
separator: stringThe keys method returns an ICollection 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.
Optional
predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.last();
// 4
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.last(item => item < 4);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.last(item => item > 10);
// null
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 .
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOr(-1);
// 4
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOr(-1, item => item < 4);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOr(-1, item => item > 10);
// -1
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOr(() => -1, 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.
Optional
predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOrFail();
// 4
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOrFail(item => item < 4);
// 3
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4]);
collection.lastOrFail(item => item > 10);
// throws an error
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 max method returns the max of all items in the collection. If the collection includes other than number items an error will be thrown. If the collection is empty an error will also be thrown.
The median method returns the median of all items in the collection. If the collection includes other than number items an error will be thrown. If the collection is empty an error will also be thrown.
The min method returns the min of all items in the collection. If the collection includes other than number items an error will be thrown. If the collection is empty an error will also be thrown.
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 { ListCollection } from "@daiso-tech/core";;
new ListCollection("abc").padEnd(10, "foo").join("");
// "abcfoofoof"
new ListCollection("abc").padEnd(6, "123465").join("");
// "abc123"
new ListCollection("abc").padEnd(8, "0").join("");
// "abc00000"
new ListCollection("abc").padEnd(1, "_").join("");
// "abc"
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 { ListCollection } from "@daiso-tech/core";;
new ListCollection("abc").padStart(10, "foo").join("");
// "foofoofabc"
new ListCollection("abc").padStart(6, "123465").join("");
// "123abc"
new ListCollection("abc").padStart(8, "0").join("");
// "00000abc"
new ListCollection("abc").padStart(1, "_").join("");
// "abc"
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.
The percentage method may be used to quickly determine the percentage of items in the collection that pass predicateFn. If the collection is empty an error will also be thrown.
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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, "2", "a", 1, 3, {}]);
function toNbrs<TInput>(
collection: ICollection<TInput>,
): ICollection<number> {
return collection
.map((item) => Number(item))
.reject((nbr) => Number.isNaN(nbr));
}
function nbrToStr(collection: ICollection<number>): number[] {
return collection.repeat(2).toArray();
}
const piped = collection.pipe(toNbrs).pipe(nbrToStr);
console.log(piped);
// [ 1, 2, 1, 3 ]
The prepend method adds iterable to the beginning of the collection.
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.
The reject method filters the collection using predicateFn, keeping only those items that not pass predicateFn.
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 .
Optional
chunkSize: 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 shuffle method randomly shuffles the items in the collection. You can provide a custom Math.random function by passing in mathRandom.
Optional
mathRandom: (() => number)The skip method skips the first offset items.
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.
Optional
start: numberOptional
end: numberimport { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
collection.slice(3).toArray();
// ["d", "e", "f"]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
collection.slice(undefined, 2).toArray();
// ["a", "b"]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
collection.slice(2, 5).toArray();
// ["c", "d", "e"]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
collection.slice(-2).toArray();
// ["e", "f"]
The sliding method returns a new collection of chunks representing a "sliding window" view of the items in the collection.
Optional
step: numberThe 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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4, 5]);
collection.sole(item => item === 4);
// 4
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4, 4, 5]);
collection.sole(item => item === 4);
// error is thrown
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 5]);
collection.sole(item => item === 4);
// error is thrown
The sort method sorts the collection. You can provide a comparator function.
Optional
comparator: Comparator<TInput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([-1, 2, 4, 3]);
collection.sort().toArray();
// [-1, 2, 3, 4]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([
{ name: "Anders", age: 30 },
{ name: "Joe", age: 20 },
{ name: "Hasan", age: 25 },
{ name: "Linda", age: 19 }
]);
collection.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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 2, 3, 4, 5]);
const chunks = collection.split(3);
chunks.map(chunk => chunk.toArray()).toArray();
// [[1, 2], [3, 4], [5]]
The sum method returns the sum of all items in the collection. If the collection includes other than number items an error will be thrown. If the collection is empty an error will also be thrown.
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.
The unique method removes all duplicate values from the collection by selectFn . By default the equality check occurs on the item.
Optional
selectFn: Map<TInput, ICollection<TInput>, TOutput>import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([1, 1, 2, 2, 3, 4, 2]);
collection.unique().toArray();
// [1, 2, 3, 4]
import { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection([
{ 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" },
]);
const unique = collection.unique({
selectFn: item => item.brand
}).toArray();
// [
// { name: "iPhone 6", brand: "Apple", type: "phone" },
// { name: "Galaxy S6", brand: "Samsung", type: "phone" },
// ]
The values method returns a copy of the collection.
The when method will execute callback when condition evaluates to true.
The whenEmpty method will execute callback when the collection is empty.
The whenNot method will execute callback when condition evaluates to false.
The whenNotEmpty method will execute callback when the collection is not empty.
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 { ListCollection } from "@daiso-tech/core";;
const collection = new ListCollection(["Chair", "Desk"]);
const zipped = collection.zip([100, 200]);
zipped.toArray();
// [["Chair", 100], ["Desk", 200]]
ICollection is immutable.