Add _.restrict(source, *keys)

Return a clone of the source with only the properties named in *keys
(either stings or arrays containing strings).

Especially useful for avoiding mass-assignment vulnerabilities.
This commit is contained in:
Chris Leishman
2012-03-24 12:26:03 -07:00
parent 00ed5d70c8
commit 7c95237845
3 changed files with 33 additions and 0 deletions

View File

@@ -1014,6 +1014,19 @@ _.functions(_);
<pre>
_.extend({name : 'moe'}, {age : 50});
=&gt; {name : 'moe', age : 50}
</pre>
<p id="restrict">
<b class="header">restrict</b><code>_.restrict(source, *keys)</code>
<br />
Return a clone of the <b>source</b> with only the properties with the
property names, or arrays of property names, provided in <b>keys</b>.
</p>
<pre>
_.restrict({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
=&gt; {name : 'moe', age : 50}
_.restrict({name : 'moe', age: 50, userid : 'moe1'}, ['name', 'age']);
=&gt; {name : 'moe', age : 50}
</pre>
<p id="defaults">