pick.js 855 B

1234567891011121314151617181920212223242526
  1. import restArguments from './restArguments.js';
  2. import isFunction from './isFunction.js';
  3. import optimizeCb from './_optimizeCb.js';
  4. import allKeys from './allKeys.js';
  5. import keyInObj from './_keyInObj.js';
  6. import flatten from './_flatten.js';
  7. // Return a copy of the object only containing the allowed properties.
  8. export default 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. });