Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-loops

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-loops - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

chain.js

24

filter.js

@@ -1,7 +0,5 @@

'use strict';
/**
* @module object-loops/filter
*/
var isObject = require('101/is-object');
var forEach = require('./for-each');
var forEach = require('./for-each')

@@ -16,19 +14,19 @@ /**

*/
module.exports = filter;
module.exports = filter
function filter (obj, callback, thisArg) {
if (Array.isArray(obj)) {
return obj.filter(callback, thisArg);
return obj.filter(callback, thisArg)
}
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function')
}
var filteredObj = {};
var filteredObj = {}
forEach(obj, function (val, key, obj) {
var include = callback.call(thisArg, val, key, obj);
var include = callback.call(thisArg, val, key, obj)
if (include) {
filteredObj[key] = val;
filteredObj[key] = val
}
});
return filteredObj;
})
return filteredObj
}

@@ -42,2 +40,2 @@ /**

* @returns {boolean} include - return true to keep that entry, false otherwise
*/
*/

@@ -1,6 +0,5 @@

'use strict';
/**
* @module object-loops/for-each
*/
var isObject = require('101/is-object');
var isObject = require('101/is-object')
/**

@@ -13,18 +12,18 @@ * Executes a provided function once per each object value.

*/
module.exports = forEach;
module.exports = forEach
function forEach (obj, callback, thisArg) {
if (Array.isArray(obj)) {
return obj.forEach(callback, thisArg);
return obj.forEach(callback, thisArg)
}
if (!isObject(obj)) {
throw new TypeError(obj + ' is not an object');
throw new TypeError(obj + ' is not an object')
}
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function')
}
Object.keys(obj).forEach(function (key) {
var val = obj[key];
callback.call(thisArg, val, key, obj);
});
var val = obj[key]
callback.call(thisArg, val, key, obj)
})
}

@@ -37,2 +36,2 @@ /**

* @param {object} obj - object which values are being iterated
*/
*/

@@ -1,9 +0,8 @@

'use strict';
/**
* @module object-loops
*/
var dasherize = require('dasherize');
var exists = require('101/exists');
var envIs = require('101/env-is');
var path = require('path');
var dasherize = require('dasherize')
var exists = require('101/exists')
var envIs = require('101/env-is')
var path = require('path')

@@ -15,3 +14,3 @@ /**

*/
module.exports = extendObjectPrototype;
module.exports = extendObjectPrototype

@@ -21,28 +20,27 @@ function extendObjectPrototype (hideWarnings) {

'forEach',
'filter',
'map',
'mapKeys',
'filter',
'reduce'
].forEach(function (methodName) {
var filename = dasherize(methodName);
var filepath = path.resolve('./'+filename);
var filename = dasherize(methodName)
var filepath = path.resolve('./' + filename)
if (Object.prototype[methodName] && !hideWarnings) {
console.log('warn: "Object.prototype.' + methodName + '" already exists.');
}
else {
var method = require(filepath);
Object.defineProperty(Object.prototype, methodName, {
console.log('warn: "Object.prototype.' + methodName + '" already exists.')
} else {
var method = require(filepath)
Object.defineProperty(Object.prototype, methodName, { // eslint-disable-line
value: function () {
if (this === global || !exists(this)) {
throw new TypeError('this is null or not defined for '+method);
throw new TypeError('this is null or not defined for ' + method)
}
var args = Array.prototype.slice.call(arguments);
args.unshift(this); // sets first arg as object instance
return method.apply(this, args);
var args = Array.prototype.slice.call(arguments)
args.unshift(this) // sets first arg as object instance
return method.apply(this, args)
},
enumerable: false,
configurable: envIs('test') // hack for tests
});
})
}
});
})
}

@@ -1,40 +0,38 @@

'use strict';
/**
* @module object-loops/map
*/
var isObject = require('101/is-object');
var forEach = require('./for-each');
var forEach = require('./for-each')
/**
* Creates a new object with the results of calling a provided function on every value in the object.
* Creates a new object with the results of calling a provided function on every key in the object.
* @function module:object-loops/map
* @param {object} [obj] - object to map values, not accepted if being used directly on Object.prototype
* @param {mapCallback} callback - function that produces the new value for the new, mapped object
* @param {object} [obj] - object to map keys, not accepted if being used directly on Object.prototype
* @param {mapKeysCallback} callback - function that produces the new key for the new mapped object
* @param {*} [thisArg] - optional. context to bind to callback
* @returns {object} newly created object with mapped values
* @returns {object} newly created object with mapped keys
*/
module.exports = mapKeys;
module.exports = mapKeys
function mapKeys (obj, callback, thisArg) {
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function')
}
var objIsArray = Array.isArray(obj);
var objIsArray = Array.isArray(obj)
if (objIsArray) {
forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);
forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach)
}
var mapped = objIsArray ? [] : {};
var mapped = objIsArray ? [] : {}
forEach(obj, function (val, key, obj) {
var newKey = callback.call(thisArg, key, val, obj);
mapped[newKey] = val;
});
return mapped;
var newKey = callback.call(thisArg, key, val, obj)
mapped[newKey] = val
})
return mapped
}
/**
* This callback type is called `mapCallback` and is displayed as a global symbol.
* @callback mapCallback
* This callback type is called `mapKeysCallback` and is displayed as a global symbol.
* @callback mapKeysCallback
* @param {string} key - object key (used in current iteration)
* @param {*} val - value for key
* @param {object} obj - object which values are being iterated
* @returns {*} mappedValue - value for key in the new, mapped object
*/
* @param {object} obj - object which keys are being iterated
* @returns {*} mappedKey - value for key in the new, mapped object
*/

@@ -1,7 +0,5 @@

'use strict';
/**
* @module object-loops/map
*/
var isObject = require('101/is-object');
var forEach = require('./for-each');
var forEach = require('./for-each')

@@ -12,20 +10,20 @@ /**

* @param {object} [obj] - object to map values, not accepted if being used directly on Object.prototype
* @param {mapCallback} callback - function that produces the new value for the new, mapped object
* @param {mapCallback} callback - function that produces the new value for the new mapped object
* @param {*} [thisArg] - optional. context to bind to callback
* @returns {object} newly created object with mapped values
*/
module.exports = map;
module.exports = map
function map (obj, callback, thisArg) {
if (Array.isArray(obj)) {
return obj.map(callback, thisArg);
return obj.map(callback, thisArg)
}
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function')
}
var mappedObj = {};
var mappedObj = {}
forEach(obj, function (val, key, obj) {
mappedObj[key] = callback.call(thisArg, val, key, obj);
});
return mappedObj;
mappedObj[key] = callback.call(thisArg, val, key, obj)
})
return mappedObj
}

@@ -39,2 +37,2 @@ /**

* @returns {*} mappedValue - value for key in the new, mapped object
*/
*/
{
"name": "object-loops",
"version": "0.3.1",
"version": "0.4.0",
"description": "Functional methods like forEach, map, filter, and other ES5 Array methods for Objects in javascript",

@@ -10,4 +10,6 @@ "main": "index.js",

"scripts": {
"test": "lab -c test",
"test-watch": "nodemon --exec lab -c test"
"test": "lab -t 100 -a code",
"test-watch": "nodemon --exec npm test",
"lint": "standard",
"format": "standard --format"
},

@@ -24,2 +26,5 @@ "repository": {

"map",
"map keys",
"keys",
"mapKeys",
"reduce",

@@ -39,12 +44,12 @@ "filter",

"dependencies": {
"101": "^0.8.0",
"dasherize": "^1.0.0"
"101": "^1.2.0",
"dasherize": "^2.0.0"
},
"devDependencies": {
"i": "^0.3.2",
"code": "^1.2.1",
"lab": "^5.1.0",
"nodemon": "^1.2.1",
"sinon": "^1.12.2"
"code": "^1.5.0",
"lab": "^6.2.0",
"nodemon": "^1.8.1",
"sinon": "^1.17.2",
"standard": "^5.4.1"
}
}

@@ -12,16 +12,43 @@ object-loops [![Build Status](https://travis-ci.org/tjmehta/object-loops.svg)](https://travis-ci.org/tjmehta/object-loops)

#### If you want to use forEach, map, reduce, filter, etc methods directly on objects:
#### You can require each method individually `object-loop/<loop>`
```js
var filter = require('object-loops/filter')
var forEach = require('object-loops/for-each')
var mapKeys = require('object-loops/map-keys')
var map = require('object-loops/map')
var reduce = require('object-loops/reduce')
// usage
forEach({ x:10, y: 20 }, callback)
filter({ x:10, y: 20 }, callback)
mapKeys({ x:10, y: 20 }, callback)
map({ x:10, y: 20 }, callback)
reduce({ x:10, y: 20 }, callback)
```
require('object-loops')();
// obj.forEach()
#### If you want to chain multiple object-loops use `object-loop/chain`
```js
var chain = require('object-loops/chain')
chain({ x:10, y: 20 })
.filter(callback)
.mapKeys(callback)
.map(callback)
.reduce(callback)
.toJSON() // must be called at the end to return modified object
```
#### If you do not like the idea of extending Object.prototype you can require each method individually:
#### If you want to use forEach, map, reduce, filter, etc methods directly on objects:
```js
require('object-loops')() // extends Object.prototype
obj.forEach(callback)
obj.filter(callback)
obj.mapKeys(callback)
obj.map(callback)
obj.reduce(callback)
```
var objForEach = require('object-loops/for-each');
objForEach({ x:10, y: 20 }, callback, thisArg);
```
## forEach

@@ -36,3 +63,3 @@

```js
require('object-loops')(); // extends Object.prototype
var forEach = require('object-loops/for-each')

@@ -43,11 +70,11 @@ var obj = {

baz: 30
};
var keyConcat = '';
var valSum = 0;
obj.forEach(function (val, key, obj) {
keyConcat += key;
valSum += val;
});
keyConcat; // = 'foobarbaz'
valSum; // = 60
}
var keyConcat = ''
var valSum = 0
forEach(obj, function (val, key, obj) {
keyConcat += key
valSum += val
})
keyConcat // = 'foobarbaz'
valSum // = 60
```

@@ -65,3 +92,3 @@

```js
require('object-loops')(); // extends Object.prototype
var map = require('object-loops/map')

@@ -72,7 +99,7 @@ var obj = {

baz: 30
};
var mappedObj = obj.map(function (val, key, obj) {
return val*2;
});
mappedObj; /* Each val multiplied by 2
}
var mappedObj = map(obj, function (val, key, obj) {
return val*2
})
mappedObj /* Each val multiplied by 2
{

@@ -96,3 +123,3 @@ foo: 20,

```js
require('object-loops')(); // extends Object.prototype
var filter = require('object-loops/filter')

@@ -104,7 +131,7 @@ var obj = {

qux: 40,
};
var filteredObj = obj.filter(function (val, key, obj) {
return val > 25;
});
filteredObj; /* Only has entries with vals greater than 25
}
var filteredObj = filter(obj, function (val, key, obj) {
return val > 25
})
filteredObj /* Only has entries with vals greater than 25
{

@@ -127,3 +154,3 @@ baz: 30,

```js
require('object-loops')(); // extends Object.prototype
var reduce = require('object-loops/reduce')

@@ -134,7 +161,7 @@ var obj = {

baz: 30
};
var valSum = obj.reduce(function (prevVal, val, key, obj) {
return prevVal + val;
});
valSum; // 60
}
var sum = reduce(obj, function (prevVal, val, key, obj) {
return prevVal + val
})
sum // 60
```

@@ -141,0 +168,0 @@

@@ -1,7 +0,5 @@

'use strict';
/**
* @module object-loops/reduce
*/
var isObject = require('101/is-object');
var forEach = require('./for-each');
var isObject = require('101/is-object')

@@ -16,39 +14,38 @@ /**

*/
module.exports = reduce;
module.exports = reduce
function reduce (obj, callback, initialValue) {
if (Array.isArray(obj)) {
return (arguments.length > 2) ?
obj.reduce(callback, initialValue) :
obj.reduce(callback);
return (arguments.length > 2)
? obj.reduce(callback, initialValue)
: obj.reduce(callback)
}
if (!isObject(obj)) {
throw new TypeError(obj + ' is not an object');
throw new TypeError(obj + ' is not an object')
}
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function')
}
var keys = Object.keys(obj);
var noInitialValue = arguments.length < 3; // initial value can be null or undefined
var keys = Object.keys(obj)
var noInitialValue = arguments.length < 3 // initial value can be null or undefined
if (keys.length === 0 && noInitialValue) {
throw new Error('Reduce of empty object with no initial value');
throw new Error('Reduce of empty object with no initial value')
}
if (keys.length === 1 && noInitialValue) {
return obj[keys[0]]; // return first value
return obj[keys[0]] // return first value
}
var finalValue = noInitialValue ?
keys.reduce(reduction) :
keys.reduce(reduction, initialValue);
var finalValue = noInitialValue
? keys.reduce(reduction)
: keys.reduce(reduction, initialValue)
function reduction (prevVal, key, i) {
if (noInitialValue && i === 1) {
// if no initial value, prevVal is first KEY
prevVal = obj[prevVal];
// if no initial value, prevVal is first KEY
prevVal = obj[prevVal]
}
var val = obj[key];
var out = callback.call(null, prevVal, val, key, obj);
return out;
var val = obj[key]
return callback(prevVal, val, key, obj)
}
return finalValue;
return finalValue
}

@@ -63,2 +60,2 @@ /**

* @returns {*} memo - return true to keep that entry, false otherwise
*/
*/

@@ -1,31 +0,16 @@

var i = require('i')();
var path = require('path');
var fs = require('fs');
var methodNames = [
'forEach',
'filter',
'map',
'mapKeys',
'reduce'
]
var methodNames = fs.readdirSync(path.resolve(__dirname+'/../../'))
.filter(matches(/.js$/))
.map(replace('.js', ''))
.map(camelize);
module.exports = resetObjectPrototype
module.exports = resetObjectPrototype;
function resetObjectPrototype (cb) {
methodNames.forEach(function (methodName) {
delete Object.prototype[methodName];
});
cb();
delete Object.prototype[methodName] // eslint-disable-line
})
cb()
}
function matches (re) {
return function (str) {
return re.test(str);
};
}
function replace (substr) {
return function (str) {
return str.replace(substr, '');
};
}
function camelize (str) {
return i.camelize(str.replace(/-/g, '_'), false);
}

@@ -1,19 +0,19 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var sinon = require('sinon');
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var sinon = require('sinon')
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var beforeEach = lab.beforeEach;
var afterEach = lab.afterEach;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var expect = Code.expect
var noop = require('101/noop');
var equals = require('101/equals');
var passAny = require('101/pass-any');
var filter = require('../filter');
var equalsOneOrThree = passAny(equals(1), equals(3));
var noop = require('101/noop')
var equals = require('101/equals')
var passAny = require('101/pass-any')
var filter = require('../filter')
var equalsOneOrThree = passAny(equals(1), equals(3))

@@ -23,6 +23,6 @@ describe('filter', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -33,20 +33,20 @@ var obj = {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
obj.filter(callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
obj.filter(callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new filterped values', function (done) {

@@ -57,17 +57,15 @@ var obj = {

baz: 3
};
var thisArg = {};
var filteredObj = obj.filter(equalsOneOrThree);
}
var filteredObj = obj.filter(equalsOneOrThree)
Object.keys(obj).forEach(function (key) {
var val = obj[key];
var val = obj[key]
if (equalsOneOrThree(val)) {
expect(filteredObj[key]).to.equal(obj[key]);
expect(filteredObj[key]).to.equal(obj[key])
} else {
expect(filteredObj[key]).to.be.undefined()
}
else {
expect(filteredObj[key]).to.be.undefined();
}
});
done();
});
});
})
done()
})
})
describe('require', function () {

@@ -79,20 +77,20 @@ it('should iterate through all the key-value pairs in the object', function (done) {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
filter(obj, callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
filter(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new filterped values', function (done) {

@@ -103,25 +101,23 @@ var obj = {

baz: 3
};
var thisArg = {};
var filteredObj = filter(obj, equalsOneOrThree);
}
var filteredObj = filter(obj, equalsOneOrThree)
Object.keys(obj).forEach(function (key) {
var val = obj[key];
var val = obj[key]
if (equalsOneOrThree(val)) {
expect(filteredObj[key]).to.equal(obj[key]);
expect(filteredObj[key]).to.equal(obj[key])
} else {
expect(filteredObj[key]).to.be.undefined()
}
else {
expect(filteredObj[key]).to.be.undefined();
}
});
done();
});
})
done()
})
describe('errors', function () {
it('should throw an error if obj is not an object', function (done) {
var obj = 'notObject';
var callback = noop;
var thisArg = {};
var fn = filter.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not an object/);
done();
});
var obj = 'notObject'
var callback = noop
var thisArg = {}
var fn = filter.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not an object/)
done()
})
it('should throw an error if callback is not a function', function (done) {

@@ -132,34 +128,28 @@ var obj = {

baz: 3
};
var callback = 'notFunction';
var thisArg = {};
var fn = filter.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not a function/);
done();
});
});
}
var callback = 'notFunction'
var thisArg = {}
var fn = filter.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not a function/)
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'filter');
done();
});
sinon.spy(Array.prototype, 'filter')
done()
})
afterEach(function (done) {
Array.prototype.filter.restore();
done();
});
Array.prototype.filter.restore()
done()
})
it('should use array filter', function (done) {
var arr = [1,2,3];
var arr = [1, 2, 3]
expect(filter(arr, equalsOneOrThree, arr))
.to.deep.equal(arr.filter(equalsOneOrThree, arr));
sinon.assert.calledWith(Array.prototype.filter, equalsOneOrThree, arr);
done();
});
});
});
});
function multiplyBy (multiplier) {
return function (multiplicand) {
return multiplicand * multiplier;
};
}
.to.deep.equal(arr.filter(equalsOneOrThree, arr))
sinon.assert.calledWith(Array.prototype.filter, equalsOneOrThree, arr)
done()
})
})
})
})

@@ -1,16 +0,16 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var sinon = require('sinon');
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var sinon = require('sinon')
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var beforeEach = lab.beforeEach;
var afterEach = lab.afterEach;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var expect = Code.expect
var noop = require('101/noop');
var forEach = require('../for-each');
var noop = require('101/noop')
var forEach = require('../for-each')

@@ -20,6 +20,6 @@ describe('forEach', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -30,21 +30,21 @@ var obj = {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
obj.forEach(callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
obj.forEach(callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
})
describe('require', function () {

@@ -56,29 +56,29 @@ it('should iterate through all the key-value pairs in the object', function (done) {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
forEach(obj, callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
forEach(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
describe('errors', function () {
it('should throw an error if obj is not an object', function (done) {
var obj = 'notObject';
var callback = noop;
var thisArg = {};
var fn = forEach.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not an object/);
done();
});
var obj = 'notObject'
var callback = noop
var thisArg = {}
var fn = forEach.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not an object/)
done()
})
it('should throw an error if callback is not a function', function (done) {

@@ -89,29 +89,29 @@ var obj = {

baz: 3
};
var callback = 'notFunction';
var thisArg = {};
var fn = forEach.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not a function/);
done();
});
});
}
var callback = 'notFunction'
var thisArg = {}
var fn = forEach.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not a function/)
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'forEach');
done();
});
sinon.spy(Array.prototype, 'forEach')
done()
})
afterEach(function (done) {
Array.prototype.forEach.restore();
done();
});
Array.prototype.forEach.restore()
done()
})
it('should use array forEach', function (done) {
var arr = [1,2,3];
var callback = noop;
var arr = [1, 2, 3]
var callback = noop
expect(forEach(arr, callback, arr))
.to.equal(arr.forEach(callback, arr));
sinon.assert.calledWith(Array.prototype.forEach, callback, arr);
done();
});
});
});
});
.to.equal(arr.forEach(callback, arr))
sinon.assert.calledWith(Array.prototype.forEach, callback, arr)
done()
})
})
})
})

@@ -1,15 +0,12 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var beforeEach = lab.beforeEach;
var afterEach = lab.afterEach;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var expect = Code.expect
var noop = require('101/noop');
var forEach = require('../for-each');
var noop = require('101/noop')

@@ -19,6 +16,6 @@ describe('index', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -29,10 +26,10 @@ var obj = {

baz: 3
};
var callback = noop;
var thisArg = {};
var fn = obj.forEach.bind(undefined, callback, thisArg);
}
var callback = noop
var thisArg = {}
var fn = obj.forEach.bind(undefined, callback, thisArg)
// assertions
expect(fn).to.throw(/this/);
done();
});
expect(fn).to.throw(/this/)
done()
})
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -43,32 +40,32 @@ var obj = {

baz: 3
};
var callback = noop;
var thisArg = {};
var forEach = obj.forEach;
var fn = forEach.bind(global, callback, thisArg);
}
var callback = noop
var thisArg = {}
var forEach = obj.forEach
var fn = forEach.bind(global, callback, thisArg)
// assertions
expect(fn).to.throw(/this/);
done();
});
});
expect(fn).to.throw(/this/)
done()
})
})
describe('warnings', function () {
var consoleLog = console.log;
var consoleLog = console.log
before(function (done) {
Object.prototype.forEach = noop;
done();
});
after(require('./fixtures/reset-object-prototype'));
Object.prototype.forEach = noop // eslint-disable-line
done()
})
after(require('./fixtures/reset-object-prototype'))
after(function (done) {
console.log = consoleLog; // restore to original
done();
});
console.log = consoleLog // restore to original
done()
})
it('should warn if the prototype was already has a method', function (done) {
console.log = function (data) {
if (~data.indexOf('warn:')) {
done();
done()
}
};
require('../index')();
});
});
});
}
require('../index')()
})
})
})

@@ -1,14 +0,14 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var sinon = require('sinon');
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var sinon = require('sinon')
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var expect = Code.expect
var noop = require('101/noop');
var mapKeys = require('../map-keys');
var noop = require('101/noop')
var mapKeys = require('../map-keys')

@@ -18,6 +18,6 @@ describe('mapKeys', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -28,20 +28,20 @@ var obj = {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
obj.mapKeys(callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
obj.mapKeys(callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal('foo');
expect(callback.firstCall.args[1]).to.equal(1);
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal('bar');
expect(callback.secondCall.args[1]).to.equal(2);
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal('baz');
expect(callback.thirdCall.args[1]).to.equal(3);
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal('foo')
expect(callback.firstCall.args[1]).to.equal(1)
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal('bar')
expect(callback.secondCall.args[1]).to.equal(2)
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal('baz')
expect(callback.thirdCall.args[1]).to.equal(3)
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new mapped values', function (done) {

@@ -52,11 +52,10 @@ var obj = {

baz: 3
};
var thisArg = {};
var mappedObj = obj.mapKeys(prependWith('yolo'));
}
var mappedObj = obj.mapKeys(prependWith('yolo'))
Object.keys(obj).forEach(function (key) {
expect(mappedObj[key]).to.equal(obj[prependWith('yolo')(key)]);
});
done();
});
});
expect(mappedObj[key]).to.equal(obj[prependWith('yolo')(key)])
})
done()
})
})
describe('require', function () {

@@ -68,20 +67,20 @@ it('should iterate through all the key-value pairs in the object', function (done) {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
mapKeys(obj, callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
mapKeys(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal('foo');
expect(callback.firstCall.args[1]).to.equal(1);
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal('bar');
expect(callback.secondCall.args[1]).to.equal(2);
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal('baz');
expect(callback.thirdCall.args[1]).to.equal(3);
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal('foo')
expect(callback.firstCall.args[1]).to.equal(1)
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal('bar')
expect(callback.secondCall.args[1]).to.equal(2)
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal('baz')
expect(callback.thirdCall.args[1]).to.equal(3)
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new mapped values', function (done) {

@@ -92,20 +91,19 @@ var obj = {

baz: 3
};
var thisArg = {};
var mappedObj = mapKeys(obj, prependWith('yolo'));
}
var mappedObj = mapKeys(obj, prependWith('yolo'))
Object.keys(obj).forEach(function (key) {
var newKey = prependWith('yolo')(key);
expect(mappedObj[newKey]).to.equal(obj[key]);
});
done();
});
var newKey = prependWith('yolo')(key)
expect(mappedObj[newKey]).to.equal(obj[key])
})
done()
})
describe('errors', function () {
it('should throw an error if obj is not an object', function (done) {
var obj = 'notObject';
var callback = noop;
var thisArg = {};
var fn = mapKeys.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not an object/);
done();
});
var obj = 'notObject'
var callback = noop
var thisArg = {}
var fn = mapKeys.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not an object/)
done()
})
it('should throw an error if callback is not a function', function (done) {

@@ -116,10 +114,10 @@ var obj = {

baz: 3
};
var callback = 'notFunction';
var thisArg = {};
var fn = mapKeys.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not a function/);
done();
});
});
}
var callback = 'notFunction'
var thisArg = {}
var fn = mapKeys.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not a function/)
done()
})
})
describe('use w/ array', function () {

@@ -131,20 +129,20 @@ it('should iterate through all the key-value pairs in the object', function (done) {

3
];
var callback = sinon.spy();
var thisArg = {};
mapKeys(arr, callback, thisArg);
]
var callback = sinon.spy()
var thisArg = {}
mapKeys(arr, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(0); // index
expect(callback.firstCall.args[1]).to.equal(1);
expect(callback.firstCall.args[2]).to.equal(arr);
expect(callback.secondCall.args[0]).to.equal(1); // index
expect(callback.secondCall.args[1]).to.equal(2);
expect(callback.secondCall.args[2]).to.equal(arr);
expect(callback.thirdCall.args[0]).to.equal(2); // index
expect(callback.thirdCall.args[1]).to.equal(3);
expect(callback.thirdCall.args[2]).to.equal(arr);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(0) // index
expect(callback.firstCall.args[1]).to.equal(1)
expect(callback.firstCall.args[2]).to.equal(arr)
expect(callback.secondCall.args[0]).to.equal(1) // index
expect(callback.secondCall.args[1]).to.equal(2)
expect(callback.secondCall.args[2]).to.equal(arr)
expect(callback.thirdCall.args[0]).to.equal(2) // index
expect(callback.thirdCall.args[1]).to.equal(3)
expect(callback.thirdCall.args[2]).to.equal(arr)
done()
})
it('should return an object with new mapped values', function (done) {

@@ -155,26 +153,25 @@ var arr = [

3
];
var thisArg = {};
var mapped = mapKeys(arr, add(1));
expect(mapped).instanceOf(Array);
]
var mapped = mapKeys(arr, add(1))
expect(mapped).instanceOf(Array)
Object.keys(arr).forEach(function (key) {
var intKey = parseInt(key);
var newKey = add(1)(intKey);
expect(mapped[newKey]).to.equal(arr[key]);
});
done();
});
});
});
});
var intKey = parseInt(key, 10)
var newKey = add(1)(intKey)
expect(mapped[newKey]).to.equal(arr[key])
})
done()
})
})
})
})
function prependWith (pre) {
return function (key) {
return pre + key;
};
return pre + key
}
}
function add (n) {
return function (i) {
return i + n;
};
}
return i + n
}
}

@@ -1,16 +0,16 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var sinon = require('sinon');
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var sinon = require('sinon')
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var beforeEach = lab.beforeEach;
var afterEach = lab.afterEach;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var expect = Code.expect
var noop = require('101/noop');
var map = require('../map');
var noop = require('101/noop')
var map = require('../map')

@@ -20,6 +20,6 @@ describe('map', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -30,20 +30,20 @@ var obj = {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
obj.map(callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
obj.map(callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new mapped values', function (done) {

@@ -54,11 +54,10 @@ var obj = {

baz: 3
};
var thisArg = {};
var mappedObj = obj.map(multiplyBy(2));
}
var mappedObj = obj.map(multiplyBy(2))
Object.keys(obj).forEach(function (key) {
expect(mappedObj[key]).to.equal(multiplyBy(2)(obj[key]));
});
done();
});
});
expect(mappedObj[key]).to.equal(multiplyBy(2)(obj[key]))
})
done()
})
})
describe('require', function () {

@@ -70,20 +69,20 @@ it('should iterate through all the key-value pairs in the object', function (done) {

baz: 3
};
var callback = sinon.spy();
var thisArg = {};
map(obj, callback, thisArg);
}
var callback = sinon.spy()
var thisArg = {}
map(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.calledOn(thisArg)).to.equal(true);
expect(callback.firstCall.args[0]).to.equal(1);
expect(callback.firstCall.args[1]).to.equal('foo');
expect(callback.firstCall.args[2]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(2);
expect(callback.secondCall.args[1]).to.equal('bar');
expect(callback.secondCall.args[2]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(3);
expect(callback.thirdCall.args[1]).to.equal('baz');
expect(callback.thirdCall.args[2]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.calledOn(thisArg)).to.equal(true)
expect(callback.firstCall.args[0]).to.equal(1)
expect(callback.firstCall.args[1]).to.equal('foo')
expect(callback.firstCall.args[2]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(2)
expect(callback.secondCall.args[1]).to.equal('bar')
expect(callback.secondCall.args[2]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(3)
expect(callback.thirdCall.args[1]).to.equal('baz')
expect(callback.thirdCall.args[2]).to.equal(obj)
done()
})
it('should return an object with new mapped values', function (done) {

@@ -94,19 +93,18 @@ var obj = {

baz: 3
};
var thisArg = {};
var mappedObj = map(obj, multiplyBy(2));
}
var mappedObj = map(obj, multiplyBy(2))
Object.keys(obj).forEach(function (key) {
expect(mappedObj[key]).to.equal(multiplyBy(2)(obj[key]));
});
done();
});
expect(mappedObj[key]).to.equal(multiplyBy(2)(obj[key]))
})
done()
})
describe('errors', function () {
it('should throw an error if obj is not an object', function (done) {
var obj = 'notObject';
var callback = noop;
var thisArg = {};
var fn = map.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not an object/);
done();
});
var obj = 'notObject'
var callback = noop
var thisArg = {}
var fn = map.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not an object/)
done()
})
it('should throw an error if callback is not a function', function (done) {

@@ -117,35 +115,35 @@ var obj = {

baz: 3
};
var callback = 'notFunction';
var thisArg = {};
var fn = map.bind(null, obj, callback, thisArg);
expect(fn).to.throw(/not a function/);
done();
});
});
}
var callback = 'notFunction'
var thisArg = {}
var fn = map.bind(null, obj, callback, thisArg)
expect(fn).to.throw(/not a function/)
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'map');
done();
});
sinon.spy(Array.prototype, 'map')
done()
})
afterEach(function (done) {
Array.prototype.map.restore();
done();
});
Array.prototype.map.restore()
done()
})
it('should use array map', function (done) {
var arr = [1,2,3];
var callback = multiplyBy(2);
var arr = [1, 2, 3]
var callback = multiplyBy(2)
expect(map(arr, callback, arr))
.to.deep.equal(arr.map(callback, arr));
sinon.assert.calledWith(Array.prototype.map, callback, arr);
done();
});
});
});
});
.to.deep.equal(arr.map(callback, arr))
sinon.assert.calledWith(Array.prototype.map, callback, arr)
done()
})
})
})
})
function multiplyBy (multiplier) {
return function (multiplicand) {
return multiplicand * multiplier;
};
}
return multiplicand * multiplier
}
}

@@ -1,17 +0,17 @@

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var sinon = require('sinon');
var Code = require('code')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var sinon = require('sinon')
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var after = lab.after;
var beforeEach = lab.beforeEach;
var afterEach = lab.afterEach;
var expect = Code.expect;
var describe = lab.describe
var it = lab.it
var before = lab.before
var after = lab.after
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var expect = Code.expect
var noop = require('101/noop');
function sum (x, y) { return x+y; }
var reduce = require('../reduce');
var noop = require('101/noop')
function sum (x, y) { return x + y }
var reduce = require('../reduce')

@@ -21,6 +21,6 @@ describe('reduce', function () {

before(function (done) {
require('../index')();
done();
});
after(require('./fixtures/reset-object-prototype'));
require('../index')()
done()
})
after(require('./fixtures/reset-object-prototype'))
it('should iterate through all the key-value pairs in the object', function (done) {

@@ -31,22 +31,22 @@ var obj = {

baz: 3
};
var callback = sinon.spy();
var initialValue = 100;
obj.reduce(callback, initialValue);
}
var callback = sinon.spy()
var initialValue = 100
obj.reduce(callback, initialValue)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.firstCall.args[0]).to.equal(initialValue);
expect(callback.firstCall.args[1]).to.equal(1);
expect(callback.firstCall.args[2]).to.equal('foo');
expect(callback.firstCall.args[3]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(undefined);
expect(callback.secondCall.args[1]).to.equal(2);
expect(callback.secondCall.args[2]).to.equal('bar');
expect(callback.secondCall.args[3]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(undefined);
expect(callback.thirdCall.args[1]).to.equal(3);
expect(callback.thirdCall.args[2]).to.equal('baz');
expect(callback.thirdCall.args[3]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.firstCall.args[0]).to.equal(initialValue)
expect(callback.firstCall.args[1]).to.equal(1)
expect(callback.firstCall.args[2]).to.equal('foo')
expect(callback.firstCall.args[3]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(undefined)
expect(callback.secondCall.args[1]).to.equal(2)
expect(callback.secondCall.args[2]).to.equal('bar')
expect(callback.secondCall.args[3]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(undefined)
expect(callback.thirdCall.args[1]).to.equal(3)
expect(callback.thirdCall.args[2]).to.equal('baz')
expect(callback.thirdCall.args[3]).to.equal(obj)
done()
})
it('should return an object with new reduced values', function (done) {

@@ -57,9 +57,8 @@ var obj = {

baz: 3
};
var thisArg = {};
var finalValue = obj.reduce(sum);
expect(finalValue).to.equal(1+2+3);
done();
});
});
}
var finalValue = obj.reduce(sum)
expect(finalValue).to.equal(1 + 2 + 3)
done()
})
})
describe('require', function () {

@@ -71,22 +70,22 @@ it('should iterate through all the key-value pairs in the object', function (done) {

baz: 3
};
var callback = sinon.spy();
var initialValue = 100;
reduce(obj, callback, initialValue);
}
var callback = sinon.spy()
var initialValue = 100
reduce(obj, callback, initialValue)
// assertions
expect(callback.callCount).to.equal(3);
expect(callback.firstCall.args[0]).to.equal(100);
expect(callback.firstCall.args[1]).to.equal(1);
expect(callback.firstCall.args[2]).to.equal('foo');
expect(callback.firstCall.args[3]).to.equal(obj);
expect(callback.secondCall.args[0]).to.equal(undefined);
expect(callback.secondCall.args[1]).to.equal(2);
expect(callback.secondCall.args[2]).to.equal('bar');
expect(callback.secondCall.args[3]).to.equal(obj);
expect(callback.thirdCall.args[0]).to.equal(undefined);
expect(callback.thirdCall.args[1]).to.equal(3);
expect(callback.thirdCall.args[2]).to.equal('baz');
expect(callback.thirdCall.args[3]).to.equal(obj);
done();
});
expect(callback.callCount).to.equal(3)
expect(callback.firstCall.args[0]).to.equal(100)
expect(callback.firstCall.args[1]).to.equal(1)
expect(callback.firstCall.args[2]).to.equal('foo')
expect(callback.firstCall.args[3]).to.equal(obj)
expect(callback.secondCall.args[0]).to.equal(undefined)
expect(callback.secondCall.args[1]).to.equal(2)
expect(callback.secondCall.args[2]).to.equal('bar')
expect(callback.secondCall.args[3]).to.equal(obj)
expect(callback.thirdCall.args[0]).to.equal(undefined)
expect(callback.thirdCall.args[1]).to.equal(3)
expect(callback.thirdCall.args[2]).to.equal('baz')
expect(callback.thirdCall.args[3]).to.equal(obj)
done()
})
it('should return an object with new reduced values', function (done) {

@@ -97,7 +96,7 @@ var obj = {

baz: 3
};
var finalValue = reduce(obj, sum);
expect(finalValue).to.equal(1+2+3);
done();
});
}
var finalValue = reduce(obj, sum)
expect(finalValue).to.equal(1 + 2 + 3)
done()
})
describe('object with only one value, and no initialValue provided', function () {

@@ -107,17 +106,17 @@ it('should return the first value', function (done) {

foo: 100
};
var callback = sinon.spy();
var finalValue = reduce(obj, callback);
expect(finalValue).to.equal(100);
done();
});
});
}
var callback = sinon.spy()
var finalValue = reduce(obj, callback)
expect(finalValue).to.equal(100)
done()
})
})
describe('errors', function () {
it('should throw an error if obj is not an object', function (done) {
var obj = 'notObject';
var callback = noop;
var fn = reduce.bind(null, obj, callback);
expect(fn).to.throw(/not an object/);
done();
});
var obj = 'notObject'
var callback = noop
var fn = reduce.bind(null, obj, callback)
expect(fn).to.throw(/not an object/)
done()
})
it('should throw an error if callback is not a function', function (done) {

@@ -128,43 +127,42 @@ var obj = {

baz: 3
};
var callback = 'notFunction';
var fn = reduce.bind(null, obj, callback);
expect(fn).to.throw(/not a function/);
done();
});
}
var callback = 'notFunction'
var fn = reduce.bind(null, obj, callback)
expect(fn).to.throw(/not a function/)
done()
})
it('should throw an error if the object is empty and no initialValue provided', function (done) {
var obj = {};
var callback = noop;
var fn = reduce.bind(null, obj, noop);
expect(fn).to.throw(/empty object/);
done();
});
});
var obj = {}
var fn = reduce.bind(null, obj, noop)
expect(fn).to.throw(/empty object/)
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'reduce');
done();
});
sinon.spy(Array.prototype, 'reduce')
done()
})
afterEach(function (done) {
Array.prototype.reduce.restore();
done();
});
Array.prototype.reduce.restore()
done()
})
it('should use array reduce', function (done) {
var arr = [1,2,3];
var callback = sum;
var arr = [1, 2, 3]
var callback = sum
expect(reduce(arr, callback, 20))
.to.equal(arr.reduce(callback, 20));
sinon.assert.calledWith(Array.prototype.reduce, callback, 20);
done();
});
.to.equal(arr.reduce(callback, 20))
sinon.assert.calledWith(Array.prototype.reduce, callback, 20)
done()
})
it('should use array reduce w/out initialValue', function (done) {
var arr = [1,2,3];
var callback = sum;
var arr = [1, 2, 3]
var callback = sum
expect(reduce(arr, callback))
.to.equal(arr.reduce(callback));
sinon.assert.calledWith(Array.prototype.reduce, callback);
done();
});
});
});
});
.to.equal(arr.reduce(callback))
sinon.assert.calledWith(Array.prototype.reduce, callback)
done()
})
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc