collect.js
Advanced tools
Comparing version 1.0.3 to 2.0.0
{ | ||
"name": "collect.js", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "Convenient and dependency free wrapper for working with arrays and objects.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
124
README.md
@@ -17,29 +17,73 @@ # <img src="https://raw.githubusercontent.com/ecrmnn/collect.js/master/collectjs.jpg" alt="collect.js"> | ||
### Tip | ||
Using Laravel as your backend? Collect.js offers an identical api to Laravel Collections | ||
Using Laravel as your backend? Collect.js offers an identical api to Laravel Collections 5.4. | ||
### Usage | ||
All | available | methods | ||
---|---|--- | ||
[all](#all) | [implode](#implode) | [reverse](#reverse) | ||
[avg](#avg) | [intersect](#intersect) | [search](#search) | ||
[chunk](#chunk) | [isEmpty](#isempty) | [shift](#shift) | ||
[collapse](#collapse) | [keyBy](#keyby) | [shuffle](#shuffle) | ||
[combine](#combine) | [keys](#keys) | [slice](#slice) | ||
[contains](#contains) | [last](#last) | [sort](#sort) | ||
[count](#count) | [map](#map) | [sortBy](#sortby) | ||
[diff](#diff) | [mapWithKeys](#mapwithkeys) | [sortByDesc](#sortbydesc) | ||
[diffKeys](#diffkeys) | [max](#max) | [splice](#splice) | ||
[each](#each) | [merge](#merge) | [sum](#sum) | ||
[every](#every) | [min](#min) | [take](#take) | ||
[except](#except) | [only](#only) | [toArray](#toarray) | ||
[filter](#filter) | [pipe](#pipe) | [toJson](#tojson) | ||
[first](#first) | [pluck](#pluck) | [transform](#transform) | ||
[flatMap](#flatmap) | [pop](#pop) | [union](#union) | ||
[flatten](#flatten) | [prepend](#prepend) | [unique](#unique) | ||
[flip](#flip) | [pull](#pull) | [values](#values) | ||
[forget](#forget) | [push](#push) | [where](#where) | ||
[forPage](#forpage) | [put](#put) | [whereStrict](#wherestrict) | ||
[get](#get) | [random](#random) | [whereIn](#wherein) | ||
[groupBy](#groupby) | [reduce](#reduce) | [whereInLoose](#whereinloose) | ||
[has](#has) | [reject](#reject) | [zip](#zip) | ||
[all](#all) | ||
[avg](#avg) | ||
[chunk](#chunk) | ||
[collapse](#collapse) | ||
[combine](#combine) | ||
[contains](#contains) | ||
[count](#count) | ||
[diff](#diff) | ||
[diffKeys](#diffkeys) | ||
[each](#each) | ||
[every](#every) | ||
[except](#except) | ||
[filter](#filter) | ||
[first](#first) | ||
[flatMap](#flatmap) | ||
[flatten](#flatten) | ||
[flip](#flip) | ||
[forget](#forget) | ||
[forPage](#forpage) | ||
[get](#get) | ||
[groupBy](#groupby) | ||
[has](#has) | ||
[implode](#implode) | ||
[intersect](#intersect) | ||
[isEmpty](#isempty) | ||
[keyBy](#keyby) | ||
[keys](#keys) | ||
[last](#last) | ||
[map](#map) | ||
[mapWithKeys](#mapwithkeys) | ||
[max](#max) | ||
[merge](#merge) | ||
[min](#min) | ||
[nth](#nth) | ||
[only](#only) | ||
[pipe](#pipe) | ||
[pluck](#pluck) | ||
[pop](#pop) | ||
[prepend](#prepend) | ||
[pull](#pull) | ||
[push](#push) | ||
[put](#put) | ||
[random](#random) | ||
[reduce](#reduce) | ||
[reject](#reject) | ||
[reverse](#reverse) | ||
[search](#search) | ||
[shift](#shift) | ||
[shuffle](#shuffle) | ||
[slice](#slice) | ||
[sort](#sort) | ||
[sortBy](#sortby) | ||
[sortByDesc](#sortbydesc) | ||
[splice](#splice) | ||
[sum](#sum) | ||
[take](#take) | ||
[toArray](#toarray) | ||
[toJson](#tojson) | ||
[transform](#transform) | ||
[union](#union) | ||
[unique](#unique) | ||
[values](#values) | ||
[where](#where) | ||
[whereIn](#wherein) | ||
[whereInLoose](#whereinloose) | ||
[whereStrict](#wherestrict) | ||
[zip](#zip) | ||
@@ -235,21 +279,11 @@ #### ``all()`` | ||
#### ``every()`` | ||
The every method creates a new collection consisting of every n-th element: | ||
The every method may be used to verify that all elements of a collection pass a given truth test: | ||
```js | ||
const collection = collect([ | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' | ||
]); | ||
collect([1, 2, 3, 4]).every(function (value, key) { | ||
return value > 2; | ||
}); | ||
collection.every(4); | ||
//=> ['a', 'e', 'i'] | ||
//=> false | ||
``` | ||
You may optionally pass an offset as the second argument: | ||
```js | ||
collection.every(4, 1); | ||
//=> ['b', 'f'] | ||
collection.every(4, 3); | ||
//=> ['d', 'h'] | ||
``` | ||
#### ``except()`` | ||
@@ -834,2 +868,14 @@ The except method returns all items in the collection except for those with the specified keys: | ||
#### ``nth()`` | ||
The nth method creates a new collection consisting of every n-th element: | ||
```js | ||
const collection = collect(['a', 'b', 'c', 'd', 'e', 'f']); | ||
const nth = collection.nth(4); | ||
nth.all(); | ||
//=> ['a', 'e'] | ||
``` | ||
#### ``only()`` | ||
@@ -836,0 +882,0 @@ The only method returns the items in the collection with the specified keys: |
@@ -452,3 +452,7 @@ 'use strict'; | ||
Collection.prototype.every = function (n, offset) { | ||
Collection.prototype.every = function (fn) { | ||
return this.items.filter(fn).length === this.items.length; | ||
} | ||
Collection.prototype.nth = function (n, offset) { | ||
if (offset === undefined) { | ||
@@ -458,3 +462,3 @@ offset = 0; | ||
return Object.create(this.items) | ||
const collection = this.items | ||
.slice(offset) | ||
@@ -464,2 +468,4 @@ .filter(function (item, index) { | ||
}); | ||
return new Collection(collection); | ||
} | ||
@@ -466,0 +472,0 @@ |
@@ -911,15 +911,30 @@ 'use strict'; | ||
it('The every method may be used to verify that all elements of a collection pass a given truth test', function () { | ||
const collection = collect([1, 2, 3, 4]); | ||
const shouldBeFalse = collection.every(function (value, key) { | ||
return value > 2; | ||
}); | ||
expect(shouldBeFalse).to.eql(false); | ||
const shouldBeTrue = collection.every(function (value, key) { | ||
return value <= 4; | ||
}); | ||
expect(shouldBeTrue).to.eql(true); | ||
}); | ||
it('should create a new collection consisting of every n-th element', function () { | ||
const collection = collect(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']); | ||
const every_4 = collection.every(4); | ||
const nth_4 = collection.nth(4); | ||
const every_4_offset_1 = collection.every(4, 1); | ||
const nth_4_offset_1 = collection.nth(4, 1); | ||
const every_4_offset_3 = collection.every(4, 3); | ||
const nth_4_offset_3 = collection.nth(4, 3); | ||
expect(every_4).to.eql(['a', 'e', 'i']); | ||
expect(every_4_offset_1).to.eql(['b', 'f']); | ||
expect(every_4_offset_3).to.eql(['d', 'h']); | ||
expect(nth_4.all()).to.eql(['a', 'e', 'i']); | ||
expect(nth_4_offset_1.all()).to.eql(['b', 'f']); | ||
expect(nth_4_offset_3.all()).to.eql(['d', 'h']); | ||
}); | ||
@@ -926,0 +941,0 @@ |
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
208757
1839
1547