Skip to content

Commit

Permalink
add support for index
Browse files Browse the repository at this point in the history
  • Loading branch information
zspecza committed Jun 14, 2016
1 parent 3b782c5 commit f6cdf9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ forEach(obj, console.log.bind(console))
// logs out all the values
```

Each callback has a method signature of `(value, key, object)` with the exception of `reduce`, which has `(accumulator, value, key, object)`.
Each callback has a method signature of `(value, key, index, object)` with the exception of `reduce`, which has `(accumulator, value, key, index, object)`. `value` is the current key's value, `key` is the current key's name, `index` is the 0-based index of the current key and `object` is the original object.

**Note:** Unlike the native array equivalent as well as other library implementations, we felt it would be better to explicitly require the passing of an accumulator to the `reduce` method.

Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
/**
* Iterates over an object's keys, applying a transform function to each value.
* @param {Object} obj - the initial object
* @param {Function} transform - the transform function. Receives `(value, key, object)` as arguments.
* @param {Function} transform - the transform function. Receives `(value, key, index, object)` as arguments.
* @return {Object} - the new object
*/
exports.map = function map (obj, transform) {
let res = {}
let index = 0
for (let [val, key] of entries(obj)) {
res[key] = transform(val, key, obj)
res[key] = transform(val, key, index++, obj)
}
return res
}

/**
* Iterates over an object's keys, applying a predicate function that determines whether the current key will appear in the returned object.
* @param {Object} obj - the initial object
* @param {Function} predicate - the predicate function. Receives `(value, key, object)` as arguments. Should return `true` or `false`.
* @param {Function} predicate - the predicate function. Receives `(value, key, index, object)` as arguments. Should return `true` or `false`.
* @return {Object} - the new object
*/
exports.filter = function filter (obj, predicate) {
let res = {}
let index = 0
for (let [val, key] of entries(obj)) {
if (predicate(val, key, obj)) {
if (predicate(val, key, index++, obj)) {
res[key] = val
}
}
Expand All @@ -33,24 +35,26 @@ exports.filter = function filter (obj, predicate) {
/**
* Iterates over an object's keys, calling an iterator function on each pass.
* @param {Object} obj - the initial object
* @param {Function} iterate - the iterator function. Recives `(value, key, object)` as arguments.
* @param {Function} iterate - the iterator function. Recives `(value, key, index, object)` as arguments.
*/
exports.forEach = function forEach (obj, iterate) {
let index = 0
for (let [val, key] of entries(obj)) {
iterate(val, key, obj)
iterate(val, key, index++, obj)
}
}

/**
* Iterates over an object's keys, applying a reducer function on each pass that reduces the current parameters into a single value.
* @param {Object} obj - the initial object.
* @param {Function} reducer - the reducer function. Receives `(accumulator, value, key, object)` as arguments.
* @param {Function} reducer - the reducer function. Receives `(accumulator, value, key, index, object)` as arguments.
* @param {*} accumulator - a value to accumulate results into.
* @return {*} - the combined accumulated value.
*/
exports.reduce = function reduce (obj, reducer, accumulator) {
let index = 0
for (let [val, key] of entries(obj)) {
accumulator = reducer(accumulator, val, key, obj)
accumulator = reducer(accumulator, val, key, index++, obj)
}
return accumulator
}
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const test = require('ava')
const {map, reduce, filter, forEach} = require('..')
const obj = { foo: 'bar', doge: 'wow' }
Expand Down

0 comments on commit f6cdf9e

Please sign in to comment.