Add _.drop, unit tests, and cleanup documentation for _.extend, _.defaults, and _.pick.

Former-commit-id: a45b0c45d52fdbe5f71984412d631f3dfe87965b
This commit is contained in:
John-David Dalton
2012-07-18 03:55:16 -04:00
parent a96d14566f
commit 9836b274b9
4 changed files with 92 additions and 18 deletions

View File

@@ -155,6 +155,7 @@
'defer': [], 'defer': [],
'delay': [], 'delay': [],
'difference': ['indexOf'], 'difference': ['indexOf'],
'drop': ['indexOf'],
'escape': [], 'escape': [],
'every': ['identity'], 'every': ['identity'],
'extend': [], 'extend': [],

View File

@@ -10,17 +10,20 @@
'accumulator', 'accumulator',
'args', 'args',
'arrayClass', 'arrayClass',
'ArrayProto',
'bind', 'bind',
'callback', 'callback',
'className', 'className',
'collection', 'collection',
'compareAscending', 'compareAscending',
'concat',
'ctor', 'ctor',
'funcClass', 'funcClass',
'funcs', 'funcs',
'hasOwnProperty', 'hasOwnProperty',
'identity', 'identity',
'index', 'index',
'indexOf',
'isFunc', 'isFunc',
'iteratee', 'iteratee',
'iterateeIndex', 'iterateeIndex',
@@ -97,6 +100,7 @@
'delay', 'delay',
'detect', 'detect',
'difference', 'difference',
'drop',
'each', 'each',
'environment', 'environment',
'escape', 'escape',

View File

@@ -575,16 +575,16 @@
} }
// create the function factory // create the function factory
var factory = Function( var factory = Function(
'arrayClass, bind, compareAscending, funcClass, hasOwnProperty, identity, ' + 'arrayClass, ArrayProto, bind, compareAscending, concat, funcClass, ' +
'iteratorBind, objectTypes, nativeKeys, propertyIsEnumerable, slice, ' + 'hasOwnProperty, identity, indexOf, iteratorBind, objectTypes, nativeKeys, ' +
'stringClass, toString', 'propertyIsEnumerable, slice, stringClass, toString',
'return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}' 'return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}'
); );
// return the compiled function // return the compiled function
return factory( return factory(
arrayClass, bind, compareAscending, funcClass, hasOwnProperty, identity, arrayClass, ArrayProto, bind, compareAscending, concat, funcClass,
iteratorBind, objectTypes, nativeKeys, propertyIsEnumerable, slice, hasOwnProperty, identity, indexOf, iteratorBind, objectTypes, nativeKeys,
stringClass, toString propertyIsEnumerable, slice, stringClass, toString
); );
} }
@@ -2449,16 +2449,17 @@
} }
/** /**
* Assigns missing properties on `object` with default values from the defaults * Assigns enumerable properties of the default object(s) to the `destination`
* objects. Once a property is set, additional defaults of the same property * object for all `destination` properties that resolve to `null`/`undefined`.
* will be ignored. * Once a property is set, additional defaults of the same property will be
* ignored.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The object to populate. * @param {Object} object The destination object.
* @param {Object} [defaults1, defaults2, ...] The defaults objects to apply to `object`. * @param {Object} [default1, default2, ...] The default objects.
* @returns {Object} Returns `object`. * @returns {Object} Returns the `object`.
* @example * @example
* *
* var iceCream = { 'flavor': 'chocolate' }; * var iceCream = { 'flavor': 'chocolate' };
@@ -2470,15 +2471,40 @@
}); });
/** /**
* Copies enumerable properties from the source objects to the `destination` object. * Creates a shallow clone of `object` excluding the specified properties.
* Subsequent sources will overwrite propery assignments of previous sources. * Property names may be specified as individual arguments or as arrays of
* property names.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The source object.
* @param {Object} [prop1, prop2, ...] The properties to drop.
* @returns {Object} Returns an object without the dropped properties.
* @example
*
* _.drop({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
* // => { 'name': 'moe', 'age': 40 }
*/
var drop = createIterator({
'useHas': false,
'args': 'object',
'init': '{}',
'top': 'var props = concat.apply(ArrayProto, arguments)',
'inLoop': 'if (indexOf(props, index) < 0) result[index] = iteratee[index]'
});
/**
* Assigns enumerable properties of the source object(s) to the `destination`
* object. Subsequent sources will overwrite propery assignments of previous
* sources.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The destination object. * @param {Object} object The destination object.
* @param {Object} [source1, source2, ...] The source objects. * @param {Object} [source1, source2, ...] The source objects.
* @returns {Object} Returns the destination object. * @returns {Object} Returns the `object`.
* @example * @example
* *
* _.extend({ 'name': 'moe' }, { 'age': 40 }); * _.extend({ 'name': 'moe' }, { 'age': 40 });
@@ -3080,13 +3106,14 @@
}; };
/** /**
* Creates an object composed of the specified properties. Property names may * Creates a shallow clone of `object` composed of the specified properties.
* be specified as individual arguments or as arrays of property names. * Property names may be specified as individual arguments or as arrays of
* property names.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The object to pluck. * @param {Object} object The source object.
* @param {Object} [prop1, prop2, ...] The properties to pick. * @param {Object} [prop1, prop2, ...] The properties to pick.
* @returns {Object} Returns an object composed of the picked properties. * @returns {Object} Returns an object composed of the picked properties.
* @example * @example
@@ -3632,6 +3659,7 @@
lodash.defer = defer; lodash.defer = defer;
lodash.delay = delay; lodash.delay = delay;
lodash.difference = difference; lodash.difference = difference;
lodash.drop = drop;
lodash.escape = escape; lodash.escape = escape;
lodash.every = every; lodash.every = every;
lodash.extend = extend; lodash.extend = extend;

View File

@@ -201,6 +201,34 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.drop');
(function() {
var object = { 'a': 1, 'b': 2, 'c': 3 },
actual = { 'b': 2 };
test('should accept individual property names', function() {
deepEqual(_.drop(object, 'a', 'c'), actual);
});
test('should accept an array of property names', function() {
deepEqual(_.drop(object, ['a', 'c']), actual);
});
test('should accept mixes of individual and arrays of property names', function() {
deepEqual(_.drop(object, ['a'], 'c'), actual);
});
test('should iterate over inherited properties', function() {
function Foo() {}
Foo.prototype = object;
deepEqual(_.drop(new Foo, 'a', 'c'), actual);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.escape'); QUnit.module('lodash.escape');
(function() { (function() {
@@ -642,6 +670,19 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.pick');
(function() {
test('should iterate over inherited properties', function() {
function Foo() {}
Foo.prototype = { 'a': 1, 'b': 2, 'c': 3 };
deepEqual(_.pick(new Foo, 'b'), { 'b': 2 });
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.pluck'); QUnit.module('lodash.pluck');
(function() { (function() {