pick.js 853 B

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