- Added build files so it's no longer required to use npm for installation
chunk()
count()
dump()
flatMap()
has()
keys()
groupBy()
partition()
pluck()
split()
toArray()
toJson()
wrap()
Skipped Node 4 support
- Returns a new collection of smaller collections of the given size.
This is done because
collect.js
should give the same result as Laravel Collections. - Also works when the collection is based on an object, a string, a number or boolean.
- Also works when the collection is based on a string
- Also works when combining with a string or an object
- Also works when combining with another collection
- Also works when the collection is based on an object
- Return the number of keys in the object
- Console logs the entire collection object (
this
) instead of only the items (this.items
).
const collection = collect([
{ product: 'Desk', manufacturer: 'IKEA' },
{ product: 'Chair', manufacturer: 'Herman Miller' },
{ product: 'Bookcase', manufacturer: 'IKEA' },
{ product: 'Door' },
]);
collection.pluck('product', 'manufacturer').dump();
// Prior to 4.0.0
//=> {
//=> IKEA: 'Bookcase',
//=> 'Herman Miller': 'Chair',
//=> '': 'Door'
//=> }
// After 4.0.0
//=> Collection {
//=> items: {
//=> IKEA: 'Bookcase',
//=> 'Herman Miller': 'Chair',
//=> '': 'Door'
//=> }
//=> }
- Accepts an array or infinite number of arguments.
- Also works when the collection is based on an object.
const collection = collect({
name: 'Sadio Mané',
club: 'Liverpool FC',
});
collection.first();
//=> Sadio Mané
- Version prior to 4.0.0 did not work as expected
- Rewritten with new functionality
- See readme for further details
- Also works when the collection is based on an object
- Also works when the collection is based on an object
- Also works when the collection is based on an object
- Objects that don't have the key that we're grouping by will be grouped into a group under the
name of an empty string. This is changed from being grouped under
undefined
. - Now returns a collection of collections instead of an array of objects.
This is done because
collect.js
should give the same result as Laravel Collections.
- Accepts an array of keys to check
- Is now a variadic function and therefore accepts infinite number of arguments (keys) to check
- No longer checks if any object in the given array has the specified key.
This is done because
collect.js
should give the same result as Laravel Collections.
// Previously this would return true. It now returns false.
const collection = collect([{
animal: 'unicorn',
ability: 'magical'
}, {
animal: 'pig',
ability: 'filthy'
}]);
collection.has('ability');
//=> true (Prior to 4.0.0)
//=> false (After 4.0.0)
- Uses an empty string as the key instead of
undefined
when passed an invalid key
- Returns indexes as keys when based on an array. Indexes are mapped to
Number
.
const collection = collect([{
name: 'Sadio Mané',
}, {
name: 'Roberto Firmino',
}]);
const keys = collection.keys();
// Prior to 4.0.0
//=> ['name', 'name']
// After 4.0.0
//=> [0, 1]
- Also works when the collection is based on an object.
const collection = collect({
name: 'Sadio Mané',
club: 'Liverpool FC',
});
collection.last();
//=> Liverpool FC
- Can merge arrays and objects.
- Also works when merging with a string.
- Accepts an array or infinite number of arguments.
- Returns a collection of collections with the results instead of an array
- Returns
null
as the value instead ofundefined
- Returns
null
when an item does not contain the specified key.
const collection = collect([
{ product: 'Desk', manufacturer: 'IKEA' },
{ product: 'Chair', manufacturer: 'Herman Miller' },
{ product: 'Bookcase', manufacturer: 'IKEA' },
{ product: 'Door' },
]);
const pluck = collection.pluck('non-existing-key');
pluck.all();
//=> [null, null, null, null]
const manufacturers = collection.pluck('manufacturer');
manufacturers.all();
//=> ['IKEA', 'Herman Miller', 'IKEA', null]
- Objects that don't have the key that we're plucking by will get an empty string as its key.
This is changed from being
undefined
.
const collection = collect([
{ product: 'Desk', manufacturer: 'IKEA' },
{ product: 'Chair', manufacturer: 'Herman Miller' },
{ product: 'Bookcase', manufacturer: 'IKEA' },
{ product: 'Door' },
]);
const pluck = collection.pluck('product', 'manufacturer');
pluck.all();
//=> {
//=> IKEA: 'Bookcase',
//=> 'Herman Miller': 'Chair',
//=> '': 'Door',
//=> }
- Also works when collection is based on an object
- Accepts spread/rest operator
collection.push(...values)
- Also works when collection is based on an object
- Also works when collection is based on an object
- Also works when collection is based on an object
- Splits the collection into the given number of collections
const collection = collect([1, 2, 3, 4, 5]);
collection.split(2).dump();
// Prior to 4.0.0
//=> [
//=> [1, 2, 3],
//=> [4, 5],
//=> ]
// After 4.0.0
//=> Collection {
//=> items: {
//=> Collection {
//=> items: [1, 2, 3]
//=> },
//=> Collection {
//=> items: [4, 5]
//=> },
//=> }
//=> }
- Also works when collection is based on an object
- Now works recursively like Laravel collections
toArray()
method - More information: ecrmnn#138
- Now works recursively like Laravel collections
toArray()
method - More information: ecrmnn#138
- Now wraps objects correctly. The key/values are places directly on the collection. Previously objects were wrapped in an array.
- Added
CHANGELOG.md