collect.js
Advanced tools
Comparing version 3.1.2 to 4.0.0
@@ -29,2 +29,3 @@ 'use strict'; | ||
Collection.prototype.each = require('./methods/each'); | ||
Collection.prototype.eachSpread = require('./methods/eachSpread'); | ||
Collection.prototype.every = require('./methods/every'); | ||
@@ -34,2 +35,3 @@ Collection.prototype.except = require('./methods/except'); | ||
Collection.prototype.first = require('./methods/first'); | ||
Collection.prototype.firstWhere = require('./methods/firstWhere'); | ||
Collection.prototype.flatMap = require('./methods/flatMap'); | ||
@@ -53,2 +55,4 @@ Collection.prototype.flatten = require('./methods/flatten'); | ||
Collection.prototype.map = require('./methods/map'); | ||
Collection.prototype.mapSpread = require('./methods/mapSpread'); | ||
Collection.prototype.mapToDictionary = require('./methods/mapToDictionary'); | ||
Collection.prototype.mapInto = require('./methods/mapInto'); | ||
@@ -64,2 +68,3 @@ Collection.prototype.mapToGroups = require('./methods/mapToGroups'); | ||
Collection.prototype.only = require('./methods/only'); | ||
Collection.prototype.pad = require('./methods/pad'); | ||
Collection.prototype.partition = require('./methods/partition'); | ||
@@ -66,0 +71,0 @@ Collection.prototype.pipe = require('./methods/pipe'); |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
module.exports = function chunk(size) { | ||
var _this = this; | ||
var chunks = []; | ||
var index = 0; | ||
do { | ||
chunks.push(this.items.slice(index, index + size)); | ||
index += size; | ||
} while (index < this.items.length); | ||
if (Array.isArray(this.items)) { | ||
do { | ||
var items = this.items.slice(index, index + size); | ||
var collection = new this.constructor(items); | ||
chunks.push(collection); | ||
index += size; | ||
} while (index < this.items.length); | ||
} else if (_typeof(this.items) === 'object') { | ||
var keys = Object.keys(this.items); | ||
var _loop = function _loop() { | ||
var keysOfChunk = keys.slice(index, index + size); | ||
var collection = new _this.constructor({}); | ||
keysOfChunk.forEach(function (key) { | ||
return collection.put(key, _this.items[key]); | ||
}); | ||
chunks.push(collection); | ||
index += size; | ||
}; | ||
do { | ||
_loop(); | ||
} while (index < keys.length); | ||
} else { | ||
chunks.push(new this.constructor([this.items])); | ||
} | ||
return new this.constructor(chunks); | ||
}; |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
module.exports = function combine(array) { | ||
var _this = this; | ||
var values = array; | ||
if (values instanceof this.constructor) { | ||
values = array.all(); | ||
} | ||
var collection = {}; | ||
this.items.forEach(function (key, iterator) { | ||
collection[key] = array[iterator]; | ||
}); | ||
if (Array.isArray(this.items) && Array.isArray(values)) { | ||
this.items.forEach(function (key, iterator) { | ||
collection[key] = values[iterator]; | ||
}); | ||
} else if (_typeof(this.items) === 'object' && (typeof values === 'undefined' ? 'undefined' : _typeof(values)) === 'object') { | ||
Object.keys(this.items).forEach(function (key, index) { | ||
collection[_this.items[key]] = values[Object.keys(values)[index]]; | ||
}); | ||
} else if (Array.isArray(this.items)) { | ||
collection[this.items[0]] = values; | ||
} else if (typeof this.items === 'string' && Array.isArray(values)) { | ||
collection[this.items] = values[0]; | ||
} else if (typeof this.items === 'string') { | ||
collection[this.items] = values; | ||
} | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function count() { | ||
return this.items.length; | ||
if (Array.isArray(this.items)) { | ||
return this.items.length; | ||
} | ||
return Object.keys(this.items).length; | ||
}; |
'use strict'; | ||
module.exports = function dump() { | ||
console.log(this.items); | ||
console.log(this); | ||
return this; | ||
}; |
'use strict'; | ||
var values = require('../helpers/values'); | ||
module.exports = function every(fn) { | ||
return this.items.map(function (item, index) { | ||
var items = values(this.items); | ||
return items.map(function (item, index) { | ||
return fn(item, index); | ||
}).indexOf(false) === -1; | ||
}; |
'use strict'; | ||
module.exports = function except(properties) { | ||
var variadic = require('../helpers/variadic'); | ||
module.exports = function except() { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var properties = variadic(args); | ||
if (Array.isArray(this.items)) { | ||
var _collection = this.items.filter(function (item) { | ||
return properties.indexOf(item); | ||
return properties.indexOf(item) === -1; | ||
}); | ||
@@ -10,0 +18,0 @@ |
@@ -13,3 +13,7 @@ 'use strict'; | ||
return this.items[0]; | ||
if (Array.isArray(this.items)) { | ||
return this.items[0]; | ||
} | ||
return this.items[Object.keys(this.items)[0]]; | ||
}; |
@@ -8,7 +8,13 @@ 'use strict'; | ||
Object.keys(this.items).forEach(function (key) { | ||
collection[_this.items[key]] = key; | ||
}); | ||
if (Array.isArray(this.items)) { | ||
Object.keys(this.items).forEach(function (key) { | ||
collection[_this.items[key]] = Number(key); | ||
}); | ||
} else { | ||
Object.keys(this.items).forEach(function (key) { | ||
collection[_this.items[key]] = key; | ||
}); | ||
} | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function forget(key) { | ||
delete this.items[key]; | ||
if (Array.isArray(this.items)) { | ||
this.items.splice(key, 1); | ||
} else { | ||
delete this.items[key]; | ||
} | ||
return this; | ||
}; |
'use strict'; | ||
module.exports = function forPage(page, chunk) { | ||
var collection = this.items.slice(page * chunk - chunk, page * chunk); | ||
var _this = this; | ||
var collection = {}; | ||
if (Array.isArray(this.items)) { | ||
collection = this.items.slice(page * chunk - chunk, page * chunk); | ||
} else { | ||
Object.keys(this.items).slice(page * chunk - chunk, page * chunk).forEach(function (key) { | ||
collection[key] = _this.items[key]; | ||
}); | ||
} | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function groupBy(key) { | ||
var _this = this; | ||
var collection = {}; | ||
@@ -12,7 +14,7 @@ | ||
} else { | ||
resolvedKey = item[key]; | ||
resolvedKey = item[key] || ''; | ||
} | ||
if (collection[resolvedKey] === undefined) { | ||
collection[resolvedKey] = []; | ||
collection[resolvedKey] = new _this.constructor([]); | ||
} | ||
@@ -19,0 +21,0 @@ |
'use strict'; | ||
module.exports = function has(key) { | ||
if (Array.isArray(this.items)) { | ||
for (var i = 0, length = this.items.length; i < length; i += 1) { | ||
if (this.items[i][key] !== undefined) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
var variadic = require('../helpers/variadic'); | ||
module.exports = function has() { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return this.items[key] !== undefined; | ||
var properties = variadic(args); | ||
return properties.filter(function (key) { | ||
return _this.items[key]; | ||
}).length === properties.length; | ||
}; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
this.items.forEach(function (item) { | ||
collection[item[key]] = item; | ||
collection[item[key] || ''] = item; | ||
}); | ||
@@ -15,0 +15,0 @@ } |
'use strict'; | ||
module.exports = function keys() { | ||
var collection = Object.keys(this.items); | ||
if (Array.isArray(this.items)) { | ||
var collection = []; | ||
this.items.forEach(function (object) { | ||
Object.keys(object).forEach(function (key) { | ||
collection.push(key); | ||
}); | ||
}); | ||
return new this.constructor(collection).unique(); | ||
collection = collection.map(Number); | ||
} | ||
return new this.constructor(Object.keys(this.items)); | ||
return new this.constructor(collection); | ||
}; |
@@ -10,3 +10,8 @@ 'use strict'; | ||
return this.items[this.items.length - 1]; | ||
if (Array.isArray(this.items)) { | ||
return this.items[this.items.length - 1]; | ||
} | ||
var keys = Object.keys(this.items); | ||
return this.items[keys[keys.length - 1]]; | ||
}; |
@@ -8,4 +8,4 @@ 'use strict'; | ||
this.items.forEach(function (item) { | ||
var _fn = fn(item), | ||
this.items.forEach(function (item, key) { | ||
var _fn = fn(item, key), | ||
_fn2 = _slicedToArray(_fn, 2), | ||
@@ -12,0 +12,0 @@ keyed = _fn2[0], |
'use strict'; | ||
module.exports = function merge(objectOrArray) { | ||
if (Array.isArray(objectOrArray)) { | ||
return new this.constructor(this.items.concat(objectOrArray)); | ||
module.exports = function merge(value) { | ||
var arrayOrObject = value; | ||
if (typeof arrayOrObject === 'string') { | ||
arrayOrObject = [arrayOrObject]; | ||
} | ||
if (Array.isArray(this.items) && Array.isArray(arrayOrObject)) { | ||
return new this.constructor(this.items.concat(arrayOrObject)); | ||
} | ||
var collection = JSON.parse(JSON.stringify(this.items)); | ||
Object.keys(objectOrArray).forEach(function (key) { | ||
collection[key] = objectOrArray[key]; | ||
Object.keys(arrayOrObject).forEach(function (key) { | ||
collection[key] = arrayOrObject[key]; | ||
}); | ||
@@ -13,0 +19,0 @@ |
'use strict'; | ||
module.exports = function nth(n, offset) { | ||
var ntnOffset = offset || 0; | ||
var values = require('../helpers/values'); | ||
var collection = this.items.slice(ntnOffset).filter(function (item, index) { | ||
module.exports = function nth(n) { | ||
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
var items = values(this.items); | ||
var collection = items.slice(offset).filter(function (item, index) { | ||
return index % n === 0; | ||
@@ -8,0 +12,0 @@ }); |
'use strict'; | ||
module.exports = function only(properties) { | ||
var variadic = require('../helpers/variadic'); | ||
module.exports = function only() { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var properties = variadic(args); | ||
if (Array.isArray(this.items)) { | ||
var _collection = this.items.filter(function (item) { | ||
return !properties.indexOf(item); | ||
return properties.indexOf(item) !== -1; | ||
}); | ||
@@ -10,0 +18,0 @@ |
'use strict'; | ||
module.exports = function partition(fn) { | ||
var arrays = [[], []]; | ||
var _this = this; | ||
this.items.forEach(function (item) { | ||
if (fn(item) === true) { | ||
arrays[0].push(item); | ||
} else { | ||
arrays[1].push(item); | ||
} | ||
}); | ||
var arrays = void 0; | ||
return arrays; | ||
if (Array.isArray(this.items)) { | ||
arrays = [new this.constructor([]), new this.constructor([])]; | ||
this.items.forEach(function (item) { | ||
if (fn(item) === true) { | ||
arrays[0].push(item); | ||
} else { | ||
arrays[1].push(item); | ||
} | ||
}); | ||
} else { | ||
arrays = [new this.constructor({}), new this.constructor({})]; | ||
Object.keys(this.items).forEach(function (prop) { | ||
var value = _this.items[prop]; | ||
if (fn(value) === true) { | ||
arrays[0].put(prop, value); | ||
} else { | ||
arrays[1].put(prop, value); | ||
} | ||
}); | ||
} | ||
return new this.constructor(arrays); | ||
}; |
@@ -5,18 +5,14 @@ 'use strict'; | ||
if (key !== undefined) { | ||
var _collection = {}; | ||
var collection = {}; | ||
this.items.forEach(function (item) { | ||
_collection[item[key]] = item[value]; | ||
collection[item[key] || ''] = item[value] || null; | ||
}); | ||
return new this.constructor(_collection); | ||
return new this.constructor(collection); | ||
} | ||
var collection = this.items.filter(function (item) { | ||
return item[value] !== undefined; | ||
}).map(function (item) { | ||
return item[value]; | ||
return this.map(function (item) { | ||
return item[value] || null; | ||
}); | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function pop() { | ||
return this.items.pop(); | ||
if (Array.isArray(this.items)) { | ||
return this.items.pop(); | ||
} | ||
var keys = Object.keys(this.items); | ||
var key = keys[keys.length - 1]; | ||
var last = this.items[key]; | ||
delete this.items[key]; | ||
return last; | ||
}; |
'use strict'; | ||
module.exports = function push(item) { | ||
this.items.push(item); | ||
module.exports = function push() { | ||
var _items; | ||
(_items = this.items).push.apply(_items, arguments); | ||
return this; | ||
}; |
'use strict'; | ||
module.exports = function random(length) { | ||
var randomLength = length || 1; | ||
var values = require('../helpers/values'); | ||
var randomCollection = this.slice().shuffle().take(randomLength); | ||
module.exports = function random() { | ||
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; | ||
if (randomLength === 1) { | ||
return randomCollection.first(); | ||
var items = values(this.items); | ||
var collection = new this.constructor(items).shuffle(); | ||
if (length === 1) { | ||
return collection.first(); | ||
} | ||
return randomCollection; | ||
return collection.take(length); | ||
}; |
'use strict'; | ||
module.exports = function search(valueOrFunction, strict) { | ||
var _this = this; | ||
var valueFn = valueOrFunction; | ||
@@ -12,12 +14,24 @@ | ||
var itemKey = this.items.filter(function (item) { | ||
if (strict === true) { | ||
return item === valueFn; | ||
} | ||
var index = false; | ||
return item === Number(valueFn) || item === valueFn.toString(); | ||
})[0]; | ||
if (Array.isArray(this.items)) { | ||
var itemKey = this.items.filter(function (item) { | ||
if (strict === true) { | ||
return item === valueFn; | ||
} | ||
var index = this.items.indexOf(itemKey); | ||
return item === Number(valueFn) || item === valueFn.toString(); | ||
})[0]; | ||
index = this.items.indexOf(itemKey); | ||
} else { | ||
return Object.keys(this.items).filter(function (prop) { | ||
if (strict === true) { | ||
return _this.items[prop] === valueFn; | ||
} | ||
return _this.items[prop] === Number(valueFn) || _this.items[prop] === valueFn.toString(); | ||
})[0] || false; | ||
} | ||
if (index === -1) { | ||
@@ -24,0 +38,0 @@ return false; |
'use strict'; | ||
module.exports = function shift() { | ||
return this.items.shift(); | ||
if (Array.isArray(this.items)) { | ||
return this.items.shift(); | ||
} | ||
var key = Object.keys(this.items)[0]; | ||
var value = this.items[key] || null; | ||
delete this.items[key]; | ||
return value; | ||
}; |
'use strict'; | ||
var values = require('../helpers/values'); | ||
module.exports = function shuffle() { | ||
var items = values(this.items); | ||
var j = void 0; | ||
@@ -8,10 +12,12 @@ var x = void 0; | ||
for (i = this.items.length; i; i -= 1) { | ||
for (i = items.length; i; i -= 1) { | ||
j = Math.floor(Math.random() * i); | ||
x = this.items[i - 1]; | ||
this.items[i - 1] = this.items[j]; | ||
this.items[j] = x; | ||
x = items[i - 1]; | ||
items[i - 1] = items[j]; | ||
items[j] = x; | ||
} | ||
this.items = items; | ||
return this; | ||
}; |
@@ -10,6 +10,6 @@ 'use strict'; | ||
for (var iterator = 0; iterator < numberOfGroups; iterator += 1) { | ||
collection.push(items.splice(0, itemsPerGroup)); | ||
collection.push(new this.constructor(items.splice(0, itemsPerGroup))); | ||
} | ||
return collection; | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
module.exports = function take(length) { | ||
var _this = this; | ||
if (!Array.isArray(this.items) && _typeof(this.items) === 'object') { | ||
var keys = Object.keys(this.items); | ||
var slicedKeys = void 0; | ||
if (length < 0) { | ||
slicedKeys = keys.slice(length); | ||
} else { | ||
slicedKeys = keys.slice(0, length); | ||
} | ||
var collection = {}; | ||
keys.forEach(function (prop) { | ||
if (slicedKeys.indexOf(prop) !== -1) { | ||
collection[prop] = _this.items[prop]; | ||
} | ||
}); | ||
return new this.constructor(collection); | ||
} | ||
if (length < 0) { | ||
@@ -5,0 +30,0 @@ return new this.constructor(this.items.slice(length)); |
'use strict'; | ||
module.exports = function toArray() { | ||
var collectionInstance = this.constructor; | ||
function iterate(list, collection) { | ||
var childCollection = []; | ||
if (list instanceof collectionInstance) { | ||
list.items.forEach(function (i) { | ||
return iterate(i, childCollection); | ||
}); | ||
collection.push(childCollection); | ||
} else if (Array.isArray(list)) { | ||
list.forEach(function (i) { | ||
return iterate(i, childCollection); | ||
}); | ||
collection.push(childCollection); | ||
} else { | ||
collection.push(list); | ||
} | ||
} | ||
if (Array.isArray(this.items)) { | ||
return this.all(); | ||
var collection = []; | ||
this.items.forEach(function (items) { | ||
iterate(items, collection); | ||
}); | ||
return collection; | ||
} | ||
@@ -7,0 +33,0 @@ |
'use strict'; | ||
module.exports = function toJson() { | ||
return JSON.stringify(this.items); | ||
return JSON.stringify(this.toArray()); | ||
}; |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
module.exports = function wrap(value) { | ||
@@ -8,3 +10,3 @@ if (value instanceof this.constructor) { | ||
if (Array.isArray(value)) { | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
return new this.constructor(value); | ||
@@ -11,0 +13,0 @@ } |
'use strict'; | ||
module.exports = function zip(array) { | ||
var _this = this; | ||
var values = array; | ||
if (values instanceof this.constructor) { | ||
values = values.all(); | ||
} | ||
var collection = this.items.map(function (item, index) { | ||
return [item, array[index]]; | ||
return new _this.constructor([item, values[index]]); | ||
}); | ||
@@ -7,0 +15,0 @@ |
The MIT License (MIT) | ||
Copyright (c) 2015 Daniel Eckermann | ||
Copyright (c) Daniel Eckermann | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "collect.js", | ||
"version": "3.1.2", | ||
"version": "4.0.0", | ||
"description": "Convenient and dependency free wrapper for working with arrays and objects.", | ||
@@ -10,4 +10,4 @@ "main": "dist/index.js", | ||
"test": "node_modules/.bin/mocha test/tests.js", | ||
"transpile": "node_modules/babel-cli/bin/babel.js src --out-dir dist", | ||
"eslint": "node_modules/.bin/eslint src/", | ||
"transpile": "node_modules/babel-cli/bin/babel.js src --quiet --out-dir dist", | ||
"eslint": "node_modules/.bin/eslint src/ test/", | ||
"coverage": "node_modules/.bin/nyc mocha test/tests.js", | ||
@@ -19,3 +19,3 @@ "reporter": "node_modules/.bin/nyc report --reporter=html", | ||
"presets": [ | ||
"es2015" | ||
"env" | ||
] | ||
@@ -57,11 +57,12 @@ }, | ||
"babel-core": "^6.25.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-env": "^1.6.1", | ||
"benchmark": "^2.1.0", | ||
"chai": "^3.5.0", | ||
"chai": "^4.1.2", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.2.0", | ||
"eslint-plugin-import": "^2.5.0", | ||
"mocha": "^2.4.5", | ||
"hoax.js": "^1.0.0", | ||
"mocha": "^3.5.2", | ||
"nyc": "^11.0.2" | ||
} | ||
} |
148
README.md
@@ -34,5 +34,7 @@ # <img src="https://raw.githubusercontent.com/ecrmnn/collect.js/master/collectjs.jpg" alt="collect.js"> | ||
- [diff](#diff) | ||
- [diffAssoc](#diffassoc) | ||
- [diffKeys](#diffkeys) | ||
- [dump](#dump) | ||
- [each](#each) | ||
- [eachSpread](#eachspread) | ||
- [every](#every) | ||
@@ -42,2 +44,3 @@ - [except](#except) | ||
- [first](#first) | ||
- [firstWhere](#firstwhere) | ||
- [flatMap](#flatmap) | ||
@@ -62,2 +65,4 @@ - [flatten](#flatten) | ||
- [mapInto](#mapinto) | ||
- [mapSpread](#mapspread) | ||
- [mapToDictionary](#maptodictionary) | ||
- [mapToGroups](#maptogroups) | ||
@@ -72,2 +77,3 @@ - [mapWithKeys](#mapwithkeys) | ||
- [only](#only) | ||
- [pad](#pad) | ||
- [partition](#partition) | ||
@@ -313,2 +319,24 @@ - [pipe](#pipe) | ||
#### ``diffAssoc()`` | ||
The diffAssoc method compares the collection against another collection or a plain 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: | ||
```js | ||
const collection = collect({ | ||
color: 'orange', | ||
type: 'fruit', | ||
remain: 6, | ||
}); | ||
const diff = collection.diffAssoc({ | ||
color: 'yellow', | ||
type: 'fruit', | ||
remain: 3, | ||
used: 6, | ||
}); | ||
diff.all(); | ||
//=> { color: 'orange', remain: 6 }; | ||
``` | ||
#### ``diffKeys()`` | ||
@@ -379,2 +407,19 @@ The diffKeys method compares the collection against another collection or a plain 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: | ||
#### ``eachSpread()`` | ||
The eachSpread method iterates over the collection's items, passing each nested item value into the given callback: | ||
```js | ||
const collection = collect([['John Doe', 35], ['Jane Doe', 33]]); | ||
collection.eachSpread((name, age) => { | ||
// | ||
}); | ||
``` | ||
You may stop iterating through the items by returning false from the callback: | ||
```js | ||
collection.eachSpread((name, age) => { | ||
return false; | ||
}); | ||
``` | ||
#### ``every()`` | ||
@@ -458,2 +503,17 @@ The every method may be used to verify that all elements of a collection pass a given truth test: | ||
#### ``firstWhere()`` | ||
The firstWhere method returns the first element in the collection with the given key / value pair: | ||
```js | ||
const collection = collect([ | ||
{name: 'Regena', age: 12}, | ||
{name: 'Linda', age: 14}, | ||
{name: 'Diego', age: 23}, | ||
{name: 'Linda', age: 84}, | ||
]); | ||
collection.firstWhere('name', 'Linda'); | ||
//=> { name: 'Linda', age: 14 } | ||
``` | ||
#### ``flatMap()`` | ||
@@ -463,12 +523,8 @@ The flatMap 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. Then, the array is flattened by a level: | ||
const collection = collect([ | ||
{ name: 'Robbie Fowler' }, | ||
{ nickname: 'The God' }, | ||
{ position: 'Striker' }, | ||
{ name: 'Robbie Fowler' }, | ||
{ nickname: 'The God' }, | ||
{ position: 'Striker' }, | ||
]); | ||
const flatMapped = collection.flatMap(function (values) { | ||
return values.map(function (value) { | ||
return value.toUpperCase(); | ||
}); | ||
}); | ||
const flatMapped = collection.flatMap(values => values.map(value => value.toUpperCase())); | ||
@@ -721,17 +777,3 @@ flatMapped.all(); | ||
``` | ||
An array of objects also works: | ||
```js | ||
const collection = collect([{ | ||
animal: 'unicorn', | ||
ability: 'magical' | ||
}, { | ||
anmial: 'pig', | ||
ability: 'filthy' | ||
}]); | ||
collection.has('ability'); | ||
//=> true | ||
``` | ||
#### ``implode()`` | ||
@@ -964,2 +1006,41 @@ The implode method joins the 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: | ||
#### ``mapSpread()`` | ||
The mapSpread method iterates over the collection's items, passing each nested item value into the given callback. | ||
The callback is free to modify the item and return it, thus forming a new collection of modified items: | ||
```js | ||
const collection = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
const chunks = collection.chunk(2); | ||
const sequence = chunks.mapSpread((odd, even) => { | ||
return odd + even; | ||
}); | ||
sequence.all(); | ||
//=> [1, 5, 9, 13, 17] | ||
``` | ||
#### ``mapToDictionary()`` | ||
Run a dictionary map over the items. | ||
The callback should return an associative array with a single key/value pair. | ||
```js | ||
const collection = collect([ | ||
{ id: 1, name: 'a' }, | ||
{ id: 2, name: 'b' }, | ||
{ id: 3, name: 'c' }, | ||
{ id: 4, name: 'b' }, | ||
]); | ||
const groups = collection.mapToDictionary(item => [item.name, item.id]); | ||
groups.all(); | ||
//=> { | ||
//=> a: [1], | ||
//=> b: [2, 4], | ||
//=> c: [3], | ||
//=> } | ||
``` | ||
#### ``mapToGroups()`` | ||
@@ -1163,2 +1244,23 @@ The mapToGroups method iterates through the collection and passes each value to the given callback: | ||
#### ``pad()`` | ||
The pad method will fill the array with the given value until the array reaches the specified size. This method | ||
behaves like the [array_pad](https://secure.php.net/manual/en/function.array-pad.php) PHP function. | ||
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: | ||
```js | ||
const collection = collect(['A', 'B', 'C']); | ||
let filtered = collection.pad(5, 0); | ||
filtered.all(); | ||
//=> ['A', 'B', 'C', 0, 0] | ||
filtered = collection.pad(-5, 0); | ||
filtered.all(); | ||
//=> [0, 0, 'A', 'B', 'C'] | ||
``` | ||
#### ``partition()`` | ||
@@ -1169,3 +1271,3 @@ The partition method may be combined with destructuring to separate elements that pass a given truth test from those that do not: | ||
const [underThree, underThree] = collection.partition(function (i) { | ||
const [underThree, overThree] = collection.partition(function (i) { | ||
return i < 3; | ||
@@ -1172,0 +1274,0 @@ }); |
@@ -29,2 +29,3 @@ 'use strict'; | ||
Collection.prototype.each = require('./methods/each'); | ||
Collection.prototype.eachSpread = require('./methods/eachSpread'); | ||
Collection.prototype.every = require('./methods/every'); | ||
@@ -34,2 +35,3 @@ Collection.prototype.except = require('./methods/except'); | ||
Collection.prototype.first = require('./methods/first'); | ||
Collection.prototype.firstWhere = require('./methods/firstWhere'); | ||
Collection.prototype.flatMap = require('./methods/flatMap'); | ||
@@ -53,2 +55,4 @@ Collection.prototype.flatten = require('./methods/flatten'); | ||
Collection.prototype.map = require('./methods/map'); | ||
Collection.prototype.mapSpread = require('./methods/mapSpread'); | ||
Collection.prototype.mapToDictionary = require('./methods/mapToDictionary'); | ||
Collection.prototype.mapInto = require('./methods/mapInto'); | ||
@@ -64,2 +68,3 @@ Collection.prototype.mapToGroups = require('./methods/mapToGroups'); | ||
Collection.prototype.only = require('./methods/only'); | ||
Collection.prototype.pad = require('./methods/pad'); | ||
Collection.prototype.partition = require('./methods/partition'); | ||
@@ -66,0 +71,0 @@ Collection.prototype.pipe = require('./methods/pipe'); |
@@ -7,8 +7,27 @@ 'use strict'; | ||
do { | ||
chunks.push(this.items.slice(index, index + size)); | ||
index += size; | ||
} while (index < this.items.length); | ||
if (Array.isArray(this.items)) { | ||
do { | ||
const items = this.items.slice(index, index + size); | ||
const collection = new this.constructor(items); | ||
chunks.push(collection); | ||
index += size; | ||
} while (index < this.items.length); | ||
} else if (typeof this.items === 'object') { | ||
const keys = Object.keys(this.items); | ||
do { | ||
const keysOfChunk = keys.slice(index, index + size); | ||
const collection = new this.constructor({}); | ||
keysOfChunk.forEach(key => collection.put(key, this.items[key])); | ||
chunks.push(collection); | ||
index += size; | ||
} while (index < keys.length); | ||
} else { | ||
chunks.push(new this.constructor([this.items])); | ||
} | ||
return new this.constructor(chunks); | ||
}; |
'use strict'; | ||
module.exports = function combine(array) { | ||
let values = array; | ||
if (values instanceof this.constructor) { | ||
values = array.all(); | ||
} | ||
const collection = {}; | ||
this.items.forEach((key, iterator) => { | ||
collection[key] = array[iterator]; | ||
}); | ||
if (Array.isArray(this.items) && Array.isArray(values)) { | ||
this.items.forEach((key, iterator) => { | ||
collection[key] = values[iterator]; | ||
}); | ||
} else if (typeof this.items === 'object' && typeof values === 'object') { | ||
Object.keys(this.items).forEach((key, index) => { | ||
collection[this.items[key]] = values[Object.keys(values)[index]]; | ||
}); | ||
} else if (Array.isArray(this.items)) { | ||
collection[this.items[0]] = values; | ||
} else if (typeof this.items === 'string' && Array.isArray(values)) { | ||
collection[this.items] = values[0]; | ||
} else if (typeof this.items === 'string') { | ||
collection[this.items] = values; | ||
} | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function count() { | ||
return this.items.length; | ||
if (Array.isArray(this.items)) { | ||
return this.items.length; | ||
} | ||
return Object.keys(this.items).length; | ||
}; |
'use strict'; | ||
module.exports = function dump() { | ||
console.log(this.items); | ||
console.log(this); | ||
return this; | ||
}; |
'use strict'; | ||
const values = require('../helpers/values'); | ||
module.exports = function every(fn) { | ||
return this.items | ||
const items = values(this.items); | ||
return items | ||
.map((item, index) => fn(item, index)) | ||
.indexOf(false) === -1; | ||
}; |
'use strict'; | ||
module.exports = function except(properties) { | ||
const variadic = require('../helpers/variadic'); | ||
module.exports = function except(...args) { | ||
const properties = variadic(args); | ||
if (Array.isArray(this.items)) { | ||
const collection = this.items.filter(item => properties.indexOf(item)); | ||
const collection = this.items | ||
.filter(item => properties.indexOf(item) === -1); | ||
@@ -7,0 +12,0 @@ return new this.constructor(collection); |
@@ -13,3 +13,7 @@ 'use strict'; | ||
return this.items[0]; | ||
if (Array.isArray(this.items)) { | ||
return this.items[0]; | ||
} | ||
return this.items[Object.keys(this.items)[0]]; | ||
}; |
@@ -6,7 +6,13 @@ 'use strict'; | ||
Object.keys(this.items).forEach((key) => { | ||
collection[this.items[key]] = key; | ||
}); | ||
if (Array.isArray(this.items)) { | ||
Object.keys(this.items).forEach((key) => { | ||
collection[this.items[key]] = Number(key); | ||
}); | ||
} else { | ||
Object.keys(this.items).forEach((key) => { | ||
collection[this.items[key]] = key; | ||
}); | ||
} | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function forget(key) { | ||
delete this.items[key]; | ||
if (Array.isArray(this.items)) { | ||
this.items.splice(key, 1); | ||
} else { | ||
delete this.items[key]; | ||
} | ||
return this; | ||
}; |
'use strict'; | ||
module.exports = function forPage(page, chunk) { | ||
const collection = this.items.slice((page * chunk) - chunk, page * chunk); | ||
let collection = {}; | ||
if (Array.isArray(this.items)) { | ||
collection = this.items.slice((page * chunk) - chunk, page * chunk); | ||
} else { | ||
Object | ||
.keys(this.items) | ||
.slice((page * chunk) - chunk, page * chunk) | ||
.forEach((key) => { | ||
collection[key] = this.items[key]; | ||
}); | ||
} | ||
return new this.constructor(collection); | ||
}; |
@@ -12,7 +12,7 @@ 'use strict'; | ||
} else { | ||
resolvedKey = item[key]; | ||
resolvedKey = item[key] || ''; | ||
} | ||
if (collection[resolvedKey] === undefined) { | ||
collection[resolvedKey] = []; | ||
collection[resolvedKey] = new this.constructor([]); | ||
} | ||
@@ -19,0 +19,0 @@ |
'use strict'; | ||
module.exports = function has(key) { | ||
if (Array.isArray(this.items)) { | ||
for (let i = 0, length = this.items.length; i < length; i += 1) { | ||
if (this.items[i][key] !== undefined) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
const variadic = require('../helpers/variadic'); | ||
return this.items[key] !== undefined; | ||
module.exports = function has(...args) { | ||
const properties = variadic(args); | ||
return properties.filter(key => this.items[key]).length === properties.length; | ||
}; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
this.items.forEach((item) => { | ||
collection[item[key]] = item; | ||
collection[item[key] || ''] = item; | ||
}); | ||
@@ -15,0 +15,0 @@ } |
'use strict'; | ||
module.exports = function keys() { | ||
let collection = Object.keys(this.items); | ||
if (Array.isArray(this.items)) { | ||
const collection = []; | ||
this.items.forEach((object) => { | ||
Object.keys(object).forEach((key) => { | ||
collection.push(key); | ||
}); | ||
}); | ||
return new this.constructor(collection).unique(); | ||
collection = collection.map(Number); | ||
} | ||
return new this.constructor(Object.keys(this.items)); | ||
return new this.constructor(collection); | ||
}; |
@@ -10,3 +10,8 @@ 'use strict'; | ||
return this.items[this.items.length - 1]; | ||
if (Array.isArray(this.items)) { | ||
return this.items[this.items.length - 1]; | ||
} | ||
const keys = Object.keys(this.items); | ||
return this.items[keys[keys.length - 1]]; | ||
}; |
@@ -6,4 +6,4 @@ 'use strict'; | ||
this.items.forEach((item) => { | ||
const [keyed, value] = fn(item); | ||
this.items.forEach((item, key) => { | ||
const [keyed, value] = fn(item, key); | ||
@@ -10,0 +10,0 @@ if (collection[keyed] === undefined) { |
'use strict'; | ||
module.exports = function merge(objectOrArray) { | ||
if (Array.isArray(objectOrArray)) { | ||
return new this.constructor(this.items.concat(objectOrArray)); | ||
module.exports = function merge(value) { | ||
let arrayOrObject = value; | ||
if (typeof arrayOrObject === 'string') { | ||
arrayOrObject = [arrayOrObject]; | ||
} | ||
if (Array.isArray(this.items) && Array.isArray(arrayOrObject)) { | ||
return new this.constructor(this.items.concat(arrayOrObject)); | ||
} | ||
const collection = JSON.parse(JSON.stringify(this.items)); | ||
Object.keys(objectOrArray).forEach((key) => { | ||
collection[key] = objectOrArray[key]; | ||
Object.keys(arrayOrObject).forEach((key) => { | ||
collection[key] = arrayOrObject[key]; | ||
}); | ||
@@ -13,0 +19,0 @@ |
'use strict'; | ||
module.exports = function nth(n, offset) { | ||
const ntnOffset = offset || 0; | ||
const values = require('../helpers/values'); | ||
const collection = this.items | ||
.slice(ntnOffset) | ||
module.exports = function nth(n, offset = 0) { | ||
const items = values(this.items); | ||
const collection = items | ||
.slice(offset) | ||
.filter((item, index) => index % n === 0); | ||
@@ -9,0 +11,0 @@ |
'use strict'; | ||
module.exports = function only(properties) { | ||
const variadic = require('../helpers/variadic'); | ||
module.exports = function only(...args) { | ||
const properties = variadic(args); | ||
if (Array.isArray(this.items)) { | ||
const collection = this.items.filter(item => !properties.indexOf(item)); | ||
const collection = this.items | ||
.filter(item => properties.indexOf(item) !== -1); | ||
@@ -7,0 +12,0 @@ return new this.constructor(collection); |
'use strict'; | ||
module.exports = function partition(fn) { | ||
const arrays = [[], []]; | ||
let arrays; | ||
this.items.forEach((item) => { | ||
if (fn(item) === true) { | ||
arrays[0].push(item); | ||
} else { | ||
arrays[1].push(item); | ||
} | ||
}); | ||
if (Array.isArray(this.items)) { | ||
arrays = [new this.constructor([]), new this.constructor([])]; | ||
return arrays; | ||
this.items.forEach((item) => { | ||
if (fn(item) === true) { | ||
arrays[0].push(item); | ||
} else { | ||
arrays[1].push(item); | ||
} | ||
}); | ||
} else { | ||
arrays = [new this.constructor({}), new this.constructor({})]; | ||
Object.keys(this.items).forEach((prop) => { | ||
const value = this.items[prop]; | ||
if (fn(value) === true) { | ||
arrays[0].put(prop, value); | ||
} else { | ||
arrays[1].put(prop, value); | ||
} | ||
}); | ||
} | ||
return new this.constructor(arrays); | ||
}; |
@@ -8,3 +8,3 @@ 'use strict'; | ||
this.items.forEach((item) => { | ||
collection[item[key]] = item[value]; | ||
collection[item[key] || ''] = item[value] || null; | ||
}); | ||
@@ -15,7 +15,3 @@ | ||
const collection = this.items | ||
.filter(item => item[value] !== undefined) | ||
.map(item => item[value]); | ||
return new this.constructor(collection); | ||
return this.map(item => item[value] || null); | ||
}; |
'use strict'; | ||
module.exports = function pop() { | ||
return this.items.pop(); | ||
if (Array.isArray(this.items)) { | ||
return this.items.pop(); | ||
} | ||
const keys = Object.keys(this.items); | ||
const key = keys[keys.length - 1]; | ||
const last = this.items[key]; | ||
delete this.items[key]; | ||
return last; | ||
}; |
'use strict'; | ||
module.exports = function push(item) { | ||
this.items.push(item); | ||
module.exports = function push(...items) { | ||
this.items.push(...items); | ||
return this; | ||
}; |
'use strict'; | ||
module.exports = function random(length) { | ||
const randomLength = length || 1; | ||
const values = require('../helpers/values'); | ||
const randomCollection = this.slice().shuffle().take(randomLength); | ||
module.exports = function random(length = 1) { | ||
const items = values(this.items); | ||
if (randomLength === 1) { | ||
return randomCollection.first(); | ||
const collection = new this.constructor(items).shuffle(); | ||
if (length === 1) { | ||
return collection.first(); | ||
} | ||
return randomCollection; | ||
return collection.take(length); | ||
}; |
@@ -10,12 +10,24 @@ 'use strict'; | ||
const itemKey = this.items.filter((item) => { | ||
if (strict === true) { | ||
return item === valueFn; | ||
} | ||
let index = false; | ||
return item === Number(valueFn) || item === valueFn.toString(); | ||
})[0]; | ||
if (Array.isArray(this.items)) { | ||
const itemKey = this.items.filter((item) => { | ||
if (strict === true) { | ||
return item === valueFn; | ||
} | ||
const index = this.items.indexOf(itemKey); | ||
return item === Number(valueFn) || item === valueFn.toString(); | ||
})[0]; | ||
index = this.items.indexOf(itemKey); | ||
} else { | ||
return Object.keys(this.items).filter((prop) => { | ||
if (strict === true) { | ||
return this.items[prop] === valueFn; | ||
} | ||
return this.items[prop] === Number(valueFn) || this.items[prop] === valueFn.toString(); | ||
})[0] || false; | ||
} | ||
if (index === -1) { | ||
@@ -22,0 +34,0 @@ return false; |
'use strict'; | ||
module.exports = function shift() { | ||
return this.items.shift(); | ||
if (Array.isArray(this.items)) { | ||
return this.items.shift(); | ||
} | ||
const key = Object.keys(this.items)[0]; | ||
const value = this.items[key] || null; | ||
delete this.items[key]; | ||
return value; | ||
}; |
'use strict'; | ||
const values = require('../helpers/values'); | ||
module.exports = function shuffle() { | ||
const items = values(this.items); | ||
let j; | ||
@@ -8,10 +12,12 @@ let x; | ||
for (i = this.items.length; i; i -= 1) { | ||
for (i = items.length; i; i -= 1) { | ||
j = Math.floor(Math.random() * i); | ||
x = this.items[i - 1]; | ||
this.items[i - 1] = this.items[j]; | ||
this.items[j] = x; | ||
x = items[i - 1]; | ||
items[i - 1] = items[j]; | ||
items[j] = x; | ||
} | ||
this.items = items; | ||
return this; | ||
}; |
@@ -10,6 +10,6 @@ 'use strict'; | ||
for (let iterator = 0; iterator < numberOfGroups; iterator += 1) { | ||
collection.push(items.splice(0, itemsPerGroup)); | ||
collection.push(new this.constructor(items.splice(0, itemsPerGroup))); | ||
} | ||
return collection; | ||
return new this.constructor(collection); | ||
}; |
'use strict'; | ||
module.exports = function take(length) { | ||
if (!Array.isArray(this.items) && typeof this.items === 'object') { | ||
const keys = Object.keys(this.items); | ||
let slicedKeys; | ||
if (length < 0) { | ||
slicedKeys = keys.slice(length); | ||
} else { | ||
slicedKeys = keys.slice(0, length); | ||
} | ||
const collection = {}; | ||
keys.forEach((prop) => { | ||
if (slicedKeys.indexOf(prop) !== -1) { | ||
collection[prop] = this.items[prop]; | ||
} | ||
}); | ||
return new this.constructor(collection); | ||
} | ||
if (length < 0) { | ||
@@ -5,0 +26,0 @@ return new this.constructor(this.items.slice(length)); |
'use strict'; | ||
module.exports = function toArray() { | ||
const collectionInstance = this.constructor; | ||
function iterate(list, collection) { | ||
const childCollection = []; | ||
if (list instanceof collectionInstance) { | ||
list.items.forEach(i => iterate(i, childCollection)); | ||
collection.push(childCollection); | ||
} else if (Array.isArray(list)) { | ||
list.forEach(i => iterate(i, childCollection)); | ||
collection.push(childCollection); | ||
} else { | ||
collection.push(list); | ||
} | ||
} | ||
if (Array.isArray(this.items)) { | ||
return this.all(); | ||
const collection = []; | ||
this.items.forEach((items) => { | ||
iterate(items, collection); | ||
}); | ||
return collection; | ||
} | ||
@@ -7,0 +29,0 @@ |
'use strict'; | ||
module.exports = function toJson() { | ||
return JSON.stringify(this.items); | ||
return JSON.stringify(this.toArray()); | ||
}; |
@@ -8,3 +8,3 @@ 'use strict'; | ||
if (Array.isArray(value)) { | ||
if (typeof value === 'object') { | ||
return new this.constructor(value); | ||
@@ -11,0 +11,0 @@ } |
'use strict'; | ||
module.exports = function zip(array) { | ||
const collection = this.items.map((item, index) => [item, array[index]]); | ||
let values = array; | ||
if (values instanceof this.constructor) { | ||
values = values.all(); | ||
} | ||
const collection = this.items.map((item, index) => new this.constructor([item, values[index]])); | ||
return new this.constructor(collection); | ||
}; |
Sorry, the diff of this file is not supported yet
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
270412
202
3083
2069
11