node package manager

object.assign

ES6 spec-compliant Object.assign shim. From https://github.com/es-shims/es6-shim

object.assign Version Badge

Build Status dependency status dev dependency status

npm badge

browser support

An Object.assign shim. Invoke its "shim" method to shim Object.assign if it is unavailable.

Most common usage:

var assign = Object.assign || require('object.assign');

Example

// Multiple sources!
var target = { a: true };
var source1 = { b: true };
var source2 = { c: true };
var sourceN = { n: true };

var expected = {
    a: true,
    b: true,
    c: true,
    n: true
};

var assign = require('object.assign');
var assert = require('assert');

[source1, source2, sourceN].reduce(Object.assign, target);
assert.deepEqual(target, expected); // AWESOME!
var assign = require('object.assign');
var assert = require('assert');
var target = {
    a: true,
    b: true,
    c: true
};
var source = {
    c: false,
    d: false,
    e: false
};

var assigned = assign(target, source);
assert.equal(target, assigned); // returns the target object
assert.deepEqual(assigned, {
    a: true,
    b: true,
    c: false,
    d: false,
    e: false
});
var assign = require('object.assign');
var assert = require('assert');
/* when Object.assign is not present */
delete Object.assign;
var shimmedAssign = assign.shim();
assert.equal(shimmedAssign, assign);

var target = {
    a: true,
    b: true,
    c: true
};
var source = {
    c: false,
    d: false,
    e: false
};

var assigned = assign(target, source);
assert.deepEqual(Object.assign(target, source), assign(target, source));
var assign = require('object.assign');
var assert = require('assert');
/* when Object.assign is present */
var shimmedAssign = assign.shim();
assert.equal(shimmedAssign, Object.assign);

var target = {
    a: true,
    b: true,
    c: true
};
var source = {
    c: false,
    d: false,
    e: false
};

assert.deepEqual(Object.assign(target, source), assign(target, source));

Source

Implementation taken directly from es6-shim and modified locally.

Tests

Simply clone the repo, npm install, and run npm test