omit.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var arrayMap = require('./_arrayMap'),
  2. baseClone = require('./_baseClone'),
  3. baseUnset = require('./_baseUnset'),
  4. castPath = require('./_castPath'),
  5. copyObject = require('./_copyObject'),
  6. customOmitClone = require('./_customOmitClone'),
  7. flatRest = require('./_flatRest'),
  8. getAllKeysIn = require('./_getAllKeysIn');
  9. /** Used to compose bitmasks for cloning. */
  10. var CLONE_DEEP_FLAG = 1,
  11. CLONE_FLAT_FLAG = 2,
  12. CLONE_SYMBOLS_FLAG = 4;
  13. /**
  14. * The opposite of `_.pick`; this method creates an object composed of the
  15. * own and inherited enumerable property paths of `object` that are not omitted.
  16. *
  17. * **Note:** This method is considerably slower than `_.pick`.
  18. *
  19. * @static
  20. * @since 0.1.0
  21. * @memberOf _
  22. * @category Object
  23. * @param {Object} object The source object.
  24. * @param {...(string|string[])} [paths] The property paths to omit.
  25. * @returns {Object} Returns the new object.
  26. * @example
  27. *
  28. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  29. *
  30. * _.omit(object, ['a', 'c']);
  31. * // => { 'b': '2' }
  32. */
  33. var omit = flatRest(function(object, paths) {
  34. var result = {};
  35. if (object == null) {
  36. return result;
  37. }
  38. var isDeep = false;
  39. paths = arrayMap(paths, function(path) {
  40. path = castPath(path, object);
  41. isDeep || (isDeep = path.length > 1);
  42. return path;
  43. });
  44. copyObject(object, getAllKeysIn(object), result);
  45. if (isDeep) {
  46. result = baseClone(result, CLONE_DEEP_FLAG | CLONE_FLAT_FLAG | CLONE_SYMBOLS_FLAG, customOmitClone);
  47. }
  48. var length = paths.length;
  49. while (length--) {
  50. baseUnset(result, paths[length]);
  51. }
  52. return result;
  53. });
  54. module.exports = omit;