_iterate.js
 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 call       = Function.prototype.call
 8    , keys       = Object.keys
 9    , isCallable = require('./is-callable')
10    , callable   = require('./valid-callable')
11    , value      = require('./valid-value');
12  
13  module.exports = function (method) {
14  	return function (obj, cb) {
15  		var list, thisArg = arguments[2], compareFn = arguments[3];
16  		value(obj);
17  		callable(cb);
18  
19  		list = keys(obj);
20  		if (compareFn) {
21  			list.sort(isCallable(compareFn) ? compareFn : undefined);
22  		}
23  		return list[method](function (key, index) {
24  			return call.call(cb, thisArg, obj[key], key, obj, index);
25  		});
26  	};
27  };