Skip to content

Commit

Permalink
util: update defaults().
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington committed Jan 10, 2014
1 parent 609c991 commit 1774d38
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 67 deletions.
27 changes: 2 additions & 25 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,10 @@ util.dateify = function(dateish) {

// this is slightly different from lodash version
util.defaults = function(object, source, guard) {
object = object || {};

var args = arguments;
var argsIndex = 0;
var argsLength = typeof guard == 'number' ? 2 : args.length;

while (++argsIndex < argsLength) {
source = args[argsIndex];

if (util.isObject(source)) {
var index = -1;
var props = util.keys(source);
var length = props.length;

while (++index < length) {
var key = props[index];

if (typeof object[key] == 'undefined' || object[key] === null) {
object[key] = source[key];
} else if (util.isObject(object[key]) && util.isObject(source[key])) {
object[key] = util.defaults(object[key], source[key]);
}
}
}
}
args[0] = args[0] || {};

return object;
return util._.defaults.apply(util._, args);
};

util.dosDateTime = function(d, utc) {
Expand Down
43 changes: 1 addition & 42 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('utils', function() {
});
});

describe('defaults(object, source)', function() {
describe('defaults(object, source, guard)', function() {
it('should default when object key is missing', function() {
var actual = utils.defaults({ value1: true }, {
value2: true
Expand All @@ -155,47 +155,6 @@ describe('utils', function() {
value2: true
});
});

it('should default when object key contains null value', function() {
var actual = utils.defaults({ value1: null }, {
value1: true,
value2: true
});

assert.deepEqual(actual, {
value1: true,
value2: true
});
});

it('should not default when object value is zero', function() {
var actual = utils.defaults({ value1: 0 }, {
value1: 1
});

assert.deepEqual(actual, {
value1: 0
});
});

it('should support defaulting multiple levels', function() {
var actual = utils.defaults({
level1: {
value1: 0
}
}, {
level1: {
value2: 2
}
});

assert.deepEqual(actual, {
level1: {
value1: 0,
value2: 2
}
});
});
});

describe('dosDateTime(date, utc)', function() {
Expand Down

0 comments on commit 1774d38

Please sign in to comment.