omit.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var arrayMap = require('../internal/arrayMap'),
  2. baseDifference = require('../internal/baseDifference'),
  3. baseFlatten = require('../internal/baseFlatten'),
  4. bindCallback = require('../internal/bindCallback'),
  5. keysIn = require('./keysIn'),
  6. pickByArray = require('../internal/pickByArray'),
  7. pickByCallback = require('../internal/pickByCallback'),
  8. restParam = require('../function/restParam');
  9. /**
  10. * The opposite of `_.pick`; this method creates an object composed of the
  11. * own and inherited enumerable properties of `object` that are not omitted.
  12. *
  13. * @static
  14. * @memberOf _
  15. * @category Object
  16. * @param {Object} object The source object.
  17. * @param {Function|...(string|string[])} [predicate] The function invoked per
  18. * iteration or property names to omit, specified as individual property
  19. * names or arrays of property names.
  20. * @param {*} [thisArg] The `this` binding of `predicate`.
  21. * @returns {Object} Returns the new object.
  22. * @example
  23. *
  24. * var object = { 'user': 'fred', 'age': 40 };
  25. *
  26. * _.omit(object, 'age');
  27. * // => { 'user': 'fred' }
  28. *
  29. * _.omit(object, _.isNumber);
  30. * // => { 'user': 'fred' }
  31. */
  32. var omit = restParam(function(object, props) {
  33. if (object == null) {
  34. return {};
  35. }
  36. if (typeof props[0] != 'function') {
  37. var props = arrayMap(baseFlatten(props), String);
  38. return pickByArray(object, baseDifference(keysIn(object), props));
  39. }
  40. var predicate = bindCallback(props[0], props[1], 3);
  41. return pickByCallback(object, function(value, key, object) {
  42. return !predicate(value, key, object);
  43. });
  44. });
  45. module.exports = omit;