暫無描述

pick.js 899B

12345678910111213141516171819202122232425262728
  1. var restArguments = require('./restArguments.js');
  2. var isFunction = require('./isFunction.js');
  3. var _optimizeCb = require('./_optimizeCb.js');
  4. var allKeys = require('./allKeys.js');
  5. var _keyInObj = require('./_keyInObj.js');
  6. var _flatten = require('./_flatten.js');
  7. // Return a copy of the object only containing the allowed properties.
  8. var pick = restArguments(function(obj, keys) {
  9. var result = {}, iteratee = keys[0];
  10. if (obj == null) return result;
  11. if (isFunction(iteratee)) {
  12. if (keys.length > 1) iteratee = _optimizeCb(iteratee, keys[1]);
  13. keys = allKeys(obj);
  14. } else {
  15. iteratee = _keyInObj;
  16. keys = _flatten(keys, false, false);
  17. obj = Object(obj);
  18. }
  19. for (var i = 0, length = keys.length; i < length; i++) {
  20. var key = keys[i];
  21. var value = obj[key];
  22. if (iteratee(value, key, obj)) result[key] = value;
  23. }
  24. return result;
  25. });
  26. module.exports = pick;