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:
Daniel Loomer 2019-06-15 22:48:24 -04:00 committed by Isiah Meadows
parent dd6572579d
commit 10f0b4934a
3 changed files with 28 additions and 1 deletions

View file

@ -1,5 +1,5 @@
"use strict" "use strict"
module.exports = Object.assign || function(target, source) { 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] })
} }

View file

@ -13,6 +13,7 @@
<script src="test-buildPathname.js"></script> <script src="test-buildPathname.js"></script>
<script src="test-parsePathname.js"></script> <script src="test-parsePathname.js"></script>
<script src="test-parseTemplate.js"></script> <script src="test-parseTemplate.js"></script>
<script src="test-assign.js"></script>
<script>require("../../ospec/ospec").run()</script> <script>require("../../ospec/ospec").run()</script>
</body> </body>

View 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})
})
})