time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

omit.js (1629B)


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