ui
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Clone helper
|
||||
*
|
||||
* Clone an array or object
|
||||
*
|
||||
* @param items
|
||||
* @returns {*}
|
||||
*/
|
||||
module.exports = function clone(items) {
|
||||
let cloned;
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
cloned = [];
|
||||
|
||||
cloned.push(...items);
|
||||
} else {
|
||||
cloned = {};
|
||||
|
||||
Object.keys(items).forEach((prop) => {
|
||||
cloned[prop] = items[prop];
|
||||
});
|
||||
}
|
||||
|
||||
return cloned;
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const variadic = require('./variadic');
|
||||
|
||||
/**
|
||||
* Delete keys helper
|
||||
*
|
||||
* Delete one or multiple keys from an object
|
||||
*
|
||||
* @param obj
|
||||
* @param keys
|
||||
* @returns {void}
|
||||
*/
|
||||
module.exports = function deleteKeys(obj, ...keys) {
|
||||
variadic(keys).forEach((key) => {
|
||||
// eslint-disable-next-line
|
||||
delete obj[key];
|
||||
});
|
||||
};
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isArray: item => Array.isArray(item),
|
||||
|
||||
/**
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isObject: item => typeof item === 'object' && Array.isArray(item) === false && item !== null,
|
||||
|
||||
/**
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isFunction: item => typeof item === 'function',
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Get value of a nested property
|
||||
*
|
||||
* @param mainObject
|
||||
* @param key
|
||||
* @returns {*}
|
||||
*/
|
||||
module.exports = function nestedValue(mainObject, key) {
|
||||
try {
|
||||
return key.split('.').reduce((obj, property) => obj[property], mainObject);
|
||||
} catch (err) {
|
||||
// If we end up here, we're not working with an object, and @var mainObject is the value itself
|
||||
return mainObject;
|
||||
}
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Values helper
|
||||
*
|
||||
* Retrieve values from [this.items] when it is an array, object or Collection
|
||||
*
|
||||
* @param items
|
||||
* @returns {*}
|
||||
*/
|
||||
module.exports = function values(items) {
|
||||
const valuesArray = [];
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
valuesArray.push(...items);
|
||||
} else if (items.constructor.name === 'Collection') {
|
||||
valuesArray.push(...items.all());
|
||||
} else {
|
||||
Object.keys(items).forEach(prop => valuesArray.push(items[prop]));
|
||||
}
|
||||
|
||||
return valuesArray;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Variadic helper function
|
||||
*
|
||||
* @param args
|
||||
* @returns {Array}
|
||||
*/
|
||||
module.exports = function variadic(args) {
|
||||
if (Array.isArray(args[0])) {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
return args;
|
||||
};
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
'use strict';
|
||||
|
||||
function Collection(collection) {
|
||||
if (collection !== undefined && !Array.isArray(collection) && typeof collection !== 'object') {
|
||||
this.items = [collection];
|
||||
} else if (collection instanceof this.constructor) {
|
||||
this.items = collection.all();
|
||||
} else {
|
||||
this.items = collection || [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Symbol.iterator
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
|
||||
*/
|
||||
const SymbolIterator = require('./methods/symbol.iterator');
|
||||
|
||||
if (typeof Symbol !== 'undefined') {
|
||||
Collection.prototype[Symbol.iterator] = SymbolIterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Support JSON.stringify
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
|
||||
*/
|
||||
Collection.prototype.toJSON = function toJSON() {
|
||||
return this.items;
|
||||
};
|
||||
|
||||
Collection.prototype.all = require('./methods/all');
|
||||
Collection.prototype.average = require('./methods/average');
|
||||
Collection.prototype.avg = require('./methods/avg');
|
||||
Collection.prototype.chunk = require('./methods/chunk');
|
||||
Collection.prototype.collapse = require('./methods/collapse');
|
||||
Collection.prototype.combine = require('./methods/combine');
|
||||
Collection.prototype.concat = require('./methods/concat');
|
||||
Collection.prototype.contains = require('./methods/contains');
|
||||
Collection.prototype.count = require('./methods/count');
|
||||
Collection.prototype.countBy = require('./methods/countBy');
|
||||
Collection.prototype.crossJoin = require('./methods/crossJoin');
|
||||
Collection.prototype.dd = require('./methods/dd');
|
||||
Collection.prototype.diff = require('./methods/diff');
|
||||
Collection.prototype.diffAssoc = require('./methods/diffAssoc');
|
||||
Collection.prototype.diffKeys = require('./methods/diffKeys');
|
||||
Collection.prototype.doesntContain = require('./methods/doesntContain');
|
||||
Collection.prototype.dump = require('./methods/dump');
|
||||
Collection.prototype.duplicates = require('./methods/duplicates');
|
||||
Collection.prototype.each = require('./methods/each');
|
||||
Collection.prototype.eachSpread = require('./methods/eachSpread');
|
||||
Collection.prototype.every = require('./methods/every');
|
||||
Collection.prototype.except = require('./methods/except');
|
||||
Collection.prototype.filter = require('./methods/filter');
|
||||
Collection.prototype.first = require('./methods/first');
|
||||
Collection.prototype.firstOrFail = require('./methods/firstOrFail');
|
||||
Collection.prototype.firstWhere = require('./methods/firstWhere');
|
||||
Collection.prototype.flatMap = require('./methods/flatMap');
|
||||
Collection.prototype.flatten = require('./methods/flatten');
|
||||
Collection.prototype.flip = require('./methods/flip');
|
||||
Collection.prototype.forPage = require('./methods/forPage');
|
||||
Collection.prototype.forget = require('./methods/forget');
|
||||
Collection.prototype.get = require('./methods/get');
|
||||
Collection.prototype.groupBy = require('./methods/groupBy');
|
||||
Collection.prototype.has = require('./methods/has');
|
||||
Collection.prototype.implode = require('./methods/implode');
|
||||
Collection.prototype.intersect = require('./methods/intersect');
|
||||
Collection.prototype.intersectByKeys = require('./methods/intersectByKeys');
|
||||
Collection.prototype.isEmpty = require('./methods/isEmpty');
|
||||
Collection.prototype.isNotEmpty = require('./methods/isNotEmpty');
|
||||
Collection.prototype.join = require('./methods/join');
|
||||
Collection.prototype.keyBy = require('./methods/keyBy');
|
||||
Collection.prototype.keys = require('./methods/keys');
|
||||
Collection.prototype.last = require('./methods/last');
|
||||
Collection.prototype.macro = require('./methods/macro');
|
||||
Collection.prototype.make = require('./methods/make');
|
||||
Collection.prototype.map = require('./methods/map');
|
||||
Collection.prototype.mapSpread = require('./methods/mapSpread');
|
||||
Collection.prototype.mapToDictionary = require('./methods/mapToDictionary');
|
||||
Collection.prototype.mapInto = require('./methods/mapInto');
|
||||
Collection.prototype.mapToGroups = require('./methods/mapToGroups');
|
||||
Collection.prototype.mapWithKeys = require('./methods/mapWithKeys');
|
||||
Collection.prototype.max = require('./methods/max');
|
||||
Collection.prototype.median = require('./methods/median');
|
||||
Collection.prototype.merge = require('./methods/merge');
|
||||
Collection.prototype.mergeRecursive = require('./methods/mergeRecursive');
|
||||
Collection.prototype.min = require('./methods/min');
|
||||
Collection.prototype.mode = require('./methods/mode');
|
||||
Collection.prototype.nth = require('./methods/nth');
|
||||
Collection.prototype.only = require('./methods/only');
|
||||
Collection.prototype.pad = require('./methods/pad');
|
||||
Collection.prototype.partition = require('./methods/partition');
|
||||
Collection.prototype.pipe = require('./methods/pipe');
|
||||
Collection.prototype.pluck = require('./methods/pluck');
|
||||
Collection.prototype.pop = require('./methods/pop');
|
||||
Collection.prototype.prepend = require('./methods/prepend');
|
||||
Collection.prototype.pull = require('./methods/pull');
|
||||
Collection.prototype.push = require('./methods/push');
|
||||
Collection.prototype.put = require('./methods/put');
|
||||
Collection.prototype.random = require('./methods/random');
|
||||
Collection.prototype.reduce = require('./methods/reduce');
|
||||
Collection.prototype.reject = require('./methods/reject');
|
||||
Collection.prototype.replace = require('./methods/replace');
|
||||
Collection.prototype.replaceRecursive = require('./methods/replaceRecursive');
|
||||
Collection.prototype.reverse = require('./methods/reverse');
|
||||
Collection.prototype.search = require('./methods/search');
|
||||
Collection.prototype.shift = require('./methods/shift');
|
||||
Collection.prototype.shuffle = require('./methods/shuffle');
|
||||
Collection.prototype.skip = require('./methods/skip');
|
||||
Collection.prototype.skipUntil = require('./methods/skipUntil');
|
||||
Collection.prototype.skipWhile = require('./methods/skipWhile');
|
||||
Collection.prototype.slice = require('./methods/slice');
|
||||
Collection.prototype.sole = require('./methods/sole');
|
||||
Collection.prototype.some = require('./methods/some');
|
||||
Collection.prototype.sort = require('./methods/sort');
|
||||
Collection.prototype.sortDesc = require('./methods/sortDesc');
|
||||
Collection.prototype.sortBy = require('./methods/sortBy');
|
||||
Collection.prototype.sortByDesc = require('./methods/sortByDesc');
|
||||
Collection.prototype.sortKeys = require('./methods/sortKeys');
|
||||
Collection.prototype.sortKeysDesc = require('./methods/sortKeysDesc');
|
||||
Collection.prototype.splice = require('./methods/splice');
|
||||
Collection.prototype.split = require('./methods/split');
|
||||
Collection.prototype.sum = require('./methods/sum');
|
||||
Collection.prototype.take = require('./methods/take');
|
||||
Collection.prototype.takeUntil = require('./methods/takeUntil');
|
||||
Collection.prototype.takeWhile = require('./methods/takeWhile');
|
||||
Collection.prototype.tap = require('./methods/tap');
|
||||
Collection.prototype.times = require('./methods/times');
|
||||
Collection.prototype.toArray = require('./methods/toArray');
|
||||
Collection.prototype.toJson = require('./methods/toJson');
|
||||
Collection.prototype.transform = require('./methods/transform');
|
||||
Collection.prototype.unless = require('./methods/unless');
|
||||
Collection.prototype.unlessEmpty = require('./methods/whenNotEmpty');
|
||||
Collection.prototype.unlessNotEmpty = require('./methods/whenEmpty');
|
||||
Collection.prototype.union = require('./methods/union');
|
||||
Collection.prototype.unique = require('./methods/unique');
|
||||
Collection.prototype.unwrap = require('./methods/unwrap');
|
||||
Collection.prototype.values = require('./methods/values');
|
||||
Collection.prototype.when = require('./methods/when');
|
||||
Collection.prototype.whenEmpty = require('./methods/whenEmpty');
|
||||
Collection.prototype.whenNotEmpty = require('./methods/whenNotEmpty');
|
||||
Collection.prototype.where = require('./methods/where');
|
||||
Collection.prototype.whereBetween = require('./methods/whereBetween');
|
||||
Collection.prototype.whereIn = require('./methods/whereIn');
|
||||
Collection.prototype.whereInstanceOf = require('./methods/whereInstanceOf');
|
||||
Collection.prototype.whereNotBetween = require('./methods/whereNotBetween');
|
||||
Collection.prototype.whereNotIn = require('./methods/whereNotIn');
|
||||
Collection.prototype.whereNull = require('./methods/whereNull');
|
||||
Collection.prototype.whereNotNull = require('./methods/whereNotNull');
|
||||
Collection.prototype.wrap = require('./methods/wrap');
|
||||
Collection.prototype.zip = require('./methods/zip');
|
||||
|
||||
const collect = collection => new Collection(collection);
|
||||
|
||||
module.exports = collect;
|
||||
module.exports.collect = collect;
|
||||
module.exports.default = collect;
|
||||
module.exports.Collection = Collection;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function all() {
|
||||
return this.items;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function average(key) {
|
||||
if (key === undefined) {
|
||||
return this.sum() / this.items.length;
|
||||
}
|
||||
|
||||
if (isFunction(key)) {
|
||||
return new this.constructor(this.items).sum(key) / this.items.length;
|
||||
}
|
||||
|
||||
return new this.constructor(this.items).pluck(key).sum() / this.items.length;
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const average = require('./average');
|
||||
|
||||
module.exports = average;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function chunk(size) {
|
||||
const chunks = [];
|
||||
let index = 0;
|
||||
|
||||
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);
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function collapse() {
|
||||
return new this.constructor([].concat(...this.items));
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function combine(array) {
|
||||
let values = array;
|
||||
|
||||
if (values instanceof this.constructor) {
|
||||
values = array.all();
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
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;
|
||||
} else if (typeof this.items === 'string') {
|
||||
collection[this.items] = values;
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const clone = require('../helpers/clone');
|
||||
|
||||
module.exports = function concat(collectionOrArrayOrObject) {
|
||||
let list = collectionOrArrayOrObject;
|
||||
|
||||
if (collectionOrArrayOrObject instanceof this.constructor) {
|
||||
list = collectionOrArrayOrObject.all();
|
||||
} else if (typeof collectionOrArrayOrObject === 'object') {
|
||||
list = [];
|
||||
Object.keys(collectionOrArrayOrObject).forEach((property) => {
|
||||
list.push(collectionOrArrayOrObject[property]);
|
||||
});
|
||||
}
|
||||
|
||||
const collection = clone(this.items);
|
||||
|
||||
list.forEach((item) => {
|
||||
if (typeof item === 'object') {
|
||||
Object.keys(item).forEach(key => collection.push(item[key]));
|
||||
} else {
|
||||
collection.push(item);
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function contains(key, value) {
|
||||
if (value !== undefined) {
|
||||
if (Array.isArray(this.items)) {
|
||||
return this.items
|
||||
.filter(items => items[key] !== undefined && items[key] === value)
|
||||
.length > 0;
|
||||
}
|
||||
|
||||
return this.items[key] !== undefined && this.items[key] === value;
|
||||
}
|
||||
|
||||
if (isFunction(key)) {
|
||||
return (this.items.filter((item, index) => key(item, index)).length > 0);
|
||||
}
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
return this.items.indexOf(key) !== -1;
|
||||
}
|
||||
|
||||
const keysAndValues = values(this.items);
|
||||
keysAndValues.push(...Object.keys(this.items));
|
||||
|
||||
return keysAndValues.indexOf(key) !== -1;
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function count() {
|
||||
let arrayLength = 0;
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
arrayLength = this.items.length;
|
||||
}
|
||||
|
||||
return Math.max(Object.keys(this.items).length, arrayLength);
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function countBy(fn = value => value) {
|
||||
return new this.constructor(this.items)
|
||||
.groupBy(fn)
|
||||
.map(value => value.count());
|
||||
};
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function crossJoin(...values) {
|
||||
function join(collection, constructor, args) {
|
||||
let current = args[0];
|
||||
|
||||
if (current instanceof constructor) {
|
||||
current = current.all();
|
||||
}
|
||||
|
||||
const rest = args.slice(1);
|
||||
const last = !rest.length;
|
||||
let result = [];
|
||||
|
||||
for (let i = 0; i < current.length; i += 1) {
|
||||
const collectionCopy = collection.slice();
|
||||
collectionCopy.push(current[i]);
|
||||
|
||||
if (last) {
|
||||
result.push(collectionCopy);
|
||||
} else {
|
||||
result = result.concat(join(collectionCopy, constructor, rest));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return new this.constructor(join([], this.constructor, [].concat([this.items], values)));
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function dd() {
|
||||
this.dump();
|
||||
|
||||
if (typeof process !== 'undefined') {
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function diff(values) {
|
||||
let valuesToDiff;
|
||||
|
||||
if (values instanceof this.constructor) {
|
||||
valuesToDiff = values.all();
|
||||
} else {
|
||||
valuesToDiff = values;
|
||||
}
|
||||
|
||||
const collection = this.items.filter(item => valuesToDiff.indexOf(item) === -1);
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function diffAssoc(values) {
|
||||
let diffValues = values;
|
||||
|
||||
if (values instanceof this.constructor) {
|
||||
diffValues = values.all();
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
if (diffValues[key] === undefined || diffValues[key] !== this.items[key]) {
|
||||
collection[key] = this.items[key];
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function diffKeys(object) {
|
||||
let objectToDiff;
|
||||
|
||||
if (object instanceof this.constructor) {
|
||||
objectToDiff = object.all();
|
||||
} else {
|
||||
objectToDiff = object;
|
||||
}
|
||||
|
||||
const objectKeys = Object.keys(objectToDiff);
|
||||
|
||||
const remainingKeys = Object.keys(this.items)
|
||||
.filter(item => objectKeys.indexOf(item) === -1);
|
||||
|
||||
return new this.constructor(this.items).only(
|
||||
remainingKeys,
|
||||
);
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function contains(key, value) {
|
||||
return !this.contains(key, value);
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function dump() {
|
||||
// eslint-disable-next-line
|
||||
console.log(this);
|
||||
|
||||
return this;
|
||||
};
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function duplicates() {
|
||||
const occuredValues = [];
|
||||
const duplicateValues = {};
|
||||
|
||||
const stringifiedValue = (value) => {
|
||||
if (Array.isArray(value) || typeof value === 'object') {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
this.items.forEach((value, index) => {
|
||||
const valueAsString = stringifiedValue(value);
|
||||
|
||||
if (occuredValues.indexOf(valueAsString) === -1) {
|
||||
occuredValues.push(valueAsString);
|
||||
} else {
|
||||
duplicateValues[index] = value;
|
||||
}
|
||||
});
|
||||
} else if (typeof this.items === 'object') {
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
const valueAsString = stringifiedValue(this.items[key]);
|
||||
|
||||
if (occuredValues.indexOf(valueAsString) === -1) {
|
||||
occuredValues.push(valueAsString);
|
||||
} else {
|
||||
duplicateValues[key] = this.items[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new this.constructor(duplicateValues);
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function each(fn) {
|
||||
let stop = false;
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
const { length } = this.items;
|
||||
|
||||
for (let index = 0; index < length && !stop; index += 1) {
|
||||
stop = fn(this.items[index], index, this.items) === false;
|
||||
}
|
||||
} else {
|
||||
const keys = Object.keys(this.items);
|
||||
const { length } = keys;
|
||||
|
||||
for (let index = 0; index < length && !stop; index += 1) {
|
||||
const key = keys[index];
|
||||
|
||||
stop = fn(this.items[key], key, this.items) === false;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function eachSpread(fn) {
|
||||
this.each((values, key) => {
|
||||
fn(...values, key);
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
|
||||
module.exports = function every(fn) {
|
||||
const items = values(this.items);
|
||||
|
||||
return items.every(fn);
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
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) === -1);
|
||||
|
||||
return new this.constructor(collection);
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
Object.keys(this.items).forEach((property) => {
|
||||
if (properties.indexOf(property) === -1) {
|
||||
collection[property] = this.items[property];
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
function falsyValue(item) {
|
||||
if (Array.isArray(item)) {
|
||||
if (item.length) {
|
||||
return false;
|
||||
}
|
||||
} else if (item !== undefined && item !== null
|
||||
&& typeof item === 'object') {
|
||||
if (Object.keys(item).length) {
|
||||
return false;
|
||||
}
|
||||
} else if (item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function filterObject(func, items) {
|
||||
const result = {};
|
||||
Object.keys(items).forEach((key) => {
|
||||
if (func) {
|
||||
if (func(items[key], key)) {
|
||||
result[key] = items[key];
|
||||
}
|
||||
} else if (!falsyValue(items[key])) {
|
||||
result[key] = items[key];
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function filterArray(func, items) {
|
||||
if (func) {
|
||||
return items.filter(func);
|
||||
}
|
||||
const result = [];
|
||||
for (let i = 0; i < items.length; i += 1) {
|
||||
const item = items[i];
|
||||
if (!falsyValue(item)) {
|
||||
result.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = function filter(fn) {
|
||||
const func = fn || false;
|
||||
let filteredItems = null;
|
||||
if (Array.isArray(this.items)) {
|
||||
filteredItems = filterArray(func, this.items);
|
||||
} else {
|
||||
filteredItems = filterObject(func, this.items);
|
||||
}
|
||||
|
||||
return new this.constructor(filteredItems);
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function first(fn, defaultValue) {
|
||||
if (isFunction(fn)) {
|
||||
const keys = Object.keys(this.items);
|
||||
|
||||
for (let i = 0; i < keys.length; i += 1) {
|
||||
const key = keys[i];
|
||||
const item = this.items[key];
|
||||
|
||||
if (fn(item, key)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
if (isFunction(defaultValue)) {
|
||||
return defaultValue();
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if ((Array.isArray(this.items) && this.items.length) || (Object.keys(this.items).length)) {
|
||||
if (Array.isArray(this.items)) {
|
||||
return this.items[0];
|
||||
}
|
||||
|
||||
const firstKey = Object.keys(this.items)[0];
|
||||
|
||||
return this.items[firstKey];
|
||||
}
|
||||
|
||||
if (isFunction(defaultValue)) {
|
||||
return defaultValue();
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function firstOrFail(key, operator, value) {
|
||||
if (isFunction(key)) {
|
||||
return this.first(key, () => {
|
||||
throw new Error('Item not found.');
|
||||
});
|
||||
}
|
||||
|
||||
const collection = this.where(key, operator, value);
|
||||
|
||||
if (collection.isEmpty()) {
|
||||
throw new Error('Item not found.');
|
||||
}
|
||||
|
||||
return collection.first();
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function firstWhere(key, operator, value) {
|
||||
return this.where(key, operator, value).first() || null;
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function flatMap(fn) {
|
||||
return this.map(fn).collapse();
|
||||
};
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject } = require('../helpers/is');
|
||||
|
||||
module.exports = function flatten(depth) {
|
||||
let flattenDepth = depth || Infinity;
|
||||
|
||||
let fullyFlattened = false;
|
||||
let collection = [];
|
||||
|
||||
const flat = function flat(items) {
|
||||
collection = [];
|
||||
|
||||
if (isArray(items)) {
|
||||
items.forEach((item) => {
|
||||
if (isArray(item)) {
|
||||
collection = collection.concat(item);
|
||||
} else if (isObject(item)) {
|
||||
Object.keys(item).forEach((property) => {
|
||||
collection = collection.concat(item[property]);
|
||||
});
|
||||
} else {
|
||||
collection.push(item);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Object.keys(items).forEach((property) => {
|
||||
if (isArray(items[property])) {
|
||||
collection = collection.concat(items[property]);
|
||||
} else if (isObject(items[property])) {
|
||||
Object.keys(items[property]).forEach((prop) => {
|
||||
collection = collection.concat(items[property][prop]);
|
||||
});
|
||||
} else {
|
||||
collection.push(items[property]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fullyFlattened = collection.filter(item => isObject(item));
|
||||
fullyFlattened = fullyFlattened.length === 0;
|
||||
|
||||
flattenDepth -= 1;
|
||||
};
|
||||
|
||||
flat(this.items);
|
||||
|
||||
while (!fullyFlattened && flattenDepth > 0) {
|
||||
flat(collection);
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function flip() {
|
||||
const collection = {};
|
||||
|
||||
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);
|
||||
};
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function forPage(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);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function forget(key) {
|
||||
if (Array.isArray(this.items)) {
|
||||
this.items.splice(key, 1);
|
||||
} else {
|
||||
delete this.items[key];
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function get(key, defaultValue = null) {
|
||||
if (this.items[key] !== undefined) {
|
||||
return this.items[key];
|
||||
}
|
||||
|
||||
if (isFunction(defaultValue)) {
|
||||
return defaultValue();
|
||||
}
|
||||
|
||||
if (defaultValue !== null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const nestedValue = require('../helpers/nestedValue');
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function groupBy(key) {
|
||||
const collection = {};
|
||||
|
||||
this.items.forEach((item, index) => {
|
||||
let resolvedKey;
|
||||
|
||||
if (isFunction(key)) {
|
||||
resolvedKey = key(item, index);
|
||||
} else if (nestedValue(item, key) || nestedValue(item, key) === 0) {
|
||||
resolvedKey = nestedValue(item, key);
|
||||
} else {
|
||||
resolvedKey = '';
|
||||
}
|
||||
|
||||
if (collection[resolvedKey] === undefined) {
|
||||
collection[resolvedKey] = new this.constructor([]);
|
||||
}
|
||||
|
||||
collection[resolvedKey].push(item);
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const variadic = require('../helpers/variadic');
|
||||
|
||||
module.exports = function has(...args) {
|
||||
const properties = variadic(args);
|
||||
|
||||
return properties.filter(key => Object.hasOwnProperty.call(this.items, key)).length
|
||||
=== properties.length;
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function implode(key, glue) {
|
||||
if (glue === undefined) {
|
||||
return this.items.join(key);
|
||||
}
|
||||
|
||||
return new this.constructor(this.items).pluck(key).all().join(glue);
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function intersect(values) {
|
||||
let intersectValues = values;
|
||||
|
||||
if (values instanceof this.constructor) {
|
||||
intersectValues = values.all();
|
||||
}
|
||||
|
||||
const collection = this.items
|
||||
.filter(item => intersectValues.indexOf(item) !== -1);
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function intersectByKeys(values) {
|
||||
let intersectKeys = Object.keys(values);
|
||||
|
||||
if (values instanceof this.constructor) {
|
||||
intersectKeys = Object.keys(values.all());
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
if (intersectKeys.indexOf(key) !== -1) {
|
||||
collection[key] = this.items[key];
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function isEmpty() {
|
||||
if (Array.isArray(this.items)) {
|
||||
return !this.items.length;
|
||||
}
|
||||
|
||||
return !Object.keys(this.items).length;
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function isNotEmpty() {
|
||||
return !this.isEmpty();
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function join(glue, finalGlue) {
|
||||
const collection = this.values();
|
||||
|
||||
if (finalGlue === undefined) {
|
||||
return collection.implode(glue);
|
||||
}
|
||||
|
||||
const count = collection.count();
|
||||
|
||||
if (count === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
return collection.last();
|
||||
}
|
||||
|
||||
const finalItem = collection.pop();
|
||||
|
||||
return collection.implode(glue) + finalGlue + finalItem;
|
||||
};
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const nestedValue = require('../helpers/nestedValue');
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function keyBy(key) {
|
||||
const collection = {};
|
||||
|
||||
if (isFunction(key)) {
|
||||
this.items.forEach((item) => {
|
||||
collection[key(item)] = item;
|
||||
});
|
||||
} else {
|
||||
this.items.forEach((item) => {
|
||||
const keyValue = nestedValue(item, key);
|
||||
|
||||
collection[keyValue || ''] = item;
|
||||
});
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function keys() {
|
||||
let collection = Object.keys(this.items);
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
collection = collection.map(Number);
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function last(fn, defaultValue) {
|
||||
let { items } = this;
|
||||
|
||||
if (isFunction(fn)) {
|
||||
items = this.filter(fn).all();
|
||||
}
|
||||
|
||||
if ((Array.isArray(items) && !items.length) || (!Object.keys(items).length)) {
|
||||
if (isFunction(defaultValue)) {
|
||||
return defaultValue();
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
return items[items.length - 1];
|
||||
}
|
||||
const keys = Object.keys(items);
|
||||
|
||||
return items[keys[keys.length - 1]];
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function macro(name, fn) {
|
||||
this.constructor.prototype[name] = fn;
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function make(items = []) {
|
||||
return new this.constructor(items);
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function map(fn) {
|
||||
if (Array.isArray(this.items)) {
|
||||
return new this.constructor(this.items.map(fn));
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
collection[key] = fn(this.items[key], key);
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mapInto(ClassName) {
|
||||
return this.map((value, key) => new ClassName(value, key));
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mapSpread(fn) {
|
||||
return this.map((values, key) => fn(...values, key));
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mapToDictionary(fn) {
|
||||
const collection = {};
|
||||
|
||||
this.items.forEach((item, k) => {
|
||||
const [key, value] = fn(item, k);
|
||||
|
||||
if (collection[key] === undefined) {
|
||||
collection[key] = [value];
|
||||
} else {
|
||||
collection[key].push(value);
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mapToGroups(fn) {
|
||||
const collection = {};
|
||||
|
||||
this.items.forEach((item, key) => {
|
||||
const [keyed, value] = fn(item, key);
|
||||
|
||||
if (collection[keyed] === undefined) {
|
||||
collection[keyed] = [value];
|
||||
} else {
|
||||
collection[keyed].push(value);
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mapWithKeys(fn) {
|
||||
const collection = {};
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
this.items.forEach((item, index) => {
|
||||
const [keyed, value] = fn(item, index);
|
||||
collection[keyed] = value;
|
||||
});
|
||||
} else {
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
const [keyed, value] = fn(this.items[key], key);
|
||||
collection[keyed] = value;
|
||||
});
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function max(key) {
|
||||
if (typeof key === 'string') {
|
||||
const filtered = this.items.filter(item => item[key] !== undefined);
|
||||
|
||||
return Math.max(...filtered.map(item => item[key]));
|
||||
}
|
||||
|
||||
return Math.max(...this.items);
|
||||
};
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function median(key) {
|
||||
const { length } = this.items;
|
||||
|
||||
if (key === undefined) {
|
||||
if (length % 2 === 0) {
|
||||
return (this.items[(length / 2) - 1] + this.items[length / 2]) / 2;
|
||||
}
|
||||
|
||||
return this.items[Math.floor(length / 2)];
|
||||
}
|
||||
|
||||
if (length % 2 === 0) {
|
||||
return (this.items[(length / 2) - 1][key]
|
||||
+ this.items[length / 2][key]) / 2;
|
||||
}
|
||||
|
||||
return this.items[Math.floor(length / 2)][key];
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
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(arrayOrObject).forEach((key) => {
|
||||
collection[key] = arrayOrObject[key];
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mergeRecursive(items) {
|
||||
const merge = (target, source) => {
|
||||
const merged = {};
|
||||
|
||||
const mergedKeys = Object.keys(Object.assign({}, target, source));
|
||||
|
||||
mergedKeys.forEach((key) => {
|
||||
if (target[key] === undefined && source[key] !== undefined) {
|
||||
merged[key] = source[key];
|
||||
} else if (target[key] !== undefined && source[key] === undefined) {
|
||||
merged[key] = target[key];
|
||||
} else if (target[key] !== undefined && source[key] !== undefined) {
|
||||
if (target[key] === source[key]) {
|
||||
merged[key] = target[key];
|
||||
} else if (
|
||||
(!Array.isArray(target[key]) && typeof target[key] === 'object')
|
||||
&& (!Array.isArray(source[key]) && typeof source[key] === 'object')
|
||||
) {
|
||||
merged[key] = merge(target[key], source[key]);
|
||||
} else {
|
||||
merged[key] = [].concat(target[key], source[key]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return merged;
|
||||
};
|
||||
|
||||
if (!items) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (items.constructor.name === 'Collection') {
|
||||
return new this.constructor(merge(this.items, items.all()));
|
||||
}
|
||||
|
||||
return new this.constructor(merge(this.items, items));
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function min(key) {
|
||||
if (key !== undefined) {
|
||||
const filtered = this.items.filter(item => item[key] !== undefined);
|
||||
|
||||
return Math.min(...filtered.map(item => item[key]));
|
||||
}
|
||||
|
||||
return Math.min(...this.items);
|
||||
};
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function mode(key) {
|
||||
const values = [];
|
||||
let highestCount = 1;
|
||||
|
||||
if (!this.items.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.items.forEach((item) => {
|
||||
const tempValues = values.filter((value) => {
|
||||
if (key !== undefined) {
|
||||
return value.key === item[key];
|
||||
}
|
||||
|
||||
return value.key === item;
|
||||
});
|
||||
|
||||
if (!tempValues.length) {
|
||||
if (key !== undefined) {
|
||||
values.push({ key: item[key], count: 1 });
|
||||
} else {
|
||||
values.push({ key: item, count: 1 });
|
||||
}
|
||||
} else {
|
||||
tempValues[0].count += 1;
|
||||
const { count } = tempValues[0];
|
||||
|
||||
if (count > highestCount) {
|
||||
highestCount = count;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return values
|
||||
.filter(value => value.count === highestCount)
|
||||
.map(value => value.key);
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
|
||||
module.exports = function nth(n, offset = 0) {
|
||||
const items = values(this.items);
|
||||
|
||||
const collection = items
|
||||
.slice(offset)
|
||||
.filter((item, index) => index % n === 0);
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
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) !== -1);
|
||||
|
||||
return new this.constructor(collection);
|
||||
}
|
||||
|
||||
const collection = {};
|
||||
|
||||
Object.keys(this.items).forEach((prop) => {
|
||||
if (properties.indexOf(prop) !== -1) {
|
||||
collection[prop] = this.items[prop];
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const clone = require('../helpers/clone');
|
||||
|
||||
module.exports = function pad(size, value) {
|
||||
const abs = Math.abs(size);
|
||||
const count = this.count();
|
||||
|
||||
if (abs <= count) {
|
||||
return this;
|
||||
}
|
||||
|
||||
let diff = abs - count;
|
||||
const items = clone(this.items);
|
||||
const isArray = Array.isArray(this.items);
|
||||
const prepend = size < 0;
|
||||
|
||||
for (let iterator = 0; iterator < diff;) {
|
||||
if (!isArray) {
|
||||
if (items[iterator] !== undefined) {
|
||||
diff += 1;
|
||||
} else {
|
||||
items[iterator] = value;
|
||||
}
|
||||
} else if (prepend) {
|
||||
items.unshift(value);
|
||||
} else {
|
||||
items.push(value);
|
||||
}
|
||||
|
||||
iterator += 1;
|
||||
}
|
||||
|
||||
return new this.constructor(items);
|
||||
};
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function partition(fn) {
|
||||
let arrays;
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
arrays = [new this.constructor([]), new this.constructor([])];
|
||||
|
||||
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);
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function pipe(fn) {
|
||||
return fn(this);
|
||||
};
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject } = require('../helpers/is');
|
||||
const nestedValue = require('../helpers/nestedValue');
|
||||
|
||||
const buildKeyPathMap = function buildKeyPathMap(items) {
|
||||
const keyPaths = {};
|
||||
|
||||
items.forEach((item, index) => {
|
||||
function buildKeyPath(val, keyPath) {
|
||||
if (isObject(val)) {
|
||||
Object.keys(val).forEach((prop) => {
|
||||
buildKeyPath(val[prop], `${keyPath}.${prop}`);
|
||||
});
|
||||
} else if (isArray(val)) {
|
||||
val.forEach((v, i) => {
|
||||
buildKeyPath(v, `${keyPath}.${i}`);
|
||||
});
|
||||
}
|
||||
|
||||
keyPaths[keyPath] = val;
|
||||
}
|
||||
|
||||
buildKeyPath(item, index);
|
||||
});
|
||||
|
||||
return keyPaths;
|
||||
};
|
||||
|
||||
module.exports = function pluck(value, key) {
|
||||
if (value.indexOf('*') !== -1) {
|
||||
const keyPathMap = buildKeyPathMap(this.items);
|
||||
|
||||
const keyMatches = [];
|
||||
|
||||
if (key !== undefined) {
|
||||
const keyRegex = new RegExp(`0.${key}`, 'g');
|
||||
const keyNumberOfLevels = `0.${key}`.split('.').length;
|
||||
|
||||
Object.keys(keyPathMap).forEach((k) => {
|
||||
const matchingKey = k.match(keyRegex);
|
||||
|
||||
if (matchingKey) {
|
||||
const match = matchingKey[0];
|
||||
|
||||
if (match.split('.').length === keyNumberOfLevels) {
|
||||
keyMatches.push(keyPathMap[match]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const valueMatches = [];
|
||||
const valueRegex = new RegExp(`0.${value}`, 'g');
|
||||
const valueNumberOfLevels = `0.${value}`.split('.').length;
|
||||
|
||||
|
||||
Object.keys(keyPathMap).forEach((k) => {
|
||||
const matchingValue = k.match(valueRegex);
|
||||
|
||||
if (matchingValue) {
|
||||
const match = matchingValue[0];
|
||||
|
||||
if (match.split('.').length === valueNumberOfLevels) {
|
||||
valueMatches.push(keyPathMap[match]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (key !== undefined) {
|
||||
const collection = {};
|
||||
|
||||
this.items.forEach((item, index) => {
|
||||
collection[keyMatches[index] || ''] = valueMatches;
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
}
|
||||
|
||||
return new this.constructor([valueMatches]);
|
||||
}
|
||||
|
||||
if (key !== undefined) {
|
||||
const collection = {};
|
||||
|
||||
this.items.forEach((item) => {
|
||||
if (nestedValue(item, value) !== undefined) {
|
||||
collection[item[key] || ''] = nestedValue(item, value);
|
||||
} else {
|
||||
collection[item[key] || ''] = null;
|
||||
}
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
}
|
||||
|
||||
return this.map((item) => {
|
||||
if (nestedValue(item, value) !== undefined) {
|
||||
return nestedValue(item, value);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
};
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject } = require('../helpers/is');
|
||||
const deleteKeys = require('../helpers/deleteKeys');
|
||||
|
||||
module.exports = function pop(count = 1) {
|
||||
if (this.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isArray(this.items)) {
|
||||
if (count === 1) {
|
||||
return this.items.pop();
|
||||
}
|
||||
|
||||
return new this.constructor(this.items.splice(-count));
|
||||
}
|
||||
|
||||
if (isObject(this.items)) {
|
||||
const keys = Object.keys(this.items);
|
||||
|
||||
if (count === 1) {
|
||||
const key = keys[keys.length - 1];
|
||||
const last = this.items[key];
|
||||
|
||||
deleteKeys(this.items, key);
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
const poppedKeys = keys.slice(-count);
|
||||
|
||||
const newObject = poppedKeys.reduce((acc, current) => {
|
||||
acc[current] = this.items[current];
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
deleteKeys(this.items, poppedKeys);
|
||||
|
||||
return new this.constructor(newObject);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function prepend(value, key) {
|
||||
if (key !== undefined) {
|
||||
return this.put(key, value);
|
||||
}
|
||||
|
||||
this.items.unshift(value);
|
||||
|
||||
return this;
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function pull(key, defaultValue) {
|
||||
let returnValue = this.items[key] || null;
|
||||
|
||||
if (!returnValue && defaultValue !== undefined) {
|
||||
if (isFunction(defaultValue)) {
|
||||
returnValue = defaultValue();
|
||||
} else {
|
||||
returnValue = defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
delete this.items[key];
|
||||
|
||||
return returnValue;
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function push(...items) {
|
||||
this.items.push(...items);
|
||||
|
||||
return this;
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function put(key, value) {
|
||||
this.items[key] = value;
|
||||
|
||||
return this;
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
|
||||
module.exports = function random(length = null) {
|
||||
const items = values(this.items);
|
||||
|
||||
const collection = new this.constructor(items).shuffle();
|
||||
|
||||
// If not a length was specified
|
||||
if (length !== parseInt(length, 10)) {
|
||||
return collection.first();
|
||||
}
|
||||
|
||||
return collection.take(length);
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function reduce(fn, carry) {
|
||||
let reduceCarry = null;
|
||||
|
||||
if (carry !== undefined) {
|
||||
reduceCarry = carry;
|
||||
}
|
||||
|
||||
if (Array.isArray(this.items)) {
|
||||
this.items.forEach((item) => {
|
||||
reduceCarry = fn(reduceCarry, item);
|
||||
});
|
||||
} else {
|
||||
Object.keys(this.items).forEach((key) => {
|
||||
reduceCarry = fn(reduceCarry, this.items[key], key);
|
||||
});
|
||||
}
|
||||
|
||||
return reduceCarry;
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function reject(fn) {
|
||||
return new this.constructor(this.items).filter(item => !fn(item));
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function replace(items) {
|
||||
if (!items) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
const replaced = this.items.map((value, index) => items[index] || value);
|
||||
|
||||
return new this.constructor(replaced);
|
||||
}
|
||||
|
||||
if (items.constructor.name === 'Collection') {
|
||||
const replaced = Object.assign({}, this.items, items.all());
|
||||
|
||||
return new this.constructor(replaced);
|
||||
}
|
||||
|
||||
const replaced = Object.assign({}, this.items, items);
|
||||
|
||||
return new this.constructor(replaced);
|
||||
};
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function replaceRecursive(items) {
|
||||
const replace = (target, source) => {
|
||||
const replaced = Object.assign({}, target);
|
||||
|
||||
const mergedKeys = Object.keys(Object.assign({}, target, source));
|
||||
|
||||
mergedKeys.forEach((key) => {
|
||||
if (!Array.isArray(source[key]) && typeof source[key] === 'object') {
|
||||
replaced[key] = replace(target[key], source[key]);
|
||||
} else if (target[key] === undefined && source[key] !== undefined) {
|
||||
if (typeof target[key] === 'object') {
|
||||
replaced[key] = Object.assign({}, source[key]);
|
||||
} else {
|
||||
replaced[key] = source[key];
|
||||
}
|
||||
} else if (target[key] !== undefined && source[key] === undefined) {
|
||||
if (typeof target[key] === 'object') {
|
||||
replaced[key] = Object.assign({}, target[key]);
|
||||
} else {
|
||||
replaced[key] = target[key];
|
||||
}
|
||||
} else if (target[key] !== undefined && source[key] !== undefined) {
|
||||
if (typeof source[key] === 'object') {
|
||||
replaced[key] = Object.assign({}, source[key]);
|
||||
} else {
|
||||
replaced[key] = source[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return replaced;
|
||||
};
|
||||
|
||||
if (!items) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!Array.isArray(items) && typeof items !== 'object') {
|
||||
return new this.constructor(replace(this.items, [items]));
|
||||
}
|
||||
|
||||
if (items.constructor.name === 'Collection') {
|
||||
return new this.constructor(replace(this.items, items.all()));
|
||||
}
|
||||
|
||||
return new this.constructor(replace(this.items, items));
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function reverse() {
|
||||
const collection = [].concat(this.items).reverse();
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/* eslint-disable eqeqeq */
|
||||
|
||||
const { isArray, isObject, isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function search(valueOrFunction, strict) {
|
||||
let result;
|
||||
|
||||
const find = (item, key) => {
|
||||
if (isFunction(valueOrFunction)) {
|
||||
return valueOrFunction(this.items[key], key);
|
||||
}
|
||||
|
||||
if (strict) {
|
||||
return this.items[key] === valueOrFunction;
|
||||
}
|
||||
|
||||
return this.items[key] == valueOrFunction;
|
||||
};
|
||||
|
||||
if (isArray(this.items)) {
|
||||
result = this.items.findIndex(find);
|
||||
} else if (isObject(this.items)) {
|
||||
result = Object.keys(this.items).find(key => find(this.items[key], key));
|
||||
}
|
||||
|
||||
if (result === undefined || result < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject } = require('../helpers/is');
|
||||
const deleteKeys = require('../helpers/deleteKeys');
|
||||
|
||||
module.exports = function shift(count = 1) {
|
||||
if (this.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isArray(this.items)) {
|
||||
if (count === 1) {
|
||||
return this.items.shift();
|
||||
}
|
||||
|
||||
return new this.constructor(this.items.splice(0, count));
|
||||
}
|
||||
|
||||
if (isObject(this.items)) {
|
||||
if (count === 1) {
|
||||
const key = Object.keys(this.items)[0];
|
||||
const value = this.items[key];
|
||||
delete this.items[key];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
const keys = Object.keys(this.items);
|
||||
const poppedKeys = keys.slice(0, count);
|
||||
|
||||
const newObject = poppedKeys.reduce((acc, current) => {
|
||||
acc[current] = this.items[current];
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
deleteKeys(this.items, poppedKeys);
|
||||
|
||||
return new this.constructor(newObject);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
|
||||
module.exports = function shuffle() {
|
||||
const items = values(this.items);
|
||||
|
||||
let j;
|
||||
let x;
|
||||
let i;
|
||||
|
||||
for (i = items.length; i; i -= 1) {
|
||||
j = Math.floor(Math.random() * i);
|
||||
x = items[i - 1];
|
||||
items[i - 1] = items[j];
|
||||
items[j] = x;
|
||||
}
|
||||
|
||||
this.items = items;
|
||||
|
||||
return this;
|
||||
};
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const { isObject } = require('../helpers/is');
|
||||
|
||||
module.exports = function skip(number) {
|
||||
if (isObject(this.items)) {
|
||||
return new this.constructor(
|
||||
Object.keys(this.items)
|
||||
.reduce((accumulator, key, index) => {
|
||||
if ((index + 1) > number) {
|
||||
accumulator[key] = this.items[key];
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}, {}),
|
||||
);
|
||||
}
|
||||
|
||||
return new this.constructor(this.items.slice(number));
|
||||
};
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject, isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function skipUntil(valueOrFunction) {
|
||||
let previous = null;
|
||||
let items;
|
||||
|
||||
let callback = value => value === valueOrFunction;
|
||||
if (isFunction(valueOrFunction)) {
|
||||
callback = valueOrFunction;
|
||||
}
|
||||
|
||||
if (isArray(this.items)) {
|
||||
items = this.items.filter((item) => {
|
||||
if (previous !== true) {
|
||||
previous = callback(item);
|
||||
}
|
||||
|
||||
return previous;
|
||||
});
|
||||
}
|
||||
|
||||
if (isObject(this.items)) {
|
||||
items = Object.keys(this.items).reduce((acc, key) => {
|
||||
if (previous !== true) {
|
||||
previous = callback(this.items[key]);
|
||||
}
|
||||
|
||||
if (previous !== false) {
|
||||
acc[key] = this.items[key];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
return new this.constructor(items);
|
||||
};
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const { isArray, isObject, isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function skipWhile(valueOrFunction) {
|
||||
let previous = null;
|
||||
let items;
|
||||
|
||||
let callback = value => value === valueOrFunction;
|
||||
if (isFunction(valueOrFunction)) {
|
||||
callback = valueOrFunction;
|
||||
}
|
||||
|
||||
if (isArray(this.items)) {
|
||||
items = this.items.filter((item) => {
|
||||
if (previous !== true) {
|
||||
previous = !callback(item);
|
||||
}
|
||||
|
||||
return previous;
|
||||
});
|
||||
}
|
||||
|
||||
if (isObject(this.items)) {
|
||||
items = Object.keys(this.items).reduce((acc, key) => {
|
||||
if (previous !== true) {
|
||||
previous = !callback(this.items[key]);
|
||||
}
|
||||
|
||||
if (previous !== false) {
|
||||
acc[key] = this.items[key];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
return new this.constructor(items);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function slice(remove, limit) {
|
||||
let collection = this.items.slice(remove);
|
||||
|
||||
if (limit !== undefined) {
|
||||
collection = collection.slice(0, limit);
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function sole(key, operator, value) {
|
||||
let collection;
|
||||
|
||||
if (isFunction(key)) {
|
||||
collection = this.filter(key);
|
||||
} else {
|
||||
collection = this.where(key, operator, value);
|
||||
}
|
||||
|
||||
if (collection.isEmpty()) {
|
||||
throw new Error('Item not found.');
|
||||
}
|
||||
|
||||
if (collection.count() > 1) {
|
||||
throw new Error('Multiple items found.');
|
||||
}
|
||||
|
||||
return collection.first();
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const contains = require('./contains');
|
||||
|
||||
module.exports = contains;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function sort(fn) {
|
||||
const collection = [].concat(this.items);
|
||||
|
||||
if (fn === undefined) {
|
||||
if (this.every(item => typeof item === 'number')) {
|
||||
collection.sort((a, b) => a - b);
|
||||
} else {
|
||||
collection.sort();
|
||||
}
|
||||
} else {
|
||||
collection.sort(fn);
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
const nestedValue = require('../helpers/nestedValue');
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function sortBy(valueOrFunction) {
|
||||
const collection = [].concat(this.items);
|
||||
const getValue = (item) => {
|
||||
if (isFunction(valueOrFunction)) {
|
||||
return valueOrFunction(item);
|
||||
}
|
||||
|
||||
return nestedValue(item, valueOrFunction);
|
||||
};
|
||||
|
||||
collection.sort((a, b) => {
|
||||
const valueA = getValue(a);
|
||||
const valueB = getValue(b);
|
||||
|
||||
if (valueA === null || valueA === undefined) {
|
||||
return 1;
|
||||
}
|
||||
if (valueB === null || valueB === undefined) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (valueA < valueB) {
|
||||
return -1;
|
||||
}
|
||||
if (valueA > valueB) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function sortByDesc(valueOrFunction) {
|
||||
return this.sortBy(valueOrFunction).reverse();
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function sortDesc() {
|
||||
return this.sort().reverse();
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function sortKeys() {
|
||||
const ordered = {};
|
||||
|
||||
Object.keys(this.items).sort().forEach((key) => {
|
||||
ordered[key] = this.items[key];
|
||||
});
|
||||
|
||||
return new this.constructor(ordered);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function sortKeysDesc() {
|
||||
const ordered = {};
|
||||
|
||||
Object.keys(this.items).sort().reverse().forEach((key) => {
|
||||
ordered[key] = this.items[key];
|
||||
});
|
||||
|
||||
return new this.constructor(ordered);
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function splice(index, limit, replace) {
|
||||
const slicedCollection = this.slice(index, limit);
|
||||
|
||||
this.items = this.diff(slicedCollection.all()).all();
|
||||
|
||||
if (Array.isArray(replace)) {
|
||||
for (let iterator = 0, { length } = replace;
|
||||
iterator < length; iterator += 1) {
|
||||
this.items.splice(index + iterator, 0, replace[iterator]);
|
||||
}
|
||||
}
|
||||
|
||||
return slicedCollection;
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function split(numberOfGroups) {
|
||||
const itemsPerGroup = Math.round(this.items.length / numberOfGroups);
|
||||
|
||||
const items = JSON.parse(JSON.stringify(this.items));
|
||||
const collection = [];
|
||||
|
||||
for (let iterator = 0; iterator < numberOfGroups; iterator += 1) {
|
||||
collection.push(new this.constructor(items.splice(0, itemsPerGroup)));
|
||||
}
|
||||
|
||||
return new this.constructor(collection);
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const values = require('../helpers/values');
|
||||
const { isFunction } = require('../helpers/is');
|
||||
|
||||
module.exports = function sum(key) {
|
||||
const items = values(this.items);
|
||||
|
||||
let total = 0;
|
||||
|
||||
if (key === undefined) {
|
||||
for (let i = 0, { length } = items; i < length; i += 1) {
|
||||
total += parseFloat(items[i]);
|
||||
}
|
||||
} else if (isFunction(key)) {
|
||||
for (let i = 0, { length } = items; i < length; i += 1) {
|
||||
total += parseFloat(key(items[i]));
|
||||
}
|
||||
} else {
|
||||
for (let i = 0, { length } = items; i < length; i += 1) {
|
||||
total += parseFloat(items[i][key]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return parseFloat(total.toPrecision(12));
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function SymbolIterator() {
|
||||
let index = -1;
|
||||
|
||||
return {
|
||||
next: () => {
|
||||
index += 1;
|
||||
|
||||
return {
|
||||
value: this.items[index],
|
||||
done: index >= this.items.length,
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user