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
7 var isCallable = require('./is-callable')
8 , callable = require('./valid-callable')
9 , value = require('./valid-value')
11 , call = Function.prototype.call, keys = Object.keys
12 , propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
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));
22 list.sort(isCallable(compareFn) ? compareFn.bind(obj) : undefined);
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);