Skip to content

Commit

Permalink
_.defaults only overrides undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
caseywebdev committed Feb 12, 2013
1 parent bf657be commit 0ab5bdc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ <h2 id="objects">Object Functions</h2>
<p id="defaults">
<b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
<br />
Fill in null and undefined properties in <b>object</b> with values from the
Fill in undefined properties in <b>object</b> with values from the
<b>defaults</b> objects, and return the <b>object</b>. As soon as the
property is filled, further defaults will have no effect.
</p>
Expand Down
5 changes: 3 additions & 2 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ $(document).ready(function() {

test("defaults", function() {
var result;
var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"};
var options = {zero: 0, one: 1, empty: "", nan: NaN, nothing: null};

_.defaults(options, {zero: 1, one: 10, twenty: 20});
_.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'});
equal(options.zero, 0, 'value exists');
equal(options.one, 1, 'value exists');
equal(options.twenty, 20, 'default applied');
equal(options.nothing, null, "null isn't overriden");

_.defaults(options, {empty: "full"}, {nan: "nan"}, {word: "word"}, {word: "dog"});
equal(options.empty, "", 'value exists');
Expand Down
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
if (obj[prop] == null) obj[prop] = source[prop];
if (obj[prop] === void 0) obj[prop] = source[prop];
}
}
});
Expand Down

0 comments on commit 0ab5bdc

Please sign in to comment.