_iterate.js 1.1 KB

123456789101112131415161718192021222324252627282930
  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. "use strict";
  5. var callable = require("./valid-callable")
  6. , value = require("./valid-value")
  7. , bind = Function.prototype.bind
  8. , call = Function.prototype.call
  9. , keys = Object.keys
  10. , objPropertyIsEnumerable = Object.prototype.propertyIsEnumerable;
  11. module.exports = function (method, defVal) {
  12. return function (obj, cb /*, thisArg, compareFn*/) {
  13. var list, thisArg = arguments[2], compareFn = arguments[3];
  14. obj = Object(value(obj));
  15. callable(cb);
  16. list = keys(obj);
  17. if (compareFn) {
  18. list.sort(typeof compareFn === "function" ? bind.call(compareFn, obj) : undefined);
  19. }
  20. if (typeof method !== "function") method = list[method];
  21. return call.call(method, list, function (key, index) {
  22. if (!objPropertyIsEnumerable.call(obj, key)) return defVal;
  23. return call.call(cb, thisArg, obj[key], key, obj, index);
  24. });
  25. };
  26. };