]> git.r.bdr.sh - rbdr/dotfiles/blob
bf2c55d08601d3fe707beded8005a7dd08d66fad
[rbdr/dotfiles] /
1 // Internal method, used by iteration functions.
2 // Calls a function for each key-value pair found in object
3 // Optionally takes compareFn to iterate object in specific order
4
5 'use strict';
6
7 var isCallable = require('./is-callable')
8 , callable = require('./valid-callable')
9 , value = require('./valid-value')
10
11 , call = Function.prototype.call, keys = Object.keys
12 , propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
13
14 module.exports = function (method, defVal) {
15 return function (obj, cb/*, thisArg, compareFn*/) {
16 var list, thisArg = arguments[2], compareFn = arguments[3];
17 obj = Object(value(obj));
18 callable(cb);
19
20 list = keys(obj);
21 if (compareFn) {
22 list.sort(isCallable(compareFn) ? compareFn.bind(obj) : undefined);
23 }
24 return list[method](function (key, index) {
25 if (!propertyIsEnumerable.call(obj, key)) return defVal;
26 return call.call(cb, thisArg, obj[key], key, obj, index);
27 });
28 };
29 };