UNPKG

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