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