0
0

_iterate.js 1016 B

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