support falsy arguments in Object.assign polyfill (#2433)
* support falsy arguments in Object.assign polyfill * add tests for assign polyfill
This commit is contained in:
parent
dd6572579d
commit
10f0b4934a
3 changed files with 28 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
|||
"use strict"
|
||||
|
||||
module.exports = Object.assign || function(target, source) {
|
||||
Object.keys(source).forEach(function(key) { target[key] = source[key] })
|
||||
if(source) Object.keys(source).forEach(function(key) { target[key] = source[key] })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
<script src="test-buildPathname.js"></script>
|
||||
<script src="test-parsePathname.js"></script>
|
||||
<script src="test-parseTemplate.js"></script>
|
||||
<script src="test-assign.js"></script>
|
||||
|
||||
<script>require("../../ospec/ospec").run()</script>
|
||||
</body>
|
||||
|
|
|
|||
26
pathname/tests/test-assign.js
Normal file
26
pathname/tests/test-assign.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
"use strict"
|
||||
|
||||
var o = require("../../ospec/ospec")
|
||||
|
||||
// force usage of polyfill
|
||||
var save = Object.assign
|
||||
Object.assign = null
|
||||
delete require.cache[require.resolve("../assign")]
|
||||
var assign = require("../assign")
|
||||
Object.assign = save
|
||||
|
||||
o.spec("assign polyfill", function() {
|
||||
o("works", function() {
|
||||
var target = {hello: "world", foo: "bar"}
|
||||
var source = {foo: "foo", extra: true}
|
||||
|
||||
assign(target, source)
|
||||
|
||||
o(target).deepEquals({hello: "world", foo: "foo", extra: true})
|
||||
|
||||
var falsySources = [null, 0, "", false, void 0]
|
||||
falsySources.forEach(function(falsy) { assign(target, falsy) })
|
||||
|
||||
o(target).deepEquals({hello: "world", foo: "foo", extra: true})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue