This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lodash): replace "defaults" with pure alternative (#692)
* refactor(lodash): replace "defaults" with pure alternative The original lodash tests don't check that it also modifies the target, and as far as I can tell, we don't rely on that behaviour. * Update src/functions/defaultsPure.js
- Loading branch information
Showing
5 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
// NOTE: this behaves like lodash/defaults, but doesn't mutate the target | ||
module.exports = function defaultsPure() { | ||
const sources = Array.prototype.slice.call(arguments); | ||
return sources.reduceRight(function(acc, source) { | ||
Object.keys(Object(source)).forEach(function(key) { | ||
if (source[key] !== undefined) { | ||
acc[key] = source[key]; | ||
} | ||
}); | ||
return acc; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
var clone = require('lodash/clone'); | ||
var defaults = require('../../../src/functions/defaultsPure'); | ||
|
||
// tests modified from lodash source | ||
|
||
it('should assign source properties if missing on `object`', function() { | ||
var actual = defaults({'a': 1}, {'a': 2, 'b': 2}); | ||
expect(actual).toEqual({'a': 1, 'b': 2}); | ||
}); | ||
|
||
it('should accept multiple sources', function() { | ||
var expected = {'a': 1, 'b': 2, 'c': 3}; | ||
var actual = defaults({'a': 1, 'b': 2}, {'b': 3}, {'c': 3}); | ||
|
||
expect(actual).toEqual(expected); | ||
|
||
actual = defaults({'a': 1, 'b': 2}, {'b': 3, 'c': 3}, {'c': 2}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should not overwrite `null` values', function() { | ||
var actual = defaults({'a': null}, {'a': 1}); | ||
expect(actual.a).toBe(null); | ||
}); | ||
|
||
it('should overwrite `undefined` values', function() { | ||
var actual = defaults({'a': undefined}, {'a': 1}); | ||
expect(actual.a).toBe(1); | ||
}); | ||
|
||
it('should assign `undefined` values', function() { | ||
var source = {'a': undefined, 'b': 1}; | ||
var actual = defaults({}, source); | ||
|
||
expect(actual).toEqual({'a': undefined, 'b': 1}); | ||
}); | ||
|
||
it('should assign properties that shadow those on `Object.prototype`', function() { | ||
var object = { | ||
'constructor': Object.prototype.constructor, | ||
'hasOwnProperty': Object.prototype.hasOwnProperty, | ||
'isPrototypeOf': Object.prototype.isPrototypeOf, | ||
'propertyIsEnumerable': Object.prototype.propertyIsEnumerable, | ||
'toLocaleString': Object.prototype.toLocaleString, | ||
'toString': Object.prototype.toString, | ||
'valueOf': Object.prototype.valueOf | ||
}; | ||
|
||
var source = { | ||
'constructor': 1, | ||
'hasOwnProperty': 2, | ||
'isPrototypeOf': 3, | ||
'propertyIsEnumerable': 4, | ||
'toLocaleString': 5, | ||
'toString': 6, | ||
'valueOf': 7 | ||
}; | ||
|
||
var expected = clone(source); | ||
expect(defaults({}, source)).toEqual(expected); | ||
|
||
expected = clone(object); | ||
expect(defaults({}, object, source)).toEqual(expected); | ||
}); |