Collections
TypeScript Port
This documentation is automatically synced from Laravel's official documentation with PHP examples converted to TypeScript. Some methods may have TypeScript-specific behavior noted below.
Introduction
The Collection class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We'll use the collect helper to create a new collection instance from the array, convert each element to uppercase, and then remove all empty elements:
const collection = collect(['Taylor', 'Abigail', null])
.map(name => name?.toUpperCase())
.reject(name => !name);As you can see, the Collection class allows you to chain its methods to perform fluent mapping and reducing of the underlying array. In general, collections are immutable, meaning every Collection method returns an entirely new Collection instance.
Creating Collections
As mentioned above, the collect helper returns a new Collection instance for the given array. So, creating a collection is as simple as:
const collection = collect([1, 2, 3]);You may also create a collection using the make and fromJson methods.
Extending Collections
Collections are "macroable", which allows you to add additional methods to the Collection class at runtime. The Collection.macro() static method accepts a name and a function that will be executed when your macro is called. The macro function may access the collection's methods via this, just as if it were a real method of the collection class. For example, the following code adds a toUpper method to the Collection class:
import { Collection, collect } from 'collect-ts';
// Register the macro
Collection.macro('toUpper', function(this: Collection<string>) {
return this.map((value) => value.toUpperCase());
});
// Use the macro (with type assertion for TypeScript)
const collection = collect(['first', 'second']);
const upper = (collection as Collection<string> & { toUpper: () => Collection<string> }).toUpper();
console.log(upper.all()); // ['FIRST', 'SECOND']Typically, you should declare collection macros during your application's initialization.
Macro Arguments
If necessary, you may define macros that accept additional arguments:
Collection.macro('multiply', function(this: Collection<number>, factor: number) {
return this.map((value) => value * factor);
});
const collection = collect([1, 2, 3]);
const multiplied = (collection as Collection<number> & { multiply: (n: number) => Collection<number> })
.multiply(10);
console.log(multiplied.all()); // [10, 20, 30]Macro Management
You can check if a macro exists and flush all macros:
// Check if a macro exists
Collection.hasMacro('toUpper'); // true
// Remove all registered macros (useful for testing)
Collection.flushMacros();
Collection.hasMacro('toUpper'); // falseAvailable Methods
For the majority of the remaining collection documentation, we'll discuss each method available on the Collection class. Remember, all of these methods may be chained to fluently manipulate the underlying array. Furthermore, almost every method returns a new Collection instance, allowing you to preserve the original copy of the collection when necessary:
afterallaverageavgbeforechunkchunkWhilecollapsecollapseWithKeyscollectcombineconcatcontainscontainsOneItemcontainsStrictcountcountBycrossJoindddiffdiffAssocdiffAssocUsingdiffKeysdoesntContaindoesntContainStrictdotdumpduplicatesduplicatesStricteacheachSpreadensureeveryexceptfilterfirstfirstOrFailfirstWhereflatMapflattenflipforgetforPagefromJsongetgroupByhashasAnyimplodeintersectintersectUsingintersectAssocintersectAssocUsingintersectByKeysisEmptyisNotEmptyjoinkeyBykeyslastlazymacromakemapmapIntomapSpreadmapToDictionarymapToGroupsmapWithKeysmaxmedianmergemergeRecursiveminmodemultiplynthonlypadpartitionpercentagepipepipeIntopipeThroughpluckpopprependpullpushputrandomrangereducereduceSpreadrejectreplacereplaceRecursivereversesearchselectshiftshuffleskipskipUntilskipWhilesliceslidingsolesomesortsortBysortByDescsortDescsortKeyssortKeysDescsortKeysUsingsplicesplitsplitInsumtaketakeUntiltakeWhiletaptimestoArraytoJsontoPrettyJsontransformundotunionuniqueuniqueStrictunlessunlessEmptyunlessNotEmptyunwrapvaluevalueswhenwhenEmptywhenNotEmptywherewhereStrictwhereBetweenwhereInwhereInStrictwhereInstanceOfwhereNotBetweenwhereNotInwhereNotInStrictwhereNotNullwhereNullwrapzip
Method Listing
after()
The after method returns the item after the given item. null is returned if the given item is not found or is the last item:
collect([1, 2, 3, 4, 5]).after(3);
// 4
collect([1, 2, 3, 4, 5]).after(5);
// nullThis method searches for the given item using "loose" comparison, meaning a string containing an integer value will be considered equal to an integer of the same value. To use "strict" comparison, you may provide the strict argument to the method:
collect([2, 4, 6, 8]).after('4', true);
// nullAlternatively, you may provide your own closure to search for the first item that passes a given truth test:
collect([2, 4, 6, 8]).after((item, key) => item > 5);
// 8all()
The all method returns the underlying array represented by the collection:
collect([1, 2, 3]).all();
// [1, 2, 3]average()
Alias for the avg method.
avg()
The avg method returns the average value of a given key:
const average = collect([
{ foo: 10 },
{ foo: 10 },
{ foo: 20 },
{ foo: 40 }
]).avg('foo');
// 20
const average = collect([1, 1, 2, 4]).avg();
// 2before()
The before method is the opposite of the after method. It returns the item before the given item. null is returned if the given item is not found or is the first item:
collect([1, 2, 3, 4, 5]).before(3);
// 2
collect([1, 2, 3, 4, 5]).before(1);
// null
collect([2, 4, 6, 8]).before('4', true);
// null
collect([2, 4, 6, 8]).before((item, key) => item > 5);
// 4chunk()
The chunk method breaks the collection into multiple, smaller collections of a given size:
const chunks = collect([1, 2, 3, 4, 5, 6, 7]).chunk(4);
chunks.all();
// [[1, 2, 3, 4], [5, 6, 7]]This method is especially useful when working with a grid system such as Bootstrap. For example, imagine you have a collection of objects you want to display in a grid:
<template>
<div v-for="(chunk, index) in products.chunk(3).all()" :key="index" class="row">
<div v-for="product in chunk.all()" :key="product.name" class="col-xs-4">
{{ product.name }}
</div>
</div>
</template>
<script setup lang="ts">
import { collect } from 'collect-ts';
const products = collect([
{ name: 'Widget' }, { name: 'Gadget' }, { name: 'Gizmo' },
{ name: 'Tool' }, { name: 'Device' }
]);
</script>chunkWhile()
The chunkWhile method breaks the collection into multiple, smaller collections based on the evaluation of the given callback. The chunk parameter passed to the callback may be used to inspect the previous element:
const chunks = collect('AABBCCCD'.split('')).chunkWhile((value, key, chunk) => value === chunk.last());
chunks.all();
// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]collapse()
The collapse method collapses a collection of arrays or collections into a single, flat collection:
const collapsed = collect([
[1, 2, 3], [4, 5, 6], [7, 8, 9]
]).collapse();
collapsed.all();
// [1, 2, 3, 4, 5, 6, 7, 8, 9]collapseWithKeys()
The collapseWithKeys method flattens a collection of arrays or collections into a single collection, keeping the original keys intact. If the collection is already flat, it will return an empty collection:
const collapsed = collect({
first: collect([1, 2, 3]),
second: [4, 5, 6],
third: collect([7, 8, 9])
}).collapseWithKeys();
collapsed.all();
// {
// first: [1, 2, 3],
// second: [4, 5, 6],
// third: [7, 8, 9]
// }collect()
The collect method returns a new Collection instance with the items currently in the collection:
const collectionA = collect([1, 2, 3]);
const collectionB = collectionA.collect();
collectionB.all();
// [1, 2, 3]The collect method is primarily useful for converting lazy collections into standard Collection instances:
const lazyCollection = LazyCollection.make(function*() {
yield 1;
yield 2;
yield 3;
});
const collection = lazyCollection.collect();
// Collection class
collection.all();
// [1, 2, 3]NOTE
The collect method is especially useful when you have an instance of Enumerable and need a non-lazy collection instance. Since collect() is part of the Enumerable contract, you can safely use it to get a Collection instance.
combine()
The combine method combines the values of the collection, as keys, with the values of another array or collection:
const combined = collect(['name', 'age']).combine(['George', 29]);
combined.all();
// { name: 'George', age: 29 }concat()
The concat method appends the given array or collection's values onto the end of another collection:
const concatenated = collect(['John Doe']).concat(['Jane Doe']).concat({ name: 'Johnny Doe' });
concatenated.all();
// ['John Doe', 'Jane Doe', 'Johnny Doe']The concat method numerically reindexes keys for items concatenated onto the original collection. To maintain keys in associative collections, see the merge method.
contains()
The contains method determines whether the collection contains a given item. You may pass a closure to the contains method to determine if an element exists in the collection matching a given truth test:
collect([1, 2, 3, 4, 5]).contains((value, key) => value > 5);
// falseAlternatively, you may pass a string to the contains method to determine whether the collection contains a given item value:
collect({ name: 'Desk', price: 100 }).contains('Desk');
// true
collect({ name: 'Desk', price: 100 }).contains('New York');
// falseYou may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:
collect([{ product: 'Desk', price: 200 }, { product: 'Chair', price: 100 }]).contains('product', 'Bookcase');
// falseThe contains method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the containsStrict method to filter using "strict" comparisons.
For the inverse of contains, see the doesntContain method.
containsOneItem()
The containsOneItem method determines whether the collection contains a single item:
collect([]).containsOneItem();
// false
collect(['1']).containsOneItem();
// true
collect(['1', '2']).containsOneItem();
// false
collect([1, 2, 3]).containsOneItem(item => item === 2);
// truecontainsStrict()
This method has the same signature as the contains method; however, all values are compared using "strict" comparisons.
count()
The count method returns the total number of items in the collection:
collect([1, 2, 3, 4]).count();
// 4countBy()
The countBy method counts the occurrences of values in the collection. By default, the method counts the occurrences of every element, allowing you to count certain "types" of elements in the collection:
const counted = collect([1, 2, 2, 2, 3]).countBy();
counted.all();
// { 1: 1, 2: 3, 3: 1 }You may pass a closure to the countBy method to count all items by a custom value:
const counted = collect(['alice@gmail.com', 'bob@yahoo.com', 'carlos@gmail.com']).countBy(email => email.split('@')[1]);
counted.all();
// { 'gmail.com': 2, 'yahoo.com': 1 }crossJoin()
The crossJoin method cross joins the collection's values among the given arrays or collections, returning a Cartesian product with all possible permutations:
collect([1, 2]).crossJoin(['a', 'b']).all();
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
collect([1, 2]).crossJoin(['a', 'b'], ['I', 'II']).all();
/*
[
[1, 'a', 'I'],
[1, 'a', 'II'],
[1, 'b', 'I'],
[1, 'b', 'II'],
[2, 'a', 'I'],
[2, 'a', 'II'],
[2, 'b', 'I'],
[2, 'b', 'II'],
]
*/dd()
The dd method dumps the collection's items and ends execution of the script:
collect(['John Doe', 'Jane Doe']).dd();
/*
[
"John Doe",
"Jane Doe"
]
*/If you do not want to stop executing the script, use the dump method instead.
diff()
The diff method compares the collection against another collection or a plain JavaScript array based on its values. This method will return the values in the original collection that are not present in the given collection:
const diff = collect([1, 2, 3, 4, 5]).diff([2, 4, 6, 8]);
diff.all();
// [1, 3, 5]diffAssoc()
The diffAssoc method compares the collection against another collection or a plain JavaScript object based on its keys and values. This method will return the key / value pairs in the original collection that are not present in the given collection:
const diff = collect({
color: 'orange',
type: 'fruit',
remain: 6
}).diffAssoc({
color: 'yellow',
type: 'fruit',
remain: 3,
price: 6
});
diff.all();
// { color: 'orange', remain: 6 }diffAssocUsing()
Unlike diffAssoc, diffAssocUsing accepts a user supplied callback function for the indices comparison:
const diff = collect({
color: 'orange',
type: 'fruit',
remain: 6
}).diffAssocUsing(
{ Color: 'yellow', type: 'fruit', remain: 3 },
(a, b) => a.toLowerCase().localeCompare(b.toLowerCase())
);
diff.all();
// { color: 'orange', remain: 6 }The callback must be a comparison function that returns an integer less than, equal to, or greater than zero.
diffKeys()
The diffKeys method compares the collection against another collection or a plain JavaScript object based on its keys. This method will return the key / value pairs in the original collection that are not present in the given collection:
const diff = collect({
one: 10,
two: 20,
three: 30,
four: 40,
five: 50
}).diffKeys({
two: 2,
four: 4,
six: 6,
eight: 8
});
diff.all();
// { one: 10, three: 30, five: 50 }doesntContain()
The doesntContain method determines whether the collection does not contain a given item. You may pass a closure to the doesntContain method to determine if an element does not exist in the collection matching a given truth test:
collect([1, 2, 3, 4, 5]).doesntContain((value, key) => value < 5);
// falseAlternatively, you may pass a string to the doesntContain method to determine whether the collection does not contain a given item value:
collect({ name: 'Desk', price: 100 }).doesntContain('Table');
// true
collect({ name: 'Desk', price: 100 }).doesntContain('Desk');
// falseYou may also pass a key / value pair to the doesntContain method, which will determine if the given pair does not exist in the collection:
collect([{ product: 'Desk', price: 200 }, { product: 'Chair', price: 100 }]).doesntContain('product', 'Bookcase');
// trueThe doesntContain method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value.
doesntContainStrict()
This method has the same signature as the doesntContain method; however, all values are compared using "strict" comparisons.
dot()
The dot method flattens a multi-dimensional collection into a single level collection that uses "dot" notation to indicate depth:
const flattened = collect({ products: { desk: { price: 100 } } }).dot();
flattened.all();
// { 'products.desk.price': 100 }dump()
The dump method dumps the collection's items:
collect(['John Doe', 'Jane Doe']).dump();
/*
[
"John Doe",
"Jane Doe"
]
*/If you want to stop executing the script after dumping the collection, use the dd method instead.
duplicates()
The duplicates method retrieves and returns duplicate values from the collection:
collect(['a', 'b', 'a', 'c', 'b']).duplicates();
// { 2: 'a', 4: 'b' }If the collection contains arrays or objects, you can pass the key of the attributes that you wish to check for duplicate values:
collect([{ email: 'abigail@example.com', position: 'Developer' }, { email: 'james@example.com', position: 'Designer' }, { email: 'victoria@example.com', position: 'Developer' }]).duplicates('position');
// { 2: 'Developer' }duplicatesStrict()
This method has the same signature as the duplicates method; however, all values are compared using "strict" comparisons.
each()
The each method iterates over the items in the collection and passes each item to a closure:
collect([1, 2, 3, 4]).each((item, key) => {
// ...
});If you would like to stop iterating through the items, you may return false from your closure:
collect([1, 2, 3, 4]).each((item, key) => {
if (/* condition */) {
return false;
}
});eachSpread()
The eachSpread method iterates over the collection's items, passing each nested item value into the given callback:
collect([['John Doe', 35], ['Jane Doe', 33]]).eachSpread((name, age) => {
// ...
});You may stop iterating through the items by returning false from the callback:
collect([['John Doe', 35], ['Jane Doe', 33]]).eachSpread((name, age) => false);ensure()
The ensure method may be used to verify that all elements of a collection are of a given type or list of types. Otherwise, an UnexpectedValueException will be thrown:
return collect([new User, new User]).ensure(User);
return collect([new User, new Customer]).ensure([User, Customer]);Primitive types such as string, int, float, bool, and array may also be specified:
return collect([1, 2, 3]).ensure('number');WARNING
The ensure method does not guarantee that elements of different types will not be added to the collection at a later time.
every()
The every method may be used to verify that all elements of a collection pass a given truth test:
collect([1, 2, 3, 4]).every((value, key) => value > 2);
// falseIf the collection is empty, the every method will return true:
collect([]).every((value, key) => value > 2);
// trueexcept()
The except method returns all items in the collection except for those with the specified keys:
const filtered = collect({ product_id: 1, price: 100, discount: false }).except(['price', 'discount']);
filtered.all();
// { product_id: 1 }For the inverse of except, see the only method.
filter()
The filter method filters the collection using the given callback, keeping only those items that pass a given truth test:
const filtered = collect([1, 2, 3, 4]).filter((value, key) => value > 2);
filtered.all();
// [3, 4]If no callback is supplied, all entries of the collection that are equivalent to false will be removed:
collect([1, 2, 3, null, false, '', 0, []]).filter().all();
// [1, 2, 3]For the inverse of filter, see the reject method.
first()
The first method returns the first element in the collection that passes a given truth test:
collect([1, 2, 3, 4]).first((value, key) => value > 2);
// 3You may also call the first method with no arguments to get the first element in the collection. If the collection is empty, null is returned:
collect([1, 2, 3, 4]).first();
// 1firstOrFail()
The firstOrFail method is identical to the first method; however, if no result is found, an ItemNotFoundException exception will be thrown:
collect([1, 2, 3, 4]).firstOrFail((value, key) => value > 5);
// Throws ItemNotFoundException...You may also call the firstOrFail method with no arguments to get the first element in the collection. If the collection is empty, an ItemNotFoundException exception will be thrown:
collect([]).firstOrFail();
// Throws ItemNotFoundException...firstWhere()
The firstWhere method returns the first element in the collection with the given key / value pair:
collect([{ name: 'Regena', age: null }, { name: 'Linda', age: 14 }, { name: 'Diego', age: 23 }, { name: 'Linda', age: 84 }]).firstWhere('name', 'Linda');
// { name: 'Linda', age: 14 }You may also call the firstWhere method with a comparison operator:
collect([{ name: 'Regena', age: null }, { name: 'Linda', age: 14 }, { name: 'Diego', age: 23 }, { name: 'Linda', age: 84 }]).firstWhere('age', '>=', 18);
// { name: 'Diego', age: 23 }Like the where method, you may pass one argument to the firstWhere method. In this scenario, the firstWhere method will return the first item where the given item key's value is "truthy":
collect([{ name: 'Regena', age: null }, { name: 'Linda', age: 14 }, { name: 'Diego', age: 23 }, { name: 'Linda', age: 84 }]).firstWhere('age');
// { name: 'Linda', age: 14 }flatMap()
The flatMap method iterates through the collection and passes each value to the given closure. The closure is free to modify the item and return it, thus forming a new collection of modified items. Then, the array is flattened by one level:
const flattened = collect([
{ name: 'Sally' },
{ school: 'Arkansas' },
{ age: 28 }
]).flatMap(values => {
const entries = Object.entries(values);
return Object.fromEntries(entries.map(([k, v]) => [k, String(v).toUpperCase()]));
});
flattened.all();
// { name: 'SALLY', school: 'ARKANSAS', age: '28' }flatten()
The flatten method flattens a multi-dimensional collection into a single dimension:
const flattened = collect({ name: 'Taylor', languages: ['PHP', 'JavaScript'] }).flatten();
flattened.all();
// ['Taylor', 'PHP', 'JavaScript'];If necessary, you may pass the flatten method a "depth" argument:
const products = collect({
Apple: [{ name: 'iPhone 6S', brand: 'Apple' }],
Samsung: [{ name: 'Galaxy S7', brand: 'Samsung' }]
}).flatten(1);
products.values().all();
/*
[{ name: 'iPhone 6S', brand: 'Apple' }, { name: 'Galaxy S7', brand: 'Samsung' }]
*/In this example, calling flatten without providing the depth would have also flattened the nested arrays, resulting in ['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung']. Providing a depth allows you to specify the number of levels nested arrays will be flattened.
flip()
The flip method swaps the collection's keys with their corresponding values:
const flipped = collect({ name: 'Taylor', framework: 'Laravel' }).flip();
flipped.all();
// { Taylor: 'name', Laravel: 'framework' }forget()
The forget method removes an item from the collection by its key:
const collection = collect({ name: 'Taylor', framework: 'Laravel' });
// Forget a single key...
collection.forget('name');
// { framework: 'Laravel' }
// Forget multiple keys...
collection.forget(['name', 'framework']);
// []WARNING
Unlike most other collection methods, forget does not return a new modified collection; it modifies and returns the collection it is called on.
forPage()
The forPage method returns a new collection containing the items that would be present on a given page number. The method accepts the page number as its first argument and the number of items to show per page as its second argument:
const chunk = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]).forPage(2, 3);
chunk.all();
// [4, 5, 6]fromJson()
The static fromJson method creates a new collection instance by decoding a given JSON string using JSON.parse:
const json = JSON.stringify({ name: 'Taylor Otwell', role: 'Developer', status: 'Active' });
const collection = Collection.fromJson(json);get()
The get method returns the item at a given key. If the key does not exist, null is returned:
collect({ name: 'Taylor', framework: 'Laravel' }).get('name');
// TaylorYou may optionally pass a default value as the second argument:
collect({ name: 'Taylor', framework: 'Laravel' }).get('age', 34);
// 34You may even pass a callback as the method's default value. The result of the callback will be returned if the specified key does not exist:
collect({ name: 'Taylor', framework: 'Laravel' }).get('email', () => 'taylor@example.com');
// taylor@example.comgroupBy()
The groupBy method groups the collection's items by a given key:
const grouped = collect([
{ account_id: 'account-x10', product: 'Chair' },
{ account_id: 'account-x10', product: 'Bookcase' },
{ account_id: 'account-x11', product: 'Desk' }
]).groupBy('account_id');
grouped.all();
// {
// 'account-x10': [
// { account_id: 'account-x10', product: 'Chair' },
// { account_id: 'account-x10', product: 'Bookcase' }
// ],
// 'account-x11': [
// { account_id: 'account-x11', product: 'Desk' }
// ]
// }Instead of passing a string key, you may pass a callback. The callback should return the value you wish to key the group by:
const grouped = collect([
{ account_id: 'account-x10', product: 'Chair' },
{ account_id: 'account-x10', product: 'Bookcase' },
{ account_id: 'account-x11', product: 'Desk' }
]).groupBy((item, key) => item.account_id.slice(-3));
grouped.all();
// {
// x10: [
// { account_id: 'account-x10', product: 'Chair' },
// { account_id: 'account-x10', product: 'Bookcase' }
// ],
// x11: [
// { account_id: 'account-x11', product: 'Desk' }
// ]
// }Multiple grouping criteria may be passed as an array. Each array element will be applied to the corresponding level within a multi-dimensional array:
const data = new Collection({
10: { user: 1, skill: 1, roles: ['Role_1', 'Role_3'] },
20: { user: 2, skill: 1, roles: ['Role_1', 'Role_2'] },
30: { user: 3, skill: 2, roles: ['Role_1'] },
40: { user: 4, skill: 2, roles: ['Role_2'] }
});
const result = data.groupBy(['skill', (item) => item.roles], true);
// {
// 1: {
// Role_1: {
// 10: { user: 1, skill: 1, roles: ['Role_1', 'Role_3'] },
// 20: { user: 2, skill: 1, roles: ['Role_1', 'Role_2'] }
// },
// Role_2: {
// 20: { user: 2, skill: 1, roles: ['Role_1', 'Role_2'] }
// },
// Role_3: {
// 10: { user: 1, skill: 1, roles: ['Role_1', 'Role_3'] }
// }
// },
// 2: {
// Role_1: {
// 30: { user: 3, skill: 2, roles: ['Role_1'] }
// },
// Role_2: {
// 40: { user: 4, skill: 2, roles: ['Role_2'] }
// }
// }
// }has()
The has method determines if a given key exists in the collection:
collect({ account_id: 1, product: 'Desk', amount: 5 }).has('product');
// true
collect({ account_id: 1, product: 'Desk', amount: 5 }).has(['product', 'amount']);
// true
collect({ account_id: 1, product: 'Desk', amount: 5 }).has(['amount', 'price']);
// falsehasAny()
The hasAny method determines whether any of the given keys exist in the collection:
collect({ account_id: 1, product: 'Desk', amount: 5 }).hasAny(['product', 'price']);
// true
collect({ account_id: 1, product: 'Desk', amount: 5 }).hasAny(['name', 'price']);
// falseimplode()
The implode method joins items in a collection. Its arguments depend on the type of items in the collection. If the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values:
collect([
{ account_id: 1, product: 'Desk' },
{ account_id: 2, product: 'Chair' }
]).implode('product', ', ');
// 'Desk, Chair'If the collection contains simple strings or numeric values, you should pass the "glue" as the only argument to the method:
collect([1, 2, 3, 4, 5]).implode('-');
// '1-2-3-4-5'You may pass a closure to the implode method if you would like to format the values being imploded:
collect([
{ account_id: 1, product: 'Desk' },
{ account_id: 2, product: 'Chair' }
]).implode((item, key) => item.product.toUpperCase(), ', ');
// 'DESK, CHAIR'intersect()
The intersect method removes any values from the original collection that are not present in the given array or collection. The resulting collection will preserve the original collection's keys:
const intersect = collect(['Desk', 'Sofa', 'Chair']).intersect(['Desk', 'Chair', 'Bookcase']);
intersect.all();
// { 0: 'Desk', 2: 'Chair' }intersectUsing()
The intersectUsing method removes any values from the original collection that are not present in the given array or collection, using a custom callback to compare the values. The resulting collection will preserve the original collection's keys:
const intersect = collect(['Desk', 'Sofa', 'Chair']).intersectUsing(['desk', 'chair', 'bookcase'], (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
intersect.all();
// { 0: 'Desk', 2: 'Chair' }intersectAssoc()
The intersectAssoc method compares the original collection against another collection or array, returning the key / value pairs that are present in all of the given collections:
const intersect = collect({ color: 'red', size: 'M', material: 'cotton' }).intersectAssoc({ color: 'blue', size: 'M', material: 'polyester' });
intersect.all();
// { size: 'M' }intersectAssocUsing()
The intersectAssocUsing method compares the original collection against another collection or array, returning the key / value pairs that are present in both, using a custom comparison callback to determine equality for both keys and values:
const intersect = collect({ color: 'red', Size: 'M', material: 'cotton' }).intersectAssocUsing({ Color: 'blue', size: 'M', Material: 'polyester' }, (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
intersect.all();
// { Size: 'M' }intersectByKeys()
The intersectByKeys method removes any keys and their corresponding values from the original collection that are not present in the given array or collection:
const intersect = collect({ serial: 'UX301', type: 'screen', year: 2009 }).intersectByKeys({ reference: 'UX404', type: 'tab', year: 2011 });
intersect.all();
// { type: 'screen', year: 2009 }isEmpty()
The isEmpty method returns true if the collection is empty; otherwise, false is returned:
collect([]).isEmpty();
// trueisNotEmpty()
The isNotEmpty method returns true if the collection is not empty; otherwise, false is returned:
collect([]).isNotEmpty();
// falsejoin()
The join method joins the collection's values with a string. Using this method's second argument, you may also specify how the final element should be appended to the string:
collect(['a', 'b', 'c']).join(', '); // 'a, b, c'
collect(['a', 'b', 'c']).join(', ', ', and '); // 'a, b, and c'
collect(['a', 'b']).join(', ', ' and '); // 'a and b'
collect(['a']).join(', ', ' and '); // 'a'
collect([]).join(', ', ' and '); // ''keyBy()
The keyBy method keys the collection by the given key. If multiple items have the same key, only the last one will appear in the new collection:
const keyed = collect([
{ product_id: 'prod-100', name: 'Desk' },
{ product_id: 'prod-200', name: 'Chair' }
]).keyBy('product_id');
keyed.all();
// {
// 'prod-100': { product_id: 'prod-100', name: 'Desk' },
// 'prod-200': { product_id: 'prod-200', name: 'Chair' }
// }You may also pass a callback to the method. The callback should return the value to key the collection by:
collect([
{ product_id: 'prod-100', name: 'Desk' },
{ product_id: 'prod-200', name: 'Chair' }
]).keyBy((item, key) => item.product_id.toUpperCase()).all();
// {
// 'PROD-100': { product_id: 'prod-100', name: 'Desk' },
// 'PROD-200': { product_id: 'prod-200', name: 'Chair' }
// }keys()
The keys method returns all of the collection's keys:
const keys = collect({
'prod-100': { product_id: 'prod-100', name: 'Desk' },
'prod-200': { product_id: 'prod-200', name: 'Chair' }
}).keys();
keys.all();
// ['prod-100', 'prod-200']last()
The last method returns the last element in the collection that passes a given truth test:
collect([1, 2, 3, 4]).last((value, key) => value < 3);
// 2You may also call the last method with no arguments to get the last element in the collection. If the collection is empty, null is returned:
collect([1, 2, 3, 4]).last();
// 4lazy()
The lazy method returns a new LazyCollection instance from the underlying array of items:
const lazyCollection = collect([1, 2, 3, 4]).lazy();
// Returns a LazyCollection instance
lazyCollection.all();
// [1, 2, 3, 4]This is especially useful when you need to perform transformations on a huge Collection that contains many items:
const count = hugeCollection
.lazy()
.where('country', 'FR')
.where('balance', '>', '100')
.count();By converting the collection to a LazyCollection, we avoid having to allocate a ton of additional memory. Though the original collection still keeps its values in memory, the subsequent filters will not. Therefore, virtually no additional memory will be allocated when filtering the collection's results.
macro()
The static macro method allows you to add methods to the Collection class at run time. Refer to the documentation on extending collections for more information.
make()
The static make method creates a new collection instance. See the Creating Collections section.
const collection = Collection.make([1, 2, 3]);map()
The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:
const multiplied = collect([1, 2, 3, 4, 5]).map((item, key) => item * 2);
multiplied.all();
// [2, 4, 6, 8, 10]WARNING
Like most other collection methods, map returns a new collection instance; it does not modify the collection it is called on. If you want to transform the original collection, use the transform method.
mapInto()
The mapInto() method iterates over the collection, creating a new instance of the given class by passing the value into the constructor:
class Currency {
constructor(public code: string) {}
}
const currencies = collect(['USD', 'EUR', 'GBP']).mapInto(Currency);
currencies.all();
// [Currency('USD'), Currency('EUR'), Currency('GBP')]mapSpread()
The mapSpread method iterates over the collection's items, passing each nested item value into the given closure. The closure is free to modify the item and return it, thus forming a new collection of modified items:
const sequence = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).chunk(2).mapSpread((even, odd) => even + odd);
sequence.all();
// [1, 5, 9, 13, 17]mapToDictionary()
The mapToDictionary method groups the collection's items by the given closure. The closure should return a tuple containing a single key / value pair. Unlike mapToGroups, this method returns plain arrays instead of Collection instances:
const groups = collect([
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },
{ id: 3, name: 'A' },
]).mapToDictionary((item) => [item.name, item.id]);
groups.all();
// { A: [1, 3], B: [2] }NOTE
The mapToDictionary method returns a collection of arrays, while mapToGroups returns a collection of Collection instances. Use mapToDictionary when you need plain arrays for each group.
mapToGroups()
The mapToGroups method groups the collection's items by the given closure. The closure should return an associative array containing a single key / value pair, thus forming a new collection of grouped values:
const grouped = collect([
{ name: 'John Doe', department: 'Sales' },
{ name: 'Jane Doe', department: 'Sales' },
{ name: 'Johnny Doe', department: 'Marketing' }
]).mapToGroups((item, key) => ({ [item.department]: item.name }));
grouped.all();
// { Sales: ['John Doe', 'Jane Doe'], Marketing: ['Johnny Doe'] }
grouped.get('Sales').all();
// ['John Doe', 'Jane Doe']mapWithKeys()
The mapWithKeys method iterates through the collection and passes each value to the given callback. The callback should return an associative array containing a single key / value pair:
const keyed = collect([
{ name: 'John', department: 'Sales', email: 'john@example.com' },
{ name: 'Jane', department: 'Marketing', email: 'jane@example.com' }
]).mapWithKeys((item, key) => ({ [item.email]: item.name }));
keyed.all();
// { 'john@example.com': 'John', 'jane@example.com': 'Jane' }max()
The max method returns the maximum value of a given key:
const max = collect([
{ foo: 10 },
{ foo: 20 }
]).max('foo');
// 20
const max = collect([1, 2, 3, 4, 5]).max();
// 5median()
The median method returns the median value of a given key:
const median = collect([
{ foo: 10 },
{ foo: 10 },
{ foo: 20 },
{ foo: 40 }
]).median('foo');
// 15
const median = collect([1, 1, 2, 4]).median();
// 1.5merge()
The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given item's value will overwrite the value in the original collection:
const merged = collect({ product_id: 1, price: 100 }).merge({ price: 200, discount: false });
merged.all();
// { product_id: 1, price: 200, discount: false }If the given item's keys are numeric, the values will be appended to the end of the collection:
const merged = collect(['Desk', 'Chair']).merge(['Bookcase', 'Door']);
merged.all();
// ['Desk', 'Chair', 'Bookcase', 'Door']mergeRecursive()
The mergeRecursive method merges the given array or collection recursively with the original collection. If a string key in the given items matches a string key in the original collection, then the values for these keys are merged together into an array, and this is done recursively:
const merged = collect({ product_id: 1, price: 100 }).mergeRecursive({ product_id: 2, price: 200, discount: false });
merged.all();
// { product_id: [1, 2], price: [100, 200], discount: false }min()
The min method returns the minimum value of a given key:
const min = collect([
{ foo: 10 },
{ foo: 20 }
]).min('foo');
// 10
const min = collect([1, 2, 3, 4, 5]).min();
// 1mode()
The mode method returns the mode value of a given key:
const mode = collect([
{ foo: 10 },
{ foo: 10 },
{ foo: 20 },
{ foo: 40 }
]).mode('foo');
// [10]
const mode = collect([1, 1, 2, 4]).mode();
// [1]
const mode = collect([1, 1, 2, 2]).mode();
// [1, 2]multiply()
The multiply method creates the specified number of copies of all items in the collection:
const users = collect([
{ name: 'User #1', email: 'user1@example.com' },
{ name: 'User #2', email: 'user2@example.com' }
]).multiply(3);
/*
[
{ name: 'User #1', email: 'user1@example.com' },
{ name: 'User #2', email: 'user2@example.com' },
{ name: 'User #1', email: 'user1@example.com' },
{ name: 'User #2', email: 'user2@example.com' },
{ name: 'User #1', email: 'user1@example.com' },
{ name: 'User #2', email: 'user2@example.com' }
]
*/nth()
The nth method creates a new collection consisting of every n-th element:
collect(['a', 'b', 'c', 'd', 'e', 'f']).nth(4);
// ['a', 'e']You may optionally pass a starting offset as the second argument:
collect(['a', 'b', 'c', 'd', 'e', 'f']).nth(4, 1);
// ['b', 'f']only()
The only method returns the items in the collection with the specified keys:
const filtered = collect({ product_id: 1, name: 'Desk', price: 100, discount: false }).only(['product_id', 'name']);
filtered.all();
// { product_id: 1, name: 'Desk' }For the inverse of only, see the except method.
pad()
The pad method will fill the array with the given value until the array reaches the specified size.
To pad to the left, you should specify a negative size. No padding will take place if the absolute value of the given size is less than or equal to the length of the array:
collect(['A', 'B', 'C']).pad(5, 0).all();
// ['A', 'B', 'C', 0, 0]
collect(['A', 'B', 'C']).pad(-5, 0).all();
// [0, 0, 'A', 'B', 'C']partition()
The partition method may be combined with array destructuring to separate elements that pass a given truth test from those that do not:
const [underThree, equalOrAboveThree] = collect([1, 2, 3, 4, 5, 6]).partition((i) => i < 3);
underThree.all();
// [1, 2]
equalOrAboveThree.all();
// [3, 4, 5, 6]percentage()
The percentage method may be used to quickly determine the percentage of items in the collection that pass a given truth test:
const percentage = collect([1, 1, 2, 2, 2, 3]).percentage((value) => value === 1);
// 33.33By default, the percentage will be rounded to two decimal places. However, you may customize this behavior by providing a second argument to the method:
const percentage = collection.percentage((value) => value === 1, 3);
// 33.333pipe()
The pipe method passes the collection to the given closure and returns the result of the executed closure:
const piped = collect([1, 2, 3]).pipe((collection) => collection.sum());
// 6pipeInto()
The pipeInto method creates a new instance of the given class and passes the collection into the constructor:
class ResourceCollection {
collection: Collection<number>;
constructor(collection: Collection<number>) {
this.collection = collection;
}
}
const collection = collect([1, 2, 3]);
const resource = collection.pipeInto(ResourceCollection);
resource.collection.all();
// [1, 2, 3]pipeThrough()
The pipeThrough method passes the collection to the given array of closures and returns the result of the executed closures:
const result = collect([1, 2, 3]).pipeThrough([
(collection) => collection.merge([4, 5]),
(collection) => collection.sum(),
]);
// 15pluck()
The pluck method retrieves all of the values for a given key:
const plucked = collect([
{ product_id: 'prod-100', name: 'Desk' },
{ product_id: 'prod-200', name: 'Chair' }
]).pluck('name');
plucked.all();
// ['Desk', 'Chair']You may also specify how you wish the resulting collection to be keyed:
const plucked = collection.pluck('name', 'product_id');
plucked.all();
// { 'prod-100': 'Desk', 'prod-200': 'Chair' }The pluck method also supports retrieving nested values using "dot" notation:
const plucked = collect([
{
name: 'Laracon',
speakers: {
first_day: ['Rosa', 'Judith'],
},
},
{
name: 'VueConf',
speakers: {
first_day: ['Abigail', 'Joey'],
},
}
]).pluck('speakers.first_day');
plucked.all();
// [['Rosa', 'Judith'], ['Abigail', 'Joey']]If duplicate keys exist, the last matching element will be inserted into the plucked collection:
const plucked = collect([
{ brand: 'Tesla', color: 'red' },
{ brand: 'Pagani', color: 'white' },
{ brand: 'Tesla', color: 'black' },
{ brand: 'Pagani', color: 'orange' }
]).pluck('color', 'brand');
plucked.all();
// { Tesla: 'black', Pagani: 'orange' }pop()
The pop method removes and returns the last item from the collection. If the collection is empty, null will be returned:
const collection = collect([1, 2, 3, 4, 5]);
collection.pop();
// 5
collection.all();
// [1, 2, 3, 4]You may pass an integer to the pop method to remove and return multiple items from the end of a collection:
const collection = collect([1, 2, 3, 4, 5]);
collection.pop(3);
// collect([5, 4, 3])
collection.all();
// [1, 2]prepend()
The prepend method adds an item to the beginning of the collection:
const collection = collect([1, 2, 3, 4, 5]);
collection.prepend(0);
collection.all();
// [0, 1, 2, 3, 4, 5]You may also pass a second argument to specify the key of the prepended item:
const collection = collect({ one: 1, two: 2 });
collection.prepend(0, 'zero');
collection.all();
// { zero: 0, one: 1, two: 2 }pull()
The pull method removes and returns an item from the collection by its key:
const collection = collect({ product_id: 'prod-100', name: 'Desk' });
collection.pull('name');
// 'Desk'
collection.all();
// { product_id: 'prod-100' }push()
The push method appends an item to the end of the collection:
const collection = collect([1, 2, 3, 4]);
collection.push(5);
collection.all();
// [1, 2, 3, 4, 5]You may also provide multiple items to append to the end of the collection:
const collection = collect([1, 2, 3, 4]);
collection.push(5, 6, 7);
collection.all();
// [1, 2, 3, 4, 5, 6, 7]put()
The put method sets the given key and value in the collection:
const collection = collect({ product_id: 1, name: 'Desk' });
collection.put('price', 100);
collection.all();
// { product_id: 1, name: 'Desk', price: 100 }random()
The random method returns a random item from the collection:
collect([1, 2, 3, 4, 5]).random();
// 4 - (randomly)You may pass an integer to random to specify how many items you would like to randomly retrieve. A collection of items is always returned when explicitly passing the number of items you wish to receive:
const random = collect([1, 2, 3, 4, 5]).random(3);
random.all();
// [2, 4, 5] - (randomly)If the collection instance has fewer items than requested, the random method will throw an InvalidArgumentException.
The random method also accepts a closure, which will receive the current collection instance:
const random = collect([1, 2, 3, 4, 5]).random((items) => Math.min(10, items.count()));
random.all();
// [1, 2, 3, 4, 5] - (randomly)range()
The range method returns a collection containing integers between the specified range:
collect().range(3, 6).all();
// [3, 4, 5, 6]reduce()
The reduce method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration:
const total = collect([1, 2, 3]).reduce((carry, item) => carry + item);
// 6The value for carry on the first iteration is null; however, you may specify its initial value by passing a second argument to reduce:
collect([1, 2, 3]).reduce((carry, item) => carry + item, 4);
// 10The reduce method also passes array keys to the given callback:
const ratio = { usd: 1, gbp: 1.37, eur: 1.22 };
collect({ usd: 1400, gbp: 1200, eur: 1000 }).reduce((carry, value, key) => {
return carry + (value * ratio[key]);
}, 0);
// 4264reduceSpread()
The reduceSpread method reduces the collection to an array of values, passing the results of each iteration into the subsequent iteration. This method is similar to the reduce method; however, it can accept multiple initial values:
const [creditsRemaining, batch] = Image.where('status', 'unprocessed')
.get()
.reduceSpread((creditsRemaining, batch, image) => {
if (creditsRemaining >= image.creditsRequired()) {
batch.push(image);
creditsRemaining -= image.creditsRequired();
}
return [creditsRemaining, batch];
}, creditsAvailable, collect());reject()
The reject method filters the collection using the given closure. The closure should return true if the item should be removed from the resulting collection:
const filtered = collect([1, 2, 3, 4]).reject((value, key) => value > 2);
filtered.all();
// [1, 2]For the inverse of the reject method, see the filter method.
replace()
The replace method behaves similarly to merge; however, in addition to overwriting matching items that have string keys, the replace method will also overwrite items in the collection that have matching numeric keys:
const replaced = collect(['Taylor', 'Abigail', 'James']).replace({ 1: 'Victoria', 3: 'Finn' });
replaced.all();
// ['Taylor', 'Victoria', 'James', 'Finn']replaceRecursive()
The replaceRecursive method behaves similarly to replace, but it will recur into arrays and apply the same replacement process to the inner values:
const replaced = collect([
'Taylor', 'Abigail', [
'James', 'Victoria', 'Finn'
]
]).replaceRecursive({ 0: 'Charlie', 2: { 1: 'King' } });
replaced.all();
// ['Charlie', 'Abigail', ['James', 'King', 'Finn']]reverse()
The reverse method reverses the order of the collection's items, preserving the original keys:
const reversed = collect(['a', 'b', 'c', 'd', 'e']).reverse();
reversed.all();
// { 4: 'e', 3: 'd', 2: 'c', 1: 'b', 0: 'a' }search()
The search method searches the collection for the given value and returns its key if found. If the item is not found, false is returned:
collect([2, 4, 6, 8]).search(4);
// 1The search is done using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass true as the second argument to the method:
collect([2, 4, 6, 8]).search('4', true);
// falseAlternatively, you may provide your own closure to search for the first item that passes a given truth test:
collect([2, 4, 6, 8]).search((item, key) => item > 5);
// 2select()
The select method selects the given keys from the collection, similar to an SQL SELECT statement:
const users = collect([
{ name: 'Taylor Otwell', role: 'Developer', status: 'active' },
{ name: 'Victoria Faith', role: 'Researcher', status: 'active' }
]);
users.select(['name', 'role']);
/*
[
{ name: 'Taylor Otwell', role: 'Developer' },
{ name: 'Victoria Faith', role: 'Researcher' }
]
*/shift()
The shift method removes and returns the first item from the collection:
const collection = collect([1, 2, 3, 4, 5]);
collection.shift();
// 1
collection.all();
// [2, 3, 4, 5]You may pass an integer to the shift method to remove and return multiple items from the beginning of a collection:
const collection = collect([1, 2, 3, 4, 5]);
collection.shift(3);
// collect([1, 2, 3])
collection.all();
// [4, 5]shuffle()
The shuffle method randomly shuffles the items in the collection:
const shuffled = collect([1, 2, 3, 4, 5]).shuffle();
shuffled.all();
// [3, 2, 5, 1, 4] - (randomly)skip()
The skip method returns a new collection, with the given number of elements removed from the beginning of the collection:
const skipped = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).skip(4);
skipped.all();
// [5, 6, 7, 8, 9, 10]skipUntil()
The skipUntil method skips over items from the collection while the given callback returns false. Once the callback returns true all of the remaining items in the collection will be returned as a new collection:
const subset = collect([1, 2, 3, 4]).skipUntil((item) => item >= 3);
subset.all();
// [3, 4]You may also pass a simple value to the skipUntil method to skip all items until the given value is found:
const subset = collect([1, 2, 3, 4]).skipUntil(3);
subset.all();
// [3, 4]WARNING
If the given value is not found or the callback never returns true, the skipUntil method will return an empty collection.
skipWhile()
The skipWhile method skips over items from the collection while the given callback returns true. Once the callback returns false all of the remaining items in the collection will be returned as a new collection:
const subset = collect([1, 2, 3, 4]).skipWhile((item) => item <= 3);
subset.all();
// [4]WARNING
If the callback never returns false, the skipWhile method will return an empty collection.
slice()
The slice method returns a slice of the collection starting at the given index:
const slice = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).slice(4);
slice.all();
// [5, 6, 7, 8, 9, 10]If you would like to limit the size of the returned slice, pass the desired size as the second argument to the method:
const slice = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).slice(4, 2);
slice.all();
// [5, 6]The returned slice will preserve keys by default. If you do not wish to preserve the original keys, you can use the values method to reindex them.
sliding()
The sliding method returns a new collection of chunks representing a "sliding window" view of the items in the collection:
const chunks = collect([1, 2, 3, 4, 5]).sliding(2);
chunks.toArray();
// [[1, 2], [2, 3], [3, 4], [4, 5]]This is especially useful in conjunction with the eachSpread method:
transactions.sliding(2).eachSpread((previous, current) => {
current.total = previous.total + current.amount;
});You may optionally pass a second "step" value, which determines the distance between the first item of every chunk:
const chunks = collect([1, 2, 3, 4, 5]).sliding(3, 2);
chunks.toArray();
// [[1, 2, 3], [3, 4, 5]]sole()
The sole method returns the first element in the collection that passes a given truth test, but only if the truth test matches exactly one element:
collect([1, 2, 3, 4]).sole((value, key) => value === 2);
// 2You may also pass a key / value pair to the sole method, which will return the first element in the collection that matches the given pair, but only if it exactly one element matches:
collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 100 }
]).sole('product', 'Chair');
// { product: 'Chair', price: 100 }Alternatively, you may also call the sole method with no argument to get the first element in the collection if there is only one element:
collect([{ product: 'Desk', price: 200 }]).sole();
// { product: 'Desk', price: 200 }If there are no elements in the collection that should be returned by the sole method, an ItemNotFoundException exception will be thrown. If there is more than one element that should be returned, a MultipleItemsFoundException will be thrown.
some()
Alias for the contains method.
sort()
The sort method sorts the collection. The sorted collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively numbered indexes:
const sorted = collect([5, 3, 1, 2, 4]).sort();
sorted.values().all();
// [1, 2, 3, 4, 5]If your sorting needs are more advanced, you may pass a callback to sort with your own algorithm. The callback should return a negative number if the first argument should come before the second, a positive number if the second should come before the first, or zero if they are equal.
NOTE
If you need to sort a collection of nested arrays or objects, see the sortBy and sortByDesc methods.
sortBy()
The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively numbered indexes:
const sorted = collect([
{ name: 'Desk', price: 200 },
{ name: 'Chair', price: 100 },
{ name: 'Bookcase', price: 150 }
]).sortBy('price');
sorted.values().all();
/*
[
{ name: 'Chair', price: 100 },
{ name: 'Bookcase', price: 150 },
{ name: 'Desk', price: 200 }
]
*/You may also pass your own closure to determine how to sort the collection's values:
const sorted = collect([
{ name: 'Desk', colors: ['Black', 'Mahogany'] },
{ name: 'Chair', colors: ['Black'] },
{ name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }
]).sortBy((product, key) => product.colors.length);
sorted.values().all();
/*
[
{ name: 'Chair', colors: ['Black'] },
{ name: 'Desk', colors: ['Black', 'Mahogany'] },
{ name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }
]
*/If you would like to sort your collection by multiple attributes, you may pass an array of sort operations to the sortBy method. Each sort operation should be an array consisting of the attribute that you wish to sort by and the direction of the desired sort:
const sorted = collect([
{ name: 'Taylor Otwell', age: 34 },
{ name: 'Abigail Otwell', age: 30 },
{ name: 'Taylor Otwell', age: 36 },
{ name: 'Abigail Otwell', age: 32 }
]).sortBy([
['name', 'asc'],
['age', 'desc'],
]);
sorted.values().all();
/*
[
{ name: 'Abigail Otwell', age: 32 },
{ name: 'Abigail Otwell', age: 30 },
{ name: 'Taylor Otwell', age: 36 },
{ name: 'Taylor Otwell', age: 34 }
]
*/When sorting a collection by multiple attributes, you may also provide closures that define each sort operation:
const sorted = collect([
{ name: 'Taylor Otwell', age: 34 },
{ name: 'Abigail Otwell', age: 30 },
{ name: 'Taylor Otwell', age: 36 },
{ name: 'Abigail Otwell', age: 32 }
]).sortBy([
(a, b) => a.name.localeCompare(b.name),
(a, b) => b.age - a.age
]);
sorted.values().all();
/*
[
{ name: 'Abigail Otwell', age: 32 },
{ name: 'Abigail Otwell', age: 30 },
{ name: 'Taylor Otwell', age: 36 },
{ name: 'Taylor Otwell', age: 34 }
]
*/sortByDesc()
This method has the same signature as the sortBy method, but will sort the collection in the opposite order.
sortDesc()
This method will sort the collection in the opposite order as the sort method:
const sorted = collect([5, 3, 1, 2, 4]).sortDesc();
sorted.values().all();
// [5, 4, 3, 2, 1]Unlike sort, you may not pass a closure to sortDesc. Instead, you should use the sort method and invert your comparison.
sortKeys()
The sortKeys method sorts the collection by the keys of the underlying associative array:
const sorted = collect({ id: 22345, first: 'John', last: 'Doe' }).sortKeys();
sorted.all();
/*
{ first: 'John', id: 22345, last: 'Doe' }
*/sortKeysDesc()
This method has the same signature as the sortKeys method, but will sort the collection in the opposite order.
sortKeysUsing()
The sortKeysUsing method sorts the collection by the keys of the underlying associative array using a callback:
const sorted = collect({ ID: 22345, first: 'John', last: 'Doe' }).sortKeysUsing((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
sorted.all();
/*
{ first: 'John', ID: 22345, last: 'Doe' }
*/The callback must be a comparison function that returns an integer less than, equal to, or greater than zero.
splice()
The splice method removes and returns a slice of items starting at the specified index:
const collection = collect([1, 2, 3, 4, 5]);
const chunk = collection.splice(2);
chunk.all();
// [3, 4, 5]
collection.all();
// [1, 2]You may pass a second argument to limit the size of the resulting collection:
const collection = collect([1, 2, 3, 4, 5]);
const chunk = collection.splice(2, 1);
chunk.all();
// [3]
collection.all();
// [1, 2, 4, 5]In addition, you may pass a third argument containing the new items to replace the items removed from the collection:
const collection = collect([1, 2, 3, 4, 5]);
const chunk = collection.splice(2, 1, [10, 11]);
chunk.all();
// [3]
collection.all();
// [1, 2, 10, 11, 4, 5]split()
The split method breaks a collection into the given number of groups:
const groups = collect([1, 2, 3, 4, 5]).split(3);
groups.all();
// [[1, 2], [3, 4], [5]]splitIn()
The splitIn method breaks a collection into the given number of groups, filling non-terminal groups completely before allocating the remainder to the final group:
const groups = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).splitIn(3);
groups.all();
// [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]sum()
The sum method returns the sum of all items in the collection:
collect([1, 2, 3, 4, 5]).sum();
// 15If the collection contains nested arrays or objects, you should pass a key that will be used to determine which values to sum:
collect([
{ name: 'JavaScript: The Good Parts', pages: 176 },
{ name: 'JavaScript: The Definitive Guide', pages: 1096 }
]).sum('pages');
// 1272In addition, you may pass your own closure to determine which values of the collection to sum:
collect([
{ name: 'Chair', colors: ['Black'] },
{ name: 'Desk', colors: ['Black', 'Mahogany'] },
{ name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }
]).sum((product) => product.colors.length);
// 6take()
The take method returns a new collection with the specified number of items:
const chunk = collect([0, 1, 2, 3, 4, 5]).take(3);
chunk.all();
// [0, 1, 2]You may also pass a negative integer to take the specified number of items from the end of the collection:
const chunk = collect([0, 1, 2, 3, 4, 5]).take(-2);
chunk.all();
// [4, 5]takeUntil()
The takeUntil method returns items in the collection until the given callback returns true:
const subset = collect([1, 2, 3, 4]).takeUntil((item) => item >= 3);
subset.all();
// [1, 2]You may also pass a simple value to the takeUntil method to get the items until the given value is found:
const subset = collect([1, 2, 3, 4]).takeUntil(3);
subset.all();
// [1, 2]WARNING
If the given value is not found or the callback never returns true, the takeUntil method will return all items in the collection.
takeWhile()
The takeWhile method returns items in the collection until the given callback returns false:
const subset = collect([1, 2, 3, 4]).takeWhile((item) => item < 3);
subset.all();
// [1, 2]WARNING
If the callback never returns false, the takeWhile method will return all items in the collection.
tap()
The tap method passes the collection to the given callback, allowing you to "tap" into the collection at a specific point and do something with the items while not affecting the collection itself. The collection is then returned by the tap method:
collect([2, 4, 3, 1, 5])
.sort()
.tap((collection) => {
console.log('Values after sorting', collection.values().all());
})
.shift();
// 1times()
The static times method creates a new collection by invoking the given closure a specified number of times:
const collection = Collection.times(10, (number) => number * 9);
collection.all();
// [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]toArray()
The toArray method converts the collection into a plain JavaScript array. If the collection's values are objects, they will remain as objects in the array:
collect({ name: 'Desk', price: 200 }).toArray();
// { name: 'Desk', price: 200 }WARNING
toArray also converts all of the collection's nested objects that are an instance of Arrayable to an array. If you want to get the raw array underlying the collection, use the all method instead.
toJson()
The toJson method converts the collection into a JSON serialized string:
collect({ name: 'Desk', price: 200 }).toJson();
// '{"name":"Desk","price":200}'toPrettyJson()
The toPrettyJson method converts the collection into a formatted JSON string using the JSON_PRETTY_PRINT option:
collect({ name: 'Desk', price: 200 }).toPrettyJson();transform()
The transform method iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:
const collection = collect([1, 2, 3, 4, 5]);
collection.transform((item, key) => item * 2);
collection.all();
// [2, 4, 6, 8, 10]WARNING
Unlike most other collection methods, transform modifies the collection itself. If you wish to create a new collection instead, use the map method.
undot()
The undot method expands a single-dimensional collection that uses "dot" notation into a multi-dimensional collection:
const person = collect({
'name.first_name': 'Marie',
'name.last_name': 'Valentine',
'address.line_1': '2992 Eagle Drive',
'address.line_2': '',
'address.suburb': 'Detroit',
'address.state': 'MI',
'address.postcode': '48219'
});
const undotted = person.undot();
undotted.toArray();
/*
{
name: {
first_name: 'Marie',
last_name: 'Valentine',
},
address: {
line_1: '2992 Eagle Drive',
line_2: '',
suburb: 'Detroit',
state: 'MI',
postcode: '48219',
}
}
*/union()
The union method adds the given array to the collection. If the given array contains keys that are already in the original collection, the original collection's values will be preferred:
const union = collect({ 1: ['a'], 2: ['b'] }).union({ 3: ['c'], 1: ['d'] });
union.all();
// { 1: ['a'], 2: ['b'], 3: ['c'] }unique()
The unique method returns all of the unique items in the collection. The returned collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively numbered indexes:
const unique = collect([1, 1, 2, 2, 3, 4, 2]).unique();
unique.values().all();
// [1, 2, 3, 4]When dealing with nested arrays or objects, you may specify the key used to determine uniqueness:
const unique = collect([
{ 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('brand');
unique.values().all();
/*
[
{ name: 'iPhone 6', brand: 'Apple', type: 'phone' },
{ name: 'Galaxy S6', brand: 'Samsung', type: 'phone' }
]
*/Finally, you may also pass your own closure to the unique method to specify which value should determine an item's uniqueness:
const unique = collection.unique((item) => item.brand + item.type);
unique.values().all();
/*
[
{ name: 'iPhone 6', 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' }
]
*/The unique method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the uniqueStrict method to filter using "strict" comparisons.
uniqueStrict()
This method has the same signature as the unique method; however, all values are compared using "strict" comparisons.
unless()
The unless method will execute the given callback unless the first argument given to the method evaluates to true. The collection instance and the first argument given to the unless method will be provided to the closure:
const collection = collect([1, 2, 3]);
collection.unless(true, (collection, value) => collection.push(4));
collection.unless(false, (collection, value) => collection.push(5));
collection.all();
// [1, 2, 3, 5]A second callback may be passed to the unless method. The second callback will be executed when the first argument given to the unless method evaluates to true:
const collection = collect([1, 2, 3]);
collection.unless(true, (collection, value) => collection.push(4), (collection, value) => collection.push(5));
collection.all();
// [1, 2, 3, 5]For the inverse of unless, see the when method.
unlessEmpty()
Alias for the whenNotEmpty method.
unlessNotEmpty()
Alias for the whenEmpty method.
unwrap()
The static unwrap method returns the collection's underlying items from the given value when applicable:
Collection.unwrap(collect('John Doe'));
// ['John Doe']
Collection.unwrap(['John Doe']);
// ['John Doe']
Collection.unwrap('John Doe');
// 'John Doe'value()
The value method retrieves a given value from the first element of the collection:
collect([
{ product: 'Desk', price: 200 },
{ product: 'Speaker', price: 400 }
]).value('price');
// 200values()
The values method returns a new collection with the keys reset to consecutive integers:
const values = collect({
10: { product: 'Desk', price: 200 },
11: { product: 'Desk', price: 200 }
}).values();
values.all();
/*
[
{ product: 'Desk', price: 200 },
{ product: 'Desk', price: 200 }
]
*/when()
The when method will execute the given callback when the first argument given to the method evaluates to true. The collection instance and the first argument given to the when method will be provided to the closure:
const collection = collect([1, 2, 3]);
collection.when(true, (collection, value) => collection.push(4));
collection.when(false, (collection, value) => collection.push(5));
collection.all();
// [1, 2, 3, 4]A second callback may be passed to the when method. The second callback will be executed when the first argument given to the when method evaluates to false:
const collection = collect([1, 2, 3]);
collection.when(false, (collection, value) => collection.push(4), (collection, value) => collection.push(5));
collection.all();
// [1, 2, 3, 5]For the inverse of when, see the unless method.
whenEmpty()
The whenEmpty method will execute the given callback when the collection is empty:
const collection = collect(['Michael', 'Tom']);
collection.whenEmpty((collection) => collection.push('Adam'));
collection.all();
// ['Michael', 'Tom']
const collection = collect();
collection.whenEmpty((collection) => collection.push('Adam'));
collection.all();
// ['Adam']A second closure may be passed to the whenEmpty method that will be executed when the collection is not empty:
const collection = collect(['Michael', 'Tom']);
collection.whenEmpty((collection) => collection.push('Adam'), (collection) => collection.push('Taylor'));
collection.all();
// ['Michael', 'Tom', 'Taylor']For the inverse of whenEmpty, see the whenNotEmpty method.
whenNotEmpty()
The whenNotEmpty method will execute the given callback when the collection is not empty:
const collection = collect(['Michael', 'Tom']);
collection.whenNotEmpty((collection) => collection.push('Adam'));
collection.all();
// ['Michael', 'Tom', 'Adam']
const collection = collect();
collection.whenNotEmpty((collection) => collection.push('Adam'));
collection.all();
// []A second closure may be passed to the whenNotEmpty method that will be executed when the collection is empty:
const collection = collect();
collection.whenNotEmpty((collection) => collection.push('Adam'), (collection) => collection.push('Taylor'));
collection.all();
// ['Taylor']For the inverse of whenNotEmpty, see the whenEmpty method.
where()
The where method filters the collection by a given key / value pair:
const filtered = collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 100 },
{ product: 'Bookcase', price: 150 },
{ product: 'Door', price: 100 }
]).where('price', 100);
filtered.all();
/*
[
{ product: 'Chair', price: 100 },
{ product: 'Door', price: 100 }
]
*/The where method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the whereStrict method to filter using "strict" comparisons, or the whereNull and whereNotNull methods to filter for null values.
Optionally, you may pass a comparison operator as the second parameter. Supported operators are: '===', '!==', '!=', '==', '=', '<>', '>', '<', '>=', and '<=':
const filtered = collect([
{ name: 'Jim', platform: 'Mac' },
{ name: 'Sally', platform: 'Mac' },
{ name: 'Sue', platform: 'Linux' }
]).where('platform', '!=', 'Linux');
filtered.all();
/*
[
{ name: 'Jim', platform: 'Mac' },
{ name: 'Sally', platform: 'Mac' }
]
*/whereStrict()
This method has the same signature as the where method; however, all values are compared using "strict" comparisons.
whereBetween()
The whereBetween method filters the collection by determining if a specified item value is within a given range:
const filtered = collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 80 },
{ product: 'Bookcase', price: 150 },
{ product: 'Pencil', price: 30 },
{ product: 'Door', price: 100 }
]).whereBetween('price', [100, 200]);
filtered.all();
/*
[
{ product: 'Desk', price: 200 },
{ product: 'Bookcase', price: 150 },
{ product: 'Door', price: 100 }
]
*/whereIn()
The whereIn method removes elements from the collection that do not have a specified item value that is contained within the given array:
const filtered = collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 100 },
{ product: 'Bookcase', price: 150 },
{ product: 'Door', price: 100 }
]).whereIn('price', [150, 200]);
filtered.all();
/*
[
{ product: 'Desk', price: 200 },
{ product: 'Bookcase', price: 150 }
]
*/The whereIn method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the whereInStrict method to filter using "strict" comparisons.
whereInStrict()
This method has the same signature as the whereIn method; however, all values are compared using "strict" comparisons.
whereInstanceOf()
The whereInstanceOf method filters the collection by a given class type:
import { User } from './models/User';
import { Post } from './models/Post';
const filtered = collect([
new User(),
new User(),
new Post()
]).whereInstanceOf(User);
filtered.all();
// [User, User]whereNotBetween()
The whereNotBetween method filters the collection by determining if a specified item value is outside of a given range:
const filtered = collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 80 },
{ product: 'Bookcase', price: 150 },
{ product: 'Pencil', price: 30 },
{ product: 'Door', price: 100 }
]).whereNotBetween('price', [100, 200]);
filtered.all();
/*
[
{ product: 'Chair', price: 80 },
{ product: 'Pencil', price: 30 }
]
*/whereNotIn()
The whereNotIn method removes elements from the collection that have a specified item value that is contained within the given array:
const filtered = collect([
{ product: 'Desk', price: 200 },
{ product: 'Chair', price: 100 },
{ product: 'Bookcase', price: 150 },
{ product: 'Door', price: 100 }
]).whereNotIn('price', [150, 200]);
filtered.all();
/*
[
{ product: 'Chair', price: 100 },
{ product: 'Door', price: 100 }
]
*/The whereNotIn method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the whereNotInStrict method to filter using "strict" comparisons.
whereNotInStrict()
This method has the same signature as the whereNotIn method; however, all values are compared using "strict" comparisons.
whereNotNull()
The whereNotNull method returns items from the collection where the given key is not null:
const filtered = collect([
{ name: 'Desk' },
{ name: null },
{ name: 'Bookcase' },
{ name: 0 },
{ name: '' }
]).whereNotNull('name');
filtered.all();
/*
[
{ name: 'Desk' },
{ name: 'Bookcase' },
{ name: 0 },
{ name: '' }
]
*/whereNull()
The whereNull method returns items from the collection where the given key is null:
const filtered = collect([
{ name: 'Desk' },
{ name: null },
{ name: 'Bookcase' },
{ name: 0 },
{ name: '' }
]).whereNull('name');
filtered.all();
// [{ name: null }]wrap()
The static wrap method wraps the given value in a collection when applicable:
const collection = Collection.wrap('John Doe');
collection.all();
// ['John Doe']
const collection = Collection.wrap(['John Doe']);
collection.all();
// ['John Doe']
const collection = Collection.wrap(collect('John Doe'));
collection.all();
// ['John Doe']zip()
The zip method merges together the values of the given array with the values of the original collection at their corresponding index:
const zipped = collect(['Chair', 'Desk']).zip([100, 200]);
zipped.all();
// [['Chair', 100], ['Desk', 200]]Higher Order Messages
Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: average, avg, contains, each, every, filter, first, flatMap, groupBy, keyBy, map, max, min, partition, reject, skipUntil, skipWhile, some, sortBy, sortByDesc, sum, takeUntil, takeWhile, and unique.
Each higher order message can be accessed as a dynamic property on a collection instance. For instance, let's use the each higher order message to call a method on each object within a collection:
class User {
name: string;
constructor(name: string) { this.name = name; }
notify() { console.log(`Notifying ${this.name}`); }
}
const users = collect([new User('Alice'), new User('Bob')]);
users.each.notify();
// Notifying Alice
// Notifying BobLikewise, we can use the sum higher order message to gather the total number of "votes" for a collection of users:
const users = collect([
{ name: 'Alice', votes: 100 },
{ name: 'Bob', votes: 200 }
]);
users.sum.votes;
// 300Lazy Collections
Introduction
NOTE
Before learning more about lazy collections, take some time to familiarize yourself with JavaScript generators.
To supplement the already powerful Collection class, the LazyCollection class leverages JavaScript's generators to allow you to work with very large datasets while keeping memory usage low.
For example, imagine your application needs to process a multi-gigabyte log file while taking advantage of Laravel's collection methods to parse the logs. Instead of reading the entire file into memory at once, lazy collections may be used to keep only a small part of the file in memory at a given time:
LazyCollection.make(function* () {
const fs = require('fs');
const readline = require('readline');
const fileStream = fs.createReadStream('log.txt');
const rl = readline.createInterface({ input: fileStream });
for await (const line of rl) {
yield line;
}
}).chunk(4).map((lines) => LogEntry.fromLines(lines)).each((logEntry) => {
// Process the log entry...
});Or, imagine you need to iterate through 10,000 records. When using traditional collections, all 10,000 items must be loaded into memory at the same time:
const users = collect(allUsersArray).filter((user) => user.id > 500);However, using a lazy collection allows you to process items one at a time. In this example, the filter callback is not executed until we actually iterate over each user individually, allowing for a drastic reduction in memory usage:
const users = lazy(function* () {
for (const user of generateUsersFromSource()) {
yield user;
}
}).filter((user) => user.id > 500);
for (const user of users) {
console.log(user.id);
}Creating Lazy Collections
To create a lazy collection instance, you can use the lazy() helper function or the LazyCollection.make() method with a generator function:
import { lazy, LazyCollection } from 'collect-ts';
// Using the lazy() helper with an array
const lazyFromArray = lazy([1, 2, 3, 4, 5]);
// Using lazy() with a generator function
const lazyFromGenerator = lazy(function* () {
yield 1;
yield 2;
yield 3;
});
// Using LazyCollection.make() with a generator function
const lazyFromMake = LazyCollection.make(function* () {
for (let i = 1; i <= 1000; i++) {
yield i;
}
});
// Using static factory methods
const range = LazyCollection.range(1, 100); // Numbers 1 to 100
const times = LazyCollection.times(5, i => i * 2); // [2, 4, 6, 8, 10]
const empty = LazyCollection.empty(); // Empty lazy collectionYou can also convert an existing Collection to a LazyCollection:
import { collect } from 'collect-ts';
const lazyCollection = collect([1, 2, 3, 4, 5]).lazy();Available Methods
Almost all methods available on the Collection class are also available on the LazyCollection class. The LazyCollection implements approximately 12 truly lazy methods natively (such as map, filter, take, skip, etc.), and all other Collection methods are automatically delegated via a Proxy wrapper. This means you can use any Collection method on a LazyCollection - it will first materialize the lazy collection into a regular Collection, then call the method.
Truly lazy methods (maintain laziness through chaining):
map(),filter(),reject()take(),skip(),takeWhile(),takeUntil(),skipWhile(),skipUntil()flatMap(),chunk()tapEach(),remember(),takeUntilTimeout()
All other methods (delegated to Collection):
allaverageavgchunkchunkWhilecollapsecollectcombineconcatcontainscontainsStrictcountcountBycrossJoindddiffdiffAssocdiffKeysdumpduplicatesduplicatesStricteacheachSpreadeveryexceptfilterfirstfirstOrFailfirstWhereflatMapflattenflipforPagegetgroupByhasimplodeintersectintersectAssocintersectByKeysisEmptyisNotEmptyjoinkeyBykeyslastmacromakemapmapIntomapSpreadmapToDictionarymapToGroupsmapWithKeysmaxmedianmergemergeRecursiveminmodenthonlypadpartitionpipepluckrandomreducerejectreplacereplaceRecursivereversesearchshuffleskipslicesolesomesortsortBysortByDescsortKeyssortKeysDescsplitsumtaketaptimestoArraytoJsonunionuniqueuniqueStrictunlessunlessEmptyunlessNotEmptyunwrapvalueswhenwhenEmptywhenNotEmptywherewhereStrictwhereBetweenwhereInwhereInStrictwhereInstanceOfwhereNotBetweenwhereNotInwhereNotInStrictwrapzip
WARNING
Methods that mutate the collection (such as shift, pop, prepend etc.) are not available on the LazyCollection class.
Lazy Collection Methods
In addition to the methods defined in the Enumerable contract, the LazyCollection class contains the following methods:
takeUntilTimeout()
The takeUntilTimeout method returns a new lazy collection that will enumerate values until the specified time. After that time, the collection will then stop enumerating:
const lazyCollection = LazyCollection.times(Infinity)
.takeUntilTimeout(new Date(Date.now() + 60000));
lazyCollection.each((number) => {
console.log(number);
// sleep for 1 second
});
// 1
// 2
// ...
// 58
// 59To illustrate the usage of this method, imagine an application that submits invoices. You could set up a task that runs every 15 minutes and only processes invoices for a maximum of 14 minutes:
const processStart = Date.now();
const timeoutMs = 14 * 60 * 1000; // 14 minutes
lazy(function* () {
for (const invoice of fetchPendingInvoices()) {
yield invoice;
}
})
.takeUntilTimeout(new Date(processStart + timeoutMs))
.each((invoice) => submitInvoice(invoice));tapEach()
While the each method calls the given callback for each item in the collection right away, the tapEach method only calls the given callback as the items are being pulled out of the list one by one:
// Nothing has been dumped so far...
const lazyCollection = LazyCollection.times(Infinity).tapEach((value) => {
console.log(value);
});
// Three items are dumped...
const items = lazyCollection.take(3).all();
// 1
// 2
// 3remember()
The remember method returns a new lazy collection that will remember any values that have already been enumerated and will not retrieve them again on subsequent collection enumerations:
let computeCount = 0;
const lazyCollection = lazy(function* () {
for (let i = 1; i <= 10; i++) {
computeCount++;
yield i;
}
}).remember();
// First iteration - values are computed
lazyCollection.take(5).all(); // [1, 2, 3, 4, 5]
console.log(computeCount); // 5
// Second iteration - first 5 values come from cache
lazyCollection.take(8).all(); // [1, 2, 3, 4, 5, 6, 7, 8]
console.log(computeCount); // 8 (only 3 new values computed)