diff --git a/pathname/assign.js b/pathname/assign.js index 607b630a..9dcb3d3e 100644 --- a/pathname/assign.js +++ b/pathname/assign.js @@ -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] }) } diff --git a/pathname/tests/index.html b/pathname/tests/index.html index 659d8eec..62ee3fcb 100644 --- a/pathname/tests/index.html +++ b/pathname/tests/index.html @@ -13,6 +13,7 @@ + diff --git a/pathname/tests/test-assign.js b/pathname/tests/test-assign.js new file mode 100644 index 00000000..62e54e2f --- /dev/null +++ b/pathname/tests/test-assign.js @@ -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}) + }) +})