1 | #object.assign <sup>[![Version Badge][npm-version-svg]][npm-url]</sup>
|
---|
2 |
|
---|
3 | [![Build Status][travis-svg]][travis-url]
|
---|
4 | [![dependency status][deps-svg]][deps-url]
|
---|
5 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
6 | [![License][license-image]][license-url]
|
---|
7 | [![Downloads][downloads-image]][downloads-url]
|
---|
8 |
|
---|
9 | [![npm badge][npm-badge-png]][npm-url]
|
---|
10 |
|
---|
11 | [![browser support][testling-png]][testling-url]
|
---|
12 |
|
---|
13 | An Object.assign shim. Invoke its "shim" method to shim Object.assign if it is unavailable.
|
---|
14 |
|
---|
15 | This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](http://www.ecma-international.org/ecma-262/6.0/#sec-object.assign). In an ES6 environment, it will also work properly with `Symbol`s.
|
---|
16 |
|
---|
17 | Takes a minimum of 2 arguments: `target` and `source`.
|
---|
18 | Takes a variable sized list of source arguments - at least 1, as many as you want.
|
---|
19 | Throws a TypeError if the `target` argument is `null` or `undefined`.
|
---|
20 |
|
---|
21 | Most common usage:
|
---|
22 | ```js
|
---|
23 | var assign = require('object.assign').getPolyfill(); // returns native method if compliant
|
---|
24 | /* or */
|
---|
25 | var assign = require('object.assign/polyfill')(); // returns native method if compliant
|
---|
26 | ```
|
---|
27 |
|
---|
28 | ## Example
|
---|
29 |
|
---|
30 | ```js
|
---|
31 | var assert = require('assert');
|
---|
32 |
|
---|
33 | // Multiple sources!
|
---|
34 | var target = { a: true };
|
---|
35 | var source1 = { b: true };
|
---|
36 | var source2 = { c: true };
|
---|
37 | var sourceN = { n: true };
|
---|
38 |
|
---|
39 | var expected = {
|
---|
40 | a: true,
|
---|
41 | b: true,
|
---|
42 | c: true,
|
---|
43 | n: true
|
---|
44 | };
|
---|
45 |
|
---|
46 | assign(target, source1, source2, sourceN);
|
---|
47 | assert.deepEqual(target, expected); // AWESOME!
|
---|
48 | ```
|
---|
49 |
|
---|
50 | ```js
|
---|
51 | var target = {
|
---|
52 | a: true,
|
---|
53 | b: true,
|
---|
54 | c: true
|
---|
55 | };
|
---|
56 | var source1 = {
|
---|
57 | c: false,
|
---|
58 | d: false
|
---|
59 | };
|
---|
60 | var sourceN = {
|
---|
61 | e: false
|
---|
62 | };
|
---|
63 |
|
---|
64 | var assigned = assign(target, source1, sourceN);
|
---|
65 | assert.equal(target, assigned); // returns the target object
|
---|
66 | assert.deepEqual(assigned, {
|
---|
67 | a: true,
|
---|
68 | b: true,
|
---|
69 | c: false,
|
---|
70 | d: false,
|
---|
71 | e: false
|
---|
72 | });
|
---|
73 | ```
|
---|
74 |
|
---|
75 | ```js
|
---|
76 | /* when Object.assign is not present */
|
---|
77 | delete Object.assign;
|
---|
78 | var shimmedAssign = require('object.assign').shim();
|
---|
79 | /* or */
|
---|
80 | var shimmedAssign = require('object.assign/shim')();
|
---|
81 |
|
---|
82 | assert.equal(shimmedAssign, assign);
|
---|
83 |
|
---|
84 | var target = {
|
---|
85 | a: true,
|
---|
86 | b: true,
|
---|
87 | c: true
|
---|
88 | };
|
---|
89 | var source = {
|
---|
90 | c: false,
|
---|
91 | d: false,
|
---|
92 | e: false
|
---|
93 | };
|
---|
94 |
|
---|
95 | var assigned = assign(target, source);
|
---|
96 | assert.deepEqual(Object.assign(target, source), assign(target, source));
|
---|
97 | ```
|
---|
98 |
|
---|
99 | ```js
|
---|
100 | /* when Object.assign is present */
|
---|
101 | var shimmedAssign = require('object.assign').shim();
|
---|
102 | assert.equal(shimmedAssign, Object.assign);
|
---|
103 |
|
---|
104 | var target = {
|
---|
105 | a: true,
|
---|
106 | b: true,
|
---|
107 | c: true
|
---|
108 | };
|
---|
109 | var source = {
|
---|
110 | c: false,
|
---|
111 | d: false,
|
---|
112 | e: false
|
---|
113 | };
|
---|
114 |
|
---|
115 | assert.deepEqual(Object.assign(target, source), assign(target, source));
|
---|
116 | ```
|
---|
117 |
|
---|
118 | ## Tests
|
---|
119 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
120 |
|
---|
121 | [npm-url]: https://npmjs.org/package/object.assign
|
---|
122 | [npm-version-svg]: http://versionbadg.es/ljharb/object.assign.svg
|
---|
123 | [travis-svg]: https://travis-ci.org/ljharb/object.assign.svg
|
---|
124 | [travis-url]: https://travis-ci.org/ljharb/object.assign
|
---|
125 | [deps-svg]: https://david-dm.org/ljharb/object.assign.svg?theme=shields.io
|
---|
126 | [deps-url]: https://david-dm.org/ljharb/object.assign
|
---|
127 | [dev-deps-svg]: https://david-dm.org/ljharb/object.assign/dev-status.svg?theme=shields.io
|
---|
128 | [dev-deps-url]: https://david-dm.org/ljharb/object.assign#info=devDependencies
|
---|
129 | [testling-png]: https://ci.testling.com/ljharb/object.assign.png
|
---|
130 | [testling-url]: https://ci.testling.com/ljharb/object.assign
|
---|
131 | [npm-badge-png]: https://nodei.co/npm/object.assign.png?downloads=true&stars=true
|
---|
132 | [license-image]: http://img.shields.io/npm/l/object.assign.svg
|
---|
133 | [license-url]: LICENSE
|
---|
134 | [downloads-image]: http://img.shields.io/npm/dm/object.assign.svg
|
---|
135 | [downloads-url]: http://npm-stat.com/charts.html?package=object.assign
|
---|