collect.js
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "collect.js", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Convenient and dependency free wrapper for working with arrays and objects.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -181,2 +181,9 @@ # <img src="https://raw.githubusercontent.com/ecrmnn/collect.js/master/collectjs.jpg" alt="collect.js"> | ||
``` | ||
You may also work with arrays | ||
```js | ||
const collection = collect([1, 2, 3]); | ||
collection.contains(3); | ||
//=> true | ||
``` | ||
You may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection: | ||
@@ -183,0 +190,0 @@ ```js |
@@ -425,3 +425,3 @@ 'use strict'; | ||
Collection.prototype.contains = function (key, value) { | ||
if (typeof value === 'string') { | ||
if (typeof value !== 'undefined') { | ||
return (this.items.hasOwnProperty(key) && this.items[key] === value); | ||
@@ -436,2 +436,6 @@ } | ||
if (Array.isArray(this.items)) { | ||
return this.items.indexOf(key) !== -1; | ||
} | ||
return this.items.hasOwnProperty(key); | ||
@@ -438,0 +442,0 @@ } |
@@ -27,7 +27,7 @@ 'use strict'; | ||
const collection = 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'} | ||
{ 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' } | ||
]); | ||
@@ -38,4 +38,4 @@ | ||
expect(unique.all()).to.eql([ | ||
{name: 'iPhone 6', brand: 'Apple', type: 'phone'}, | ||
{name: 'Galaxy S6', brand: 'Samsung', type: 'phone'}, | ||
{ name: 'iPhone 6', brand: 'Apple', type: 'phone' }, | ||
{ name: 'Galaxy S6', brand: 'Samsung', type: 'phone' }, | ||
]); | ||
@@ -48,14 +48,14 @@ | ||
expect(unique2.all()).to.eql([ | ||
{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'}, | ||
{ 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' }, | ||
]); | ||
expect(collection.all()).to.eql([ | ||
{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'} | ||
{ 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' } | ||
]); | ||
@@ -69,4 +69,4 @@ }); | ||
const collection2 = collect([ | ||
{name: 'JavaScript: The Good Parts', pages: 176}, | ||
{name: 'JavaScript: The Definitive Guide', pages: 1096}, | ||
{ name: 'JavaScript: The Good Parts', pages: 176 }, | ||
{ name: 'JavaScript: The Definitive Guide', pages: 1096 }, | ||
]); | ||
@@ -77,5 +77,5 @@ | ||
const collection3 = collect([ | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
@@ -87,5 +87,5 @@ const summed = collection3.sum(function (product) { | ||
expect(collection3.all()).to.eql([ | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
@@ -266,9 +266,9 @@ }); | ||
const collection2 = collect([{a: 'a'}, {b: 'b'}]); | ||
const collection2 = collect([{ a: 'a' }, { b: 'b' }]); | ||
const first2 = collection2.first(); | ||
expect(first2).to.eql({a: 'a'}); | ||
expect(first2).to.eql({ a: 'a' }); | ||
expect(collection2.all()).to.eql([{a: 'a'}, {b: 'b'}]); | ||
expect(collection2.all()).to.eql([{ a: 'a' }, { b: 'b' }]); | ||
@@ -292,6 +292,6 @@ const collection3 = collect([1, 2, 3]); | ||
const collection2 = collect([{a: 'a'}, {b: 'b'}]); | ||
const collection2 = collect([{ a: 'a' }, { b: 'b' }]); | ||
const last2 = collection2.last(); | ||
expect(last2).to.eql({b: 'b'}); | ||
expect(collection2.all()).to.eql([{a: 'a'}, {b: 'b'}]); | ||
expect(last2).to.eql({ b: 'b' }); | ||
expect(collection2.all()).to.eql([{ a: 'a' }, { b: 'b' }]); | ||
@@ -457,3 +457,3 @@ const collection3 = collect([1, 2, 3]); | ||
expect(only.all()).to.eql({id: 100, product: 'Chair'}); | ||
expect(only.all()).to.eql({ id: 100, product: 'Chair' }); | ||
@@ -464,3 +464,3 @@ expect(collection.all()).to.eql(dataset.products[0]); | ||
it('should return everything except specified properties', function () { | ||
expect(collect(dataset.products[0]).except(['id', 'product'])).to.eql({manufacturer: 'IKEA', price: '1490 NOK'}); | ||
expect(collect(dataset.products[0]).except(['id', 'product'])).to.eql({ manufacturer: 'IKEA', price: '1490 NOK' }); | ||
}); | ||
@@ -491,6 +491,6 @@ | ||
const collection2 = collect([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Chair', 'price': 100}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': '100'}, | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Chair', 'price': 100 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': '100' }, | ||
]); | ||
@@ -500,3 +500,3 @@ | ||
expect(filtered2.all()).to.eql([{'product': 'Chair', 'price': 100}, {'product': 'Door', 'price': '100'}]); | ||
expect(filtered2.all()).to.eql([{ 'product': 'Chair', 'price': 100 }, { 'product': 'Door', 'price': '100' }]); | ||
@@ -506,5 +506,5 @@ const filtered3 = collection2.where('price', '!==', 100); | ||
expect(filtered3.all()).to.eql([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': '100'}, | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': '100' }, | ||
]); | ||
@@ -515,4 +515,4 @@ | ||
expect(filtered4.all()).to.eql([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Bookcase', 'price': 150} | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Bookcase', 'price': 150 } | ||
]); | ||
@@ -527,6 +527,6 @@ | ||
const collection = collect([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Chair', 'price': 100}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': '100'}, | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Chair', 'price': 100 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': '100' }, | ||
]); | ||
@@ -536,9 +536,9 @@ | ||
expect(filtered.all()).to.eql([{'product': 'Chair', 'price': 100}]); | ||
expect(filtered.all()).to.eql([{ 'product': 'Chair', 'price': 100 }]); | ||
expect(collection.all()).to.eql([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Chair', 'price': 100}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': '100'}, | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Chair', 'price': 100 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': '100' }, | ||
]); | ||
@@ -570,6 +570,6 @@ }); | ||
const collection2 = collect([ | ||
{'product': 'Desk', 'price': 200}, | ||
{'product': 'Chair', 'price': 100}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': 100}, | ||
{ 'product': 'Desk', 'price': 200 }, | ||
{ 'product': 'Chair', 'price': 100 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': 100 }, | ||
]); | ||
@@ -580,5 +580,5 @@ | ||
expect(filtered2.all()).to.eql([ | ||
{'product': 'Chair', 'price': 100}, | ||
{'product': 'Bookcase', 'price': 150}, | ||
{'product': 'Door', 'price': 100}, | ||
{ 'product': 'Chair', 'price': 100 }, | ||
{ 'product': 'Bookcase', 'price': 150 }, | ||
{ 'product': 'Door', 'price': 100 }, | ||
]); | ||
@@ -638,3 +638,3 @@ }); | ||
expect(a.pull('firstname')).to.eql('Daniel'); | ||
expect(a.all()).to.eql({lastname: 'Eckermann'}); | ||
expect(a.all()).to.eql({ lastname: 'Eckermann' }); | ||
@@ -822,3 +822,3 @@ expect(b.pull('name')).to.eql(null); | ||
expect(merged2.all()).to.eql({id: 1, price: 400, discount: false}); | ||
expect(merged2.all()).to.eql({ id: 1, price: 400, discount: false }); | ||
@@ -889,2 +889,5 @@ const collection3 = collect(['Unicorn', 'Rainbow']); | ||
const contains8 = collection.contains('number', 28); | ||
expect(contains8).to.eql(false); | ||
const contains4 = collection.contains('name', 'Steve Jobs'); | ||
@@ -904,2 +907,5 @@ expect(contains4).to.eql(false); | ||
expect(contains6).to.eql(true); | ||
const collection7 = collect([1, 2, 3, 4]); | ||
expect(collection7.contains(4)).to.eql(true); | ||
}); | ||
@@ -925,3 +931,3 @@ | ||
expect(_diff.all()).to.eql({a: 'a', c: 'c'}); | ||
expect(_diff.all()).to.eql({ a: 'a', c: 'c' }); | ||
expect(collection.all()).to.eql(data); | ||
@@ -931,3 +937,3 @@ | ||
const _diff2 = collection.diffKeys(diffCollection); | ||
expect(_diff2.all()).to.eql({a: 'a', c: 'c'}); | ||
expect(_diff2.all()).to.eql({ a: 'a', c: 'c' }); | ||
}); | ||
@@ -1005,6 +1011,6 @@ | ||
Apple: [ | ||
{name: 'iPhone 6S', brand: 'Apple'}, | ||
{ name: 'iPhone 6S', brand: 'Apple' }, | ||
], | ||
Samsung: [ | ||
{name: 'Galaxy S7', brand: 'Samsung'} | ||
{ name: 'Galaxy S7', brand: 'Samsung' } | ||
] | ||
@@ -1019,4 +1025,4 @@ }; | ||
.to.eql([ | ||
{name: 'iPhone 6S', brand: 'Apple'}, | ||
{name: 'Galaxy S7', brand: 'Samsung'} | ||
{ name: 'iPhone 6S', brand: 'Apple' }, | ||
{ name: 'Galaxy S7', brand: 'Samsung' } | ||
]); | ||
@@ -1309,5 +1315,5 @@ | ||
const collection = collect([ | ||
{'name': 'Desk', 'price': 200}, | ||
{'name': 'Chair', 'price': 100}, | ||
{'name': 'Bookcase', 'price': 150}, | ||
{ 'name': 'Desk', 'price': 200 }, | ||
{ 'name': 'Chair', 'price': 100 }, | ||
{ 'name': 'Bookcase', 'price': 150 }, | ||
]); | ||
@@ -1318,11 +1324,11 @@ | ||
expect(sorted.all()).to.eql([ | ||
{'name': 'Chair', 'price': 100}, | ||
{'name': 'Bookcase', 'price': 150}, | ||
{'name': 'Desk', 'price': 200}, | ||
{ 'name': 'Chair', 'price': 100 }, | ||
{ 'name': 'Bookcase', 'price': 150 }, | ||
{ 'name': 'Desk', 'price': 200 }, | ||
]); | ||
const collection2 = collect([ | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
@@ -1335,11 +1341,11 @@ | ||
expect(sorted2.all()).to.eql([ | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
expect(collection2.all()).to.eql([ | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
@@ -1350,5 +1356,5 @@ }); | ||
const collection = collect([ | ||
{'name': 'Desk', 'price': 200}, | ||
{'name': 'Chair', 'price': 100}, | ||
{'name': 'Bookcase', 'price': 150}, | ||
{ 'name': 'Desk', 'price': 200 }, | ||
{ 'name': 'Chair', 'price': 100 }, | ||
{ 'name': 'Bookcase', 'price': 150 }, | ||
]); | ||
@@ -1359,11 +1365,11 @@ | ||
expect(sorted.all()).to.eql([ | ||
{'name': 'Desk', 'price': 200}, | ||
{'name': 'Bookcase', 'price': 150}, | ||
{'name': 'Chair', 'price': 100}, | ||
{ 'name': 'Desk', 'price': 200 }, | ||
{ 'name': 'Bookcase', 'price': 150 }, | ||
{ 'name': 'Chair', 'price': 100 }, | ||
]); | ||
const collection2 = collect([ | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
]); | ||
@@ -1376,11 +1382,11 @@ | ||
expect(sorted2.all()).to.eql([ | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
]); | ||
expect(collection2.all()).to.eql([ | ||
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}, | ||
{'name': 'Chair', 'colors': ['Black']}, | ||
{'name': 'Desk', 'colors': ['Black', 'Mahogany']}, | ||
{ 'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown'] }, | ||
{ 'name': 'Chair', 'colors': ['Black'] }, | ||
{ 'name': 'Desk', 'colors': ['Black', 'Mahogany'] }, | ||
]); | ||
@@ -1523,3 +1529,3 @@ }); | ||
it('should return the object values from the collection', function () { | ||
const collection = collect({a: 'xoxo', b: 'abab', 'c': '1337', 1337: 12}); | ||
const collection = collect({ a: 'xoxo', b: 'abab', 'c': '1337', 1337: 12 }); | ||
@@ -1530,4 +1536,4 @@ const values = collection.values(); | ||
expect(collection.all()).to.eql({a: 'xoxo', b: 'abab', 'c': '1337', 1337: 12}); | ||
expect(collection.all()).to.eql({ a: 'xoxo', b: 'abab', 'c': '1337', 1337: 12 }); | ||
}); | ||
}); |
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
209486
1846
1554