collect.js
Advanced tools
Comparing version 3.0.10 to 3.0.11
{ | ||
"name": "collect.js", | ||
"version": "3.0.10", | ||
"version": "3.0.11", | ||
"description": "Convenient and dependency free wrapper for working with arrays and objects.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -50,2 +50,3 @@ # <img src="https://raw.githubusercontent.com/ecrmnn/collect.js/master/collectjs.jpg" alt="collect.js"> | ||
- [last](#last) | ||
- [macro](#macro) | ||
- [map](#map) | ||
@@ -102,3 +103,2 @@ - [mapWithKeys](#mapwithkeys) | ||
- ~~``containsStrict``~~ use ``contains()`` | ||
- ~~``toArray``~~ use ``all()`` | ||
- ~~``uniqueStrict``~~ use ``unique()`` | ||
@@ -790,2 +790,20 @@ - ~~``whereStrict``~~ use ``where()`` | ||
#### ``macro()`` | ||
The macro method lets you register custom methods | ||
```js | ||
collect().macro('uppercase', function () { | ||
return this.map(function (item) { | ||
return item.toUpperCase(); | ||
}); | ||
}); | ||
const collection = collect(['a', 'b', 'c']); | ||
collection.uppercase(); | ||
collection.all(); | ||
//=> ['A', 'B', 'C'] | ||
``` | ||
#### ``map()`` | ||
@@ -1463,5 +1481,27 @@ 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: | ||
#### ``toArray()`` | ||
> Use ``all`` instead of ``toArray`` | ||
The toArray method converts the collection into a plain array. | ||
If the collection is an object, an array containing the values will be returned. | ||
```js | ||
const collection = collect([1, 2, 3, 'b', 'c']); | ||
collection.toArray(); | ||
//=> [1, 2, 3, 'b', 'c'] | ||
``` | ||
```js | ||
const collection = collect({ | ||
name: 'Elon Musk', | ||
companies: [ | ||
'Tesla', | ||
'Space X', | ||
'SolarCity' | ||
] | ||
}); | ||
collection.toArray(); | ||
//=> ['Elon Musk', ['Tesla', 'Space X', 'SolarCity']] | ||
``` | ||
#### ``toJson()`` | ||
@@ -1468,0 +1508,0 @@ The toJson method converts the collection into JSON string: |
@@ -101,3 +101,3 @@ 'use strict'; | ||
const _values = values.filter(function (value) { | ||
if ( key !== undefined) { | ||
if (key !== undefined) { | ||
return value.key === item[key]; | ||
@@ -260,3 +260,3 @@ } | ||
Collection.prototype.where = function (key, operator, value) { | ||
if ( value === undefined) { | ||
if (value === undefined) { | ||
value = operator; | ||
@@ -267,3 +267,3 @@ operator = '==='; | ||
return new Collection( | ||
this.items.filter(function(item) { | ||
this.items.filter(function (item) { | ||
switch (operator) { | ||
@@ -331,3 +331,3 @@ case '==': | ||
Collection.prototype.pluck = function (value, key) { | ||
if ( key !== undefined) { | ||
if (key !== undefined) { | ||
const collection = {}; | ||
@@ -352,3 +352,3 @@ | ||
Collection.prototype.implode = function (key, glue) { | ||
if ( glue === undefined) { | ||
if (glue === undefined) { | ||
return this.items.join(key); | ||
@@ -470,3 +470,3 @@ } | ||
Collection.prototype.contains = function (key, value) { | ||
if ( value !== undefined) { | ||
if (value !== undefined) { | ||
return (this.items.hasOwnProperty(key) && this.items[key] === value); | ||
@@ -901,2 +901,14 @@ } | ||
Collection.prototype.toArray = function () { | ||
if (Array.isArray(this.items)) { | ||
return this.all(); | ||
} | ||
return this.values().all(); | ||
}; | ||
Collection.prototype.macro = function (name, fn) { | ||
Collection.prototype[name] = fn; | ||
}; | ||
Collection.prototype[Symbol.iterator] = function () { | ||
@@ -903,0 +915,0 @@ let index = 0; |
@@ -1647,2 +1647,41 @@ 'use strict'; | ||
it('should be able to register a custom macro/method', function () { | ||
collect().macro('uppercase', function () { | ||
return this.map(function (item) { | ||
return item.toUpperCase(); | ||
}); | ||
}); | ||
const collection = collect(['a', 'b', 'c']); | ||
expect(collection.uppercase().all()).to.eql(['A', 'B', 'C']); | ||
expect(collection.all()).to.eql(['a', 'b', 'c']); | ||
collect().macro('prefix', function (prefix) { | ||
return this.map(function (item) { | ||
return prefix + item; | ||
}); | ||
}); | ||
expect(collect(['a', 'b', 'c']).prefix('xoxo').all()).to.eql(['xoxoa', 'xoxob', 'xoxoc']); | ||
}); | ||
it('should convert the collection into a plain array', function () { | ||
const collectionArray = collect([1, 2, 3, 'b', 'c', 'ø']); | ||
expect(collectionArray.toArray()).to.eql([1, 2, 3, 'b', 'c', 'ø']); | ||
expect(collectionArray.toArray()).to.eql(collectionArray.all()); | ||
const collectionObject = collect({ | ||
name: 'Elon Musk', | ||
companies: [ | ||
'Tesla', | ||
'Space X', | ||
'SolarCity' | ||
] | ||
}); | ||
expect(collectionObject.toArray()).to.eql(['Elon Musk', ['Tesla', 'Space X', 'SolarCity']]); | ||
expect(collectionObject.toArray()).to.eql(collectionObject.values().all()); | ||
}); | ||
it('should be iterable', function () { | ||
@@ -1649,0 +1688,0 @@ let result = ''; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
220481
2082
1737