mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
adding an initial implementation of a 'pick' function. #523
This commit is contained in:
13
index.html
13
index.html
@@ -214,6 +214,7 @@
|
|||||||
<li>- <a href="#values">values</a></li>
|
<li>- <a href="#values">values</a></li>
|
||||||
<li>- <a href="#object-functions">functions</a></li>
|
<li>- <a href="#object-functions">functions</a></li>
|
||||||
<li>- <a href="#extend">extend</a></li>
|
<li>- <a href="#extend">extend</a></li>
|
||||||
|
<li>- <a href="#pick">pick</a></li>
|
||||||
<li>- <a href="#defaults">defaults</a></li>
|
<li>- <a href="#defaults">defaults</a></li>
|
||||||
<li>- <a href="#clone">clone</a></li>
|
<li>- <a href="#clone">clone</a></li>
|
||||||
<li>- <a href="#tap">tap</a></li>
|
<li>- <a href="#tap">tap</a></li>
|
||||||
@@ -1016,16 +1017,14 @@ _.extend({name : 'moe'}, {age : 50});
|
|||||||
=> {name : 'moe', age : 50}
|
=> {name : 'moe', age : 50}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p id="restrict">
|
<p id="pick">
|
||||||
<b class="header">restrict</b><code>_.restrict(source, *keys)</code>
|
<b class="header">pick</b><code>_.pick(object, *keys)</code>
|
||||||
<br />
|
<br />
|
||||||
Return a clone of the <b>source</b> with only the properties with the
|
Return a copy of the <b>object</b>, filtered to only have values for
|
||||||
property names, or arrays of property names, provided in <b>keys</b>.
|
the whitelisted <b>keys</b> (or array of valid keys).
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
_.restrict({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
|
_.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
|
||||||
=> {name : 'moe', age : 50}
|
|
||||||
_.restrict({name : 'moe', age: 50, userid : 'moe1'}, ['name', 'age']);
|
|
||||||
=> {name : 'moe', age : 50}
|
=> {name : 'moe', age : 50}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|||||||
@@ -41,13 +41,13 @@ $(document).ready(function() {
|
|||||||
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
|
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("objects: restrict", function() {
|
test("objects: pick", function() {
|
||||||
var result;
|
var result;
|
||||||
result = _.restrict({a:1, b:2, c:3}, 'a', 'c');
|
result = _.pick({a:1, b:2, c:3}, 'a', 'c');
|
||||||
ok(_.isEqual(result, {a:1, c:3}), 'can restrict properties to those named');
|
ok(_.isEqual(result, {a:1, c:3}), 'can restrict properties to those named');
|
||||||
result = _.restrict({a:1, b:2, c:3}, ['b', 'c']);
|
result = _.pick({a:1, b:2, c:3}, ['b', 'c']);
|
||||||
ok(_.isEqual(result, {b:2, c:3}), 'can restrict properties to those named in an array');
|
ok(_.isEqual(result, {b:2, c:3}), 'can restrict properties to those named in an array');
|
||||||
result = _.restrict({a:1, b:2, c:3}, ['a'], 'b');
|
result = _.pick({a:1, b:2, c:3}, ['a'], 'b');
|
||||||
ok(_.isEqual(result, {a:1, b:2}), 'can restrict properties to those named in mixed args');
|
ok(_.isEqual(result, {a:1, b:2}), 'can restrict properties to those named in mixed args');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
// Arrays with primitive and object values.
|
// Arrays with primitive and object values.
|
||||||
ok(_.isEqual([1, "Larry", true], [1, "Larry", true]), "Arrays containing identical primitives are equal");
|
ok(_.isEqual([1, "Larry", true], [1, "Larry", true]), "Arrays containing identical primitives are equal");
|
||||||
ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, 9, 25)]), "Arrays containing equivalent elements are equal");
|
ok(_.isEqual([(/Moe/g), new Date(2009, 9, 25)], [(/Moe/g), new Date(2009, 9, 25)]), "Arrays containing equivalent elements are equal");
|
||||||
|
|
||||||
// Multi-dimensional arrays.
|
// Multi-dimensional arrays.
|
||||||
var a = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
|
var a = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
|
||||||
@@ -346,7 +346,7 @@ $(document).ready(function() {
|
|||||||
Date.prototype.isEqual = function(that) {
|
Date.prototype.isEqual = function(that) {
|
||||||
var this_date_components = this.toJSON();
|
var this_date_components = this.toJSON();
|
||||||
var that_date_components = (that instanceof Date) ? that.toJSON() : that;
|
var that_date_components = (that instanceof Date) ? that.toJSON() : that;
|
||||||
delete this_date_components['_type']; delete that_date_components['_type']
|
delete this_date_components['_type']; delete that_date_components['_type'];
|
||||||
return _.isEqual(this_date_components, that_date_components);
|
return _.isEqual(this_date_components, that_date_components);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -648,14 +648,13 @@
|
|||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Restrict a given object to the properties named
|
// Return a copy of the object only containing the whitelisted properties.
|
||||||
_.restrict = function(obj) {
|
_.pick = function(obj) {
|
||||||
if (obj !== Object(obj)) throw new TypeError('Invalid object');
|
var result = {};
|
||||||
var dest = {};
|
each(_.flatten(slice.call(arguments, 1)), function(key) {
|
||||||
each(_.flatten(slice.call(arguments, 1)), function(prop) {
|
if (key in obj) result[key] = obj[key];
|
||||||
if (prop in obj) dest[prop] = obj[prop];
|
|
||||||
});
|
});
|
||||||
return dest;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fill in a given object with default properties.
|
// Fill in a given object with default properties.
|
||||||
|
|||||||
Reference in New Issue
Block a user