You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.6 KiB

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