From d85490fbbbeee82a806f0a665ba6a33afe1918a5 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
+
functions
Object Functions
=> [1, 2, 3]
- _.functions(object)
Alias: methods
@@ -1906,7 +1906,7 @@ Change Log
like so: _([1, 2, 3]).map(...);. Original patch provided by
Marc-André Cournoyer.
Wrapped objects can be chained through multiple
- method invocations. A functions method
+ method invocations. A functions method
was added, providing a sorted list of all the functions in Underscore.
_.uniqueId('contact_'); From 7a2e45eb3a47506004b1d4d864106645fd7e7740 Mon Sep 17 00:00:00 2001 From: bjh- + +Date: Thu, 26 Jan 2012 15:57:52 -0500 Subject: [PATCH 05/66] added documentation for _.isObject --- index.html | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index dd963c707..3aa65a58a 100644 --- a/index.html +++ b/index.html @@ -231,6 +231,7 @@ - isNaN - isNull - isUndefined +- isObject @@ -1223,7 +1224,17 @@Object Functions
_.isUndefined(window.missingVariable); => true
+ isObject_.isObject(variable)
+
+ Returns true if variable is an object.
+
+_.isObject({}); +=> true ++
From 53829cbd5c51e3319b843130890fbef0abd951c9 Mon Sep 17 00:00:00 2001
From: bjh
+ isObject
@@ -1223,18 +1235,6 @@
- isObject Just some text. Hey, I know this is silly but it aids consistency. Just some text. Hey, I know this is silly but it aids consistency. Just some text. Hey, I know this is silly but it aids consistency. \u2028<%= "\\u2028\\u2029" %>\u2029 \u2028\u2028\u2029\u2029
You may also read through the annotated source code.
sortBy
shuffle
throttle
once
after
has
@@ -1275,7 +1276,7 @@
escape
+ getValue
template
If ERB-style delimiters aren't your cup of tea, you can change Underscore's
template settings to use different symbols to set off interpolated code.
- Define an interpolate regex to match expressions that should be
- interpolated verbatim, an escape regex to match expressions that should
+ Define an interpolate regex to match expressions that should be
+ interpolated verbatim, an escape regex to match expressions that should
be inserted after being HTML escaped, and an evaluate regex to match
expressions that should be evaluated without insertion into the resulting
string. You may define or omit any combination of the three.
@@ -1451,7 +1464,7 @@
Underscore.php,
a PHP port of the functions that are applicable in both languages.
@@ -1459,17 +1472,17 @@
Underscore-perl,
- a Perl port of many of the Underscore.js functions,
- aimed at on Perl hashes and arrays, also
+ a Perl port of many of the Underscore.js functions,
+ aimed at on Perl hashes and arrays, also
available on GitHub.
Underscore.string,
- an Underscore extension that adds functions for string-manipulation:
+ an Underscore extension that adds functions for string-manipulation:
trim, startsWith, contains, capitalize,
reverse, sprintf, and more.
Michael Aufreiter's Data.js,
a data manipulation + persistence library for JavaScript.
@@ -1499,7 +1512,7 @@
1.3.1 — Jan. 23, 2012
1.3.0 — Jan. 11, 2012
1.2.4 — Jan. 4, 2012
1.2.3 — Dec. 7, 2011
1.2.2 — Nov. 14, 2011
1.2.1 — Oct. 24, 2011
1.2.0 — Oct. 5, 2011
1.1.7 — July 13, 2011
1.1.6 — April 18, 2011
1.1.5 — Mar 20, 2011
1.1.4 — Jan 9, 2011
1.1.3 — Dec 1, 2010
1.1.2
1.1.1
1.1.0
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.6.0 \u2028\u2028\u2029\u2029
- getValue
+ result
diff --git a/test/utility.js b/test/utility.js
index 5f2485478..ec9960eae 100644
--- a/test/utility.js
+++ b/test/utility.js
@@ -165,13 +165,13 @@ $(document).ready(function() {
strictEqual(tmpl(), ' \u2028\u2028\u2029\u2029
result
result
+ Precompiling your templates can be a big help when debugging errors you can't
+ reproduce. This is because precompiled templates can provide line numbers and
+ a stack trace, something that is not possible when compiling templates on the client.
+ template provides the source property on the compiled template
+ function for easy precompilation.
+
+ restrict
diff --git a/test/objects.js b/test/objects.js
index 1745d8a3f..422e75754 100644
--- a/test/objects.js
+++ b/test/objects.js
@@ -41,6 +41,16 @@ $(document).ready(function() {
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
});
+ test("objects: restrict", function() {
+ var result;
+ result = _.restrict({a:1, b:2, c:3}, 'a', 'c');
+ ok(_.isEqual(result, {a:1, c:3}), 'can restrict properties to those named');
+ result = _.restrict({a:1, b:2, c:3}, ['b', 'c']);
+ 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');
+ ok(_.isEqual(result, {a:1, b:2}), 'can restrict properties to those named in mixed args');
+ });
+
test("objects: defaults", function() {
var result;
var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"};
diff --git a/underscore.js b/underscore.js
index 44226a07c..17549c5e8 100644
--- a/underscore.js
+++ b/underscore.js
@@ -645,6 +645,16 @@
return obj;
};
+ // Restrict a given object to the properties named
+ _.restrict = function(obj) {
+ if (obj !== Object(obj)) throw new TypeError('Invalid object');
+ var dest = {};
+ each(_.flatten(slice.call(arguments, 1)), function(prop) {
+ if (prop in obj) dest[prop] = obj[prop];
+ });
+ return dest;
+ };
+
// Fill in a given object with default properties.
_.defaults = function(obj) {
each(slice.call(arguments, 1), function(source) {
From 6b8a99ba395c6b53fdec24c8d134861b47a09d65 Mon Sep 17 00:00:00 2001
From: "Yi, EungJun"
- Using the object-oriented style allows you to chain together methods. Calling
- chain on a wrapped object will cause all future method calls to
+ Calling chain on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use value to retrieve the final value. Here's an example of chaining
together a map/flatten/reduce, in order to get the word count of
@@ -1534,7 +1533,8 @@
- Using the object-oriented style allows you to chain together methods. Calling
- chain on a wrapped object will cause all future method calls to
+ Calling chain on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use value to retrieve the final value. Here's an example of chaining
together a map/flatten/reduce, in order to get the word count of
@@ -1520,7 +1519,8 @@
- restrict
+ pickObject Functions
_.isObject({});
=> true
+_.isObject('am I an object?');
+=> false
Utility Functions
From bdb3cb21f9aacf7de53169c99563b471f985b82d Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas Object Functions
=> false
_.isArray([1,2,3]);
=> true
+
+
+ _.isObject(value)
+
+ Returns true if value is an Object.
+
+_.isObject({});
+=> true
+_.isObject(1);
+=> false
Object Functions
_.isUndefined(window.missingVariable);
=> true
-
-
- _.isObject(variable)
-
- Returns true if variable is an object.
-
-_.isObject({});
-=> true
-_.isObject('am I an object?');
-=> false
Utility Functions
From 5c6abc4453f3bdf9470bd98ec79d7f22105f87e3 Mon Sep 17 00:00:00 2001
From: Brad Dunbar <% \
for (key in people) { \
%>
");
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
- equals(result, "
", 'can run arbitrary javascript in templates');
+ equal(result, "
", 'can run arbitrary javascript in templates');
var escapedCharsInJavascriptTemplate = _.template("<% _.each(numbers.split('\\n'), function(item) { %>
");
result = escapedCharsInJavascriptTemplate({numbers: "one\ntwo\nthree\nfour"});
- equals(result, "
", 'Can use escaped characters (e.g. \\n) in Javascript');
+ equal(result, "
", 'Can use escaped characters (e.g. \\n) in Javascript');
var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %>Downloads (Right-click, and u
< 4kb, Minified and Gzipped
-
+
Collection Functions (Arrays or Objects)
_.sortBy(list, iterator, [context])
- Returns a sorted copy of list, ranked in ascending order by the
+ Returns a sorted copy of list, ranked in ascending order by the
results of running each value through iterator.
@@ -565,7 +566,7 @@
Collection Functions (Arrays or Objects)
_.shuffle(list)
- Returns a shuffled copy of the list, using a version of the
+ Returns a shuffled copy of the list, using a version of the
Fisher-Yates shuffle.
@@ -751,7 +752,7 @@
Array Functions
Returns the index at which value can be found in the array,
or -1 if value is not present in the array. Uses the native
- indexOf function unless it's missing. If you're working with a
+ indexOf function unless it's missing. If you're working with a
large array, and you know that the array is already sorted, pass true
for isSorted to use a faster binary search.
@@ -836,7 +837,7 @@ Function (uh, ahem) Functions
memoize_.memoize(function, [hashFunction])
Memoizes a given function by caching the computed result. Useful
- for speeding up slow-running computations. If passed an optional
+ for speeding up slow-running computations. If passed an optional
hashFunction, it will be used to compute the hash key for storing
the result, based on the arguments to the original function. The default
hashFunction just uses the first argument to the memoized function
@@ -877,8 +878,8 @@ Function (uh, ahem) Functions
_.throttle(function, wait)
- Creates and returns a new, throttled version of the passed function,
- that, when invoked repeatedly, will only actually call the original function
+ Creates and returns a new, throttled version of the passed function,
+ that, when invoked repeatedly, will only actually call the original function
at most once per every wait
milliseconds. Useful for rate-limiting events that occur faster than you
can keep up with.
@@ -892,11 +893,11 @@ Function (uh, ahem) Functions
debounce_.debounce(function, wait)
Creates and returns a new debounced version of the passed function that
- will postpone its execution until after
- wait milliseconds have elapsed since the last time it
- was invoked. Useful for implementing behavior that should only happen
- after the input has stopped arriving. For example: rendering a
- preview of a Markdown comment, recalculating a layout after the window
+ will postpone its execution until after
+ wait milliseconds have elapsed since the last time it
+ was invoked. Useful for implementing behavior that should only happen
+ after the input has stopped arriving. For example: rendering a
+ preview of a Markdown comment, recalculating a layout after the window
has stopped being resized, and so on.
@@ -907,7 +908,7 @@
Function (uh, ahem) Functions
_.once(function)
- Creates a version of the function that can only be called one time.
+ Creates a version of the function that can only be called one time.
Repeated calls to the modified function will have no effect, returning
the value from the original call. Useful for initialization functions,
instead of having to set a boolean flag and then check it later.
@@ -922,7 +923,7 @@ Function (uh, ahem) Functions
_.after(count, function)
- Creates a version of the function that will only be run after first
+ Creates a version of the function that will only be run after first
being called count times. Useful for grouping asynchronous responses,
where you want to be sure that all the async calls have finished, before
proceeding.
@@ -930,7 +931,7 @@ Function (uh, ahem) Functions
var renderNotes = _.after(notes.length, render);
_.each(notes, function(note) {
- note.asyncSave({success: renderNotes});
+ note.asyncSave({success: renderNotes});
});
// renderNotes is run once, after all notes have saved.
@@ -1058,9 +1059,9 @@ Object Functions
_.has(object, key)
- Does the object contain the given key? Identical to
+ Does the object contain the given key? Identical to
object.hasOwnProperty(key), but uses a safe reference to the
- hasOwnProperty function, in case it's been
+ hasOwnProperty function, in case it's been
overridden accidentally.
@@ -1238,7 +1239,7 @@
-
+
Object Functions
_.isUndefined(window.missingVariable);
=> true
Utility Functions
Utility Functions
mixin_.mixin(object)
Allows you to extend Underscore with your own utility functions. Pass
- a hash of {name: function} definitions to have your functions
+ a hash of {name: function} definitions to have your functions
added to the Underscore object, as well as the OOP wrapper.
@@ -1302,13 +1303,25 @@
Utility Functions
_.escape(string)
- Escapes a string for insertion into HTML, replacing
+ Escapes a string for insertion into HTML, replacing
&, <, >, ", ', and / characters.
_.escape('Curly, Larry & Moe');
=> "Curly, Larry & Moe"
+ _.getValue(object, property)
+
+ Returns a value from an object as a property or as a function.
+
+var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
+_.getValue(object, 'cheese');
+=> "crumpets"
+_.getValue(object, 'stuff');
+=> "nonsense"
+
_.template(templateString, [context])
@@ -1340,7 +1353,7 @@ Utility Functions
You can also use print from within JavaScript code. This is
sometimes more convenient than using <%= ... %>.
-
+
var compiled = _.template("<% print('Hello ' + epithet); %>");
compiled({epithet: "stooge"});
@@ -1349,8 +1362,8 @@
Utility Functions
Links & Suggested Reading
The source is
available on GitHub.
-
+
Links & Suggested Reading
The source is
available on GitHub.
-
+
Links & Suggested Reading
Functional JavaScript,
which includes comprehensive higher-order function support as well as string lambdas.
-
+
Links & Suggested Reading
Change Log
-
+
@@ -1510,7 +1523,7 @@
-
+
Change Log
Added _.collect as an alias for _.map. Smalltalkers, rejoice.
Change Log
@@ -1529,12 +1542,12 @@
-
+
Change Log
-
+
Change Log
@@ -1564,12 +1577,12 @@
-
+
Change Log
@@ -1588,22 +1601,22 @@
-
+
Change Log
-
+
Change Log
grouping values by a particular common property.
Change Log
Change Log
-
+
Change Log
Added _.groupBy, which aggregates a collection into groups of like items.
- Added _.union and _.difference, to complement the
+ Added _.union and _.difference, to complement the
(re-named) _.intersection.
Various improvements for support of sparse arrays.
_.toArray now returns a clone, if directly passed an array.
_.functions now also returns the names of functions that are present
in the prototype chain.
Added _.after, which will return a function that only runs after
@@ -1684,30 +1697,30 @@ Change Log
_.extend no longer copies keys when the value is undefined.
_.bind now errors when trying to bind an undefined value.
-
+
Added an _.defaults function, for use merging together JS objects
representing default options.
Added an _.once function, for manufacturing functions that should
only ever execute a single time.
- _.bind now delegates to the native ECMAScript 5 version,
+ _.bind now delegates to the native ECMAScript 5 version,
where available.
_.keys now throws an error when used on non-Object values, as in
ECMAScript 5.
Fixed a bug with _.keys when used over sparse arrays.
- Improved compliance with ES5's Array methods when passing null
+ Improved compliance with ES5's Array methods when passing null
as a value. _.wrap now correctly sets this for the
wrapped function. _.indexOf now takes an optional flag for
finding the insertion index in an array that is guaranteed to already
be sorted. Avoiding the use of .callee, to allow _.isArray
to work properly in ES5's strict mode.
In CommonJS, Underscore may now be required with just:
@@ -1719,26 +1732,26 @@ Change Log
Improved the isType family of functions for better interoperability
with Internet Explorer host objects.
_.template now correctly escapes backslashes in templates.
- Improved _.reduce compatibility with the ECMA5 version:
+ Improved _.reduce compatibility with the ECMA5 version:
if you don't pass an initial value, the first item in the collection is used.
_.each no longer returns the iterated collection, for improved
consistency with ES5's forEach.
-
+
- Fixed _.contains, which was mistakenly pointing at
- _.intersect instead of _.include, like it should
+ Fixed _.contains, which was mistakenly pointing at
+ _.intersect instead of _.include, like it should
have been. Added _.unique as an alias for _.uniq.
Improved the speed of _.template, and its handling of multiline
- interpolations. Ryan Tenney contributed optimizations to many Underscore
+ interpolations. Ryan Tenney contributed optimizations to many Underscore
functions. An annotated version of the source code is now available.
The method signature of _.reduce has been changed to match
@@ -1747,33 +1760,33 @@ Change Log
called with no arguments, and preserves whitespace. _.contains
is a new alias for _.include.
-
+
- Andri Möll contributed the _.memoize
- function, which can be used to speed up expensive repeated computations
+ Andri Möll contributed the _.memoize
+ function, which can be used to speed up expensive repeated computations
by caching the results.
Patch that makes _.isEqual return false if any property
of the compared object has a NaN value. Technically the correct
thing to do, but of questionable semantics. Watch out for NaN comparisons.
Fixes _.isArguments in recent versions of Opera, which have
arguments objects as real Arrays.
- Bugfix for _.isEqual, when comparing two objects with the same
+ Bugfix for _.isEqual, when comparing two objects with the same
number of undefined keys, but with different names.
Things have been stable for many months now, so Underscore is now
@@ -1781,15 +1794,15 @@ Change Log
include _.isBoolean, and the ability to have _.extend
take multiple source objects.
-
+
- Major release. Incorporates a number of
+ Major release. Incorporates a number of
Mile Frawley's refactors for
safer duck-typing on collection functions, and cleaner internals. A new
_.mixin method that allows you to extend Underscore with utility
- functions of your own. Added _.times, which works the same as in
- Ruby or Prototype.js. Native support for ECMAScript 5's Array.isArray,
+ functions of your own. Added _.times, which works the same as in
+ Ruby or Prototype.js. Native support for ECMAScript 5's Array.isArray,
and Object.keys.
Change Log
0.5.1
Added an _.isArguments function. Lots of little safety checks
and optimizations contributed by
- Noah Sloan and
+ Noah Sloan and
Andri Möll.
diff --git a/test/utility.js b/test/utility.js
index c285751ea..5f2485478 100644
--- a/test/utility.js
+++ b/test/utility.js
@@ -165,4 +165,13 @@ $(document).ready(function() {
strictEqual(tmpl(), 'Utility Functions
_.escape('Curly, Larry & Moe');
=> "Curly, Larry & Moe"
- _.getValue(object, property)
+ _.result(object, property)
Returns a value from an object as a property or as a function.
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
-_.getValue(object, 'cheese');
+_.result(object, 'cheese');
=> "crumpets"
-_.getValue(object, 'stuff');
+_.result(object, 'stuff');
=> "nonsense"
Utility Functions
_.result(object, property)
- Returns a value from an object as a property or as a function.
+ If the value of the named property is a function then invoke it.
+ Otherwise, return its value.
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
diff --git a/underscore.js b/underscore.js
index 6c9512594..429a9b142 100644
--- a/underscore.js
+++ b/underscore.js
@@ -873,7 +873,8 @@
return (''+string).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
};
- // Get a value from an object as a property or as a function.
+ // If the value of the named property is a function then invoke it.
+ // Otherwise, return its value.
_.result = function(object, property) {
if (object == null) return null;
var value = object[property];
From 33be5c62b8fe39b185024ea7d4e8b0e196f29d6b Mon Sep 17 00:00:00 2001
From: Brad Dunbar
Utility Functions
_.result(object, property)
- If the value of the named property is a function then invoke it.
- Otherwise, return its value.
+ If the value of the named property is a function then invoke it; otherwise, return it.
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
diff --git a/underscore.js b/underscore.js
index 429a9b142..21a45c0d9 100644
--- a/underscore.js
+++ b/underscore.js
@@ -873,8 +873,8 @@
return (''+string).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
};
- // If the value of the named property is a function then invoke it.
- // Otherwise, return its value.
+ // If the value of the named property is a function then invoke it;
+ // otherwise, return it.
_.result = function(object, property) {
if (object == null) return null;
var value = object[property];
From 2d3edb88f0bfda175082a2f55cf3cda980e5e489 Mon Sep 17 00:00:00 2001
From: Raymond May Jr
+ Utility Functions
Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
- data sources. Template functions can both interpolate variables, using
+ data sources. Template functions can both interpolate variables, using
<%= … %>, as well as execute arbitrary JavaScript code, with
<% … %>. If you wish to interpolate a value, and have
it be HTML-escaped, use <%- … %> When you evaluate a template function, pass in a
@@ -1381,6 +1381,18 @@ Utility Functions
template({name : "Mustache"});
=> "Hello Mustache!"<script>
+ JST.project = <%= _.template(jstText).source %>;
+</script>
+
Chaining
diff --git a/underscore.js b/underscore.js
index 1649df208..44226a07c 100644
--- a/underscore.js
+++ b/underscore.js
@@ -960,7 +960,7 @@
var template = function(data) {
return render.call(this, data, _);
};
- template.source = 'function(obj, _){\n' + source + '\n}';
+ template.source = 'function(obj){\n' + source + '\n}';
return template;
};
From 4266d9180578f027a48655d8966fc771db6192e1 Mon Sep 17 00:00:00 2001
From: Raymond May Jr Object Functions
_.extend({name : 'moe'}, {age : 50});
=> {name : 'moe', age : 50}
+
+
+ _.restrict(source, *keys)
+
+ Return a clone of the source with only the properties with the
+ property names, or arrays of property names, provided in keys.
+
+_.restrict({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}
Chaining
_([1, 2, 3]).map(function(n){ return n * 2; });Change Log
1.2.4 — Jan. 4, 2012
Chaining
_([1, 2, 3]).map(function(n){ return n * 2; });
Change Log
1.2.4 — Jan. 4, 2012
Object Functions
=> {name : 'moe', age : 50}
- _.restrict(source, *keys)
+ _.pick(object, *keys)
- Return a clone of the source with only the properties with the
- property names, or arrays of property names, provided in keys.
+ Return a copy of the object, filtered to only have values for
+ the whitelisted keys (or array of valid keys).
-_.restrict({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
-=> {name : 'moe', age : 50}
-_.restrict({name : 'moe', age: 50, userid : 'moe1'}, ['name', 'age']);
+_.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
=> {name : 'moe', age : 50}
diff --git a/test/objects.js b/test/objects.js
index 422e75754..8fb3d1b9d 100644
--- a/test/objects.js
+++ b/test/objects.js
@@ -41,13 +41,13 @@ $(document).ready(function() {
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
});
- test("objects: restrict", function() {
+ test("objects: pick", function() {
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');
- 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');
- 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');
});
@@ -178,7 +178,7 @@ $(document).ready(function() {
// Arrays with primitive and object values.
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.
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) {
var this_date_components = this.toJSON();
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);
};
diff --git a/underscore.js b/underscore.js
index 12423c7e5..cb2d424de 100644
--- a/underscore.js
+++ b/underscore.js
@@ -648,14 +648,13 @@
return obj;
};
- // Restrict a given object to the properties named
- _.restrict = function(obj) {
- if (obj !== Object(obj)) throw new TypeError('Invalid object');
- var dest = {};
- each(_.flatten(slice.call(arguments, 1)), function(prop) {
- if (prop in obj) dest[prop] = obj[prop];
+ // Return a copy of the object only containing the whitelisted properties.
+ _.pick = function(obj) {
+ var result = {};
+ each(_.flatten(slice.call(arguments, 1)), function(key) {
+ if (key in obj) result[key] = obj[key];
});
- return dest;
+ return result;
};
// Fill in a given object with default properties.
From 6f5489fcabd510ca5b0e62ae04e549775da32e2d Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas