object-loops
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -19,4 +19,6 @@ /** | ||
'every', | ||
'filter', | ||
'find', | ||
'findKey', | ||
'forEach', | ||
'filter', | ||
'map', | ||
@@ -23,0 +25,0 @@ 'mapKeys', |
{ | ||
"name": "object-loops", | ||
"version": "0.5.2", | ||
"description": "Functional methods like forEach, map, filter, and other ES5 Array methods for Objects in javascript", | ||
"version": "0.6.0", | ||
"description": "Functional methods like forEach, map, filter, and other Array methods for Objects in javascript", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "directories": { |
174
README.md
object-loops [![Build Status](https://travis-ci.org/tjmehta/object-loops.svg)](https://travis-ci.org/tjmehta/object-loops) | ||
============ | ||
Functional methods like forEach, map, filter, and other ES5 Array methods for Objects in javascript | ||
Functional methods like forEach, map, filter, and other Array methods for Objects in javascript | ||
@@ -10,2 +10,14 @@ # Installation | ||
# Index | ||
[chain](https://github.com/tjmehta/object-loops#usage) | ||
[every](https://github.com/tjmehta/object-loops#every) | ||
[find](https://github.com/tjmehta/object-loops#find) | ||
[findKey](https://github.com/tjmehta/object-loops#findKey) | ||
[filter](https://github.com/tjmehta/object-loops#filter) | ||
[forEach](https://github.com/tjmehta/object-loops#forEach) | ||
[map](https://github.com/tjmehta/object-loops#map) | ||
[reduce](https://github.com/tjmehta/object-loops#reduce) | ||
[some](https://github.com/tjmehta/object-loops#some) | ||
# Usage | ||
@@ -21,2 +33,3 @@ | ||
var reduce = require('object-loops/reduce') | ||
//... and more | ||
// usage | ||
@@ -54,13 +67,14 @@ forEach({ x:10, y: 20 }, callback) | ||
## every | ||
## forEach | ||
Tests whether every value in the object passes the test implemented by the callback. | ||
Executes a provided function once per each object value. | ||
* @param {object} [obj] - object to forEach, not accepted if being used directly on Object.prototype | ||
* @param {function} callback - function that will be invoked once for each key-value pair | ||
* @function module:object-loops/every | ||
* @param {object} [obj] - object to iterate through, not accepted if being used directly on Object.prototype | ||
* @param {everyCallback} callback - function to test each value in the object. return falsey to end the loop, truthy otherwise. | ||
* @param {*} [thisArg] - optional. context to bind to callback | ||
* @returns {boolean} if callback returns false, the loop is ended and false is returned (else false) | ||
```js | ||
var forEach = require('object-loops/for-each') | ||
var every = require('object-loops/every') | ||
@@ -70,25 +84,23 @@ var obj = { | ||
bar: 20, | ||
baz: 30 | ||
baz: 30, | ||
qux: 40, | ||
} | ||
var keyConcat = '' | ||
var valSum = 0 | ||
forEach(obj, function (val, key, obj) { | ||
keyConcat += key | ||
valSum += val | ||
var allGreaterThan25 = every(obj, function (val, key, obj) { | ||
return val > 25 | ||
}) | ||
keyConcat // = 'foobarbaz' | ||
valSum // = 60 | ||
allGreaterThan25 // false | ||
*/ | ||
``` | ||
## map | ||
## find | ||
Creates a new object with the results of calling a provided function on every value in the object. | ||
Find the value of the the object that passes the test implemented by the callback. | ||
* @param {object} [obj] - object to map values, not accepted if being used directly on Object.prototype | ||
* @param {function} 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 | ||
* @param {object} [obj] - object to iterate through, not accepted if being used directly on Object.prototype | ||
* @param {findCallback} callback - function to test each value in the object. return truthy to end the loop and return index, falsey otherwise. | ||
* @param {*} [thisArg] - optional. context to bind to callback | ||
* @returns {*} if callback returns true, the loop is ended and the passing `val` is returned (else undefined) | ||
```js | ||
var map = require('object-loops/map') | ||
var find = require('object-loops/find') | ||
@@ -98,13 +110,42 @@ var obj = { | ||
bar: 20, | ||
baz: 30 | ||
baz: 30, | ||
qux: 40, | ||
} | ||
var mappedObj = map(obj, function (val, key, obj) { | ||
return val*2 | ||
var key = find(obj, function (val, key, obj) { | ||
return val > 25 | ||
}) | ||
mappedObj /* Each val multiplied by 2 | ||
{ | ||
foo: 20, | ||
bar: 40, | ||
baz: 60 | ||
key // 30 | ||
var notfound = find(obj, function (val, key, obj) { | ||
return val > 100 | ||
}) | ||
notfound // undefined | ||
*/ | ||
``` | ||
## findKey | ||
Find the key of the the object that passes the test implemented by the callback. Very similar to `Array.prototype.findIndex` | ||
* @param {object} [obj] - object to iterate through, not accepted if being used directly on Object.prototype | ||
* @param {findKeyCallback} callback - function to test each value in the object. return truthy to end the loop and return index, falsey otherwise. | ||
* @param {*} [thisArg] - optional. context to bind to callback | ||
* @returns {*} if callback returns true, the loop is ended and the passing `key` is returned (else undefined) | ||
```js | ||
var findKey = require('object-loops/find-key') | ||
var obj = { | ||
foo: 10, | ||
bar: 20, | ||
baz: 30, | ||
qux: 40, | ||
} | ||
var key = findKey(obj, function (val, key, obj) { | ||
return val > 25 | ||
}) | ||
key // 'baz' | ||
var notfound = findKey(obj, function (val, key, obj) { | ||
return val > 100 | ||
}) | ||
notfound // undefined | ||
*/ | ||
@@ -142,13 +183,12 @@ ``` | ||
## reduce | ||
## forEach | ||
Applies a function against an accumulator and each value of the object to reduce it to a single value. | ||
Executes a provided function once per each object value. | ||
* @param {object} [obj] - object to reduce values, not accepted if being used directly on Object.prototype | ||
* @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise. | ||
* @param {*} [initialValue] - optional. object to use as the first argument to the first call of the callback | ||
* @returns {*} finalValue - final value returned by reduction, or just first val if only one exists. | ||
* @param {object} [obj] - object to forEach, not accepted if being used directly on Object.prototype | ||
* @param {function} callback - function that will be invoked once for each key-value pair | ||
* @param {*} [thisArg] - optional. context to bind to callback | ||
```js | ||
var reduce = require('object-loops/reduce') | ||
var forEach = require('object-loops/for-each') | ||
@@ -160,20 +200,23 @@ var obj = { | ||
} | ||
var sum = reduce(obj, function (prevVal, val, key, obj) { | ||
return prevVal + val | ||
var keyConcat = '' | ||
var valSum = 0 | ||
forEach(obj, function (val, key, obj) { | ||
keyConcat += key | ||
valSum += val | ||
}) | ||
sum // 60 | ||
keyConcat // = 'foobarbaz' | ||
valSum // = 60 | ||
``` | ||
## every | ||
## map | ||
Tests whether every value in the object passes the test implemented by the callback. | ||
Creates a new object with the results of calling a provided function on every value in the object. | ||
* @function module:object-loops/every | ||
* @param {object} [obj] - object to iterate through, not accepted if being used directly on Object.prototype | ||
* @param {everyCallback} callback - function to test each value in the object. return falsey to end the loop, truthy otherwise. | ||
* @param {object} [obj] - object to map values, not accepted if being used directly on Object.prototype | ||
* @param {function} callback - function that produces the new value for the new, mapped object | ||
* @param {*} [thisArg] - optional. context to bind to callback | ||
* @returns {boolean} if callback returns false, the loop is ended and false is returned (else false) | ||
* @returns {object} newly created object with mapped values | ||
```js | ||
var every = require('object-loops/every') | ||
var map = require('object-loops/map') | ||
@@ -183,12 +226,39 @@ var obj = { | ||
bar: 20, | ||
baz: 30, | ||
qux: 40, | ||
baz: 30 | ||
} | ||
var allGreaterThan25 = every(obj, function (val, key, obj) { | ||
return val > 25 | ||
var mappedObj = map(obj, function (val, key, obj) { | ||
return val*2 | ||
}) | ||
allGreaterThan25 // false | ||
mappedObj /* Each val multiplied by 2 | ||
{ | ||
foo: 20, | ||
bar: 40, | ||
baz: 60 | ||
} | ||
*/ | ||
``` | ||
## reduce | ||
Applies a function against an accumulator and each value of the object to reduce it to a single value. | ||
* @param {object} [obj] - object to reduce values, not accepted if being used directly on Object.prototype | ||
* @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise. | ||
* @param {*} [initialValue] - optional. object to use as the first argument to the first call of the callback | ||
* @returns {*} finalValue - final value returned by reduction, or just first val if only one exists. | ||
```js | ||
var reduce = require('object-loops/reduce') | ||
var obj = { | ||
foo: 10, | ||
bar: 20, | ||
baz: 30 | ||
} | ||
var sum = reduce(obj, function (prevVal, val, key, obj) { | ||
return prevVal + val | ||
}) | ||
sum // 60 | ||
``` | ||
## some | ||
@@ -195,0 +265,0 @@ |
var methodNames = [ | ||
'every', | ||
'filter', | ||
'find', | ||
'findKey', | ||
'forEach', | ||
'filter', | ||
'map', | ||
@@ -6,0 +8,0 @@ 'mapKeys', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76584
28
1949
285