-
Notifications
You must be signed in to change notification settings - Fork 6
/
map.js
36 lines (34 loc) · 1.25 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @module object-loops/map
*/
var forEach = require('./for-each')
/**
* Creates a new object with the results of calling a provided function on every value 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 {*} [thisArg] - optional. context to bind to callback
* @returns {object} newly created object with mapped values
*/
module.exports = map
function map (obj, callback, thisArg) {
if (Array.isArray(obj)) {
return obj.map(callback, thisArg)
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' must be a function')
}
var mappedObj = {}
forEach(obj, function (val, key, obj) {
mappedObj[key] = callback.call(thisArg, val, key, obj)
})
return mappedObj
}
/**
* This callback type is called `mapCallback` and is displayed as a global symbol.
* @callback mapCallback
* @param {*} val - value for key
* @param {string} key - object key (used in current iteration)
* @param {object} obj - object which values are being iterated
* @returns {*} mappedValue - value for key in the new, mapped object
*/