8c912218eff12c27689a3a48b06a9c4011f63b953ca5bb488c247fb77de909f44d4c0272632c634a4277017c25b709b2b6bb650975571021a8632b54563281 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # array.prototype.reduce <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-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. [![npm badge][npm-badge-png]][package-url]
  9. An ES5 spec-compliant `Array.prototype.reduce` shim/polyfill/replacement that works as far down as ES3.
  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](https://www.ecma-international.org/ecma-262/5.1/).
  11. Because `Array.prototype.reduce` depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.
  12. ## Example
  13. ```js
  14. var reduce = require('array.prototype.reduce');
  15. var assert = require('assert');
  16. assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }), 6);
  17. assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }, 1), 7);
  18. ```
  19. ```js
  20. var reduce = require('array.prototype.reduce');
  21. var assert = require('assert');
  22. /* when Array#reduce is not present */
  23. delete Array.prototype.reduce;
  24. var shimmed = reduce.shim();
  25. assert.equal(shimmed, reduce.getPolyfill());
  26. var arr = [1, 2, 3];
  27. var sum = function (a, b) { return a + b; };
  28. assert.equal(arr.reduce(sum), reduce(arr, sum));
  29. assert.equal(arr.reduce(sum), 6);
  30. assert.equal(arr.reduce(sum, 1), 7);
  31. ```
  32. ```js
  33. var reduce = require('array.prototype.reduce');
  34. var assert = require('assert');
  35. /* when Array#reduce is present */
  36. var shimmed = reduce.shim();
  37. assert.equal(shimmed, Array.prototype.reduce);
  38. assert.equal(arr.reduce(sum), reduce(arr, sum));
  39. ```
  40. ## Tests
  41. Simply clone the repo, `npm install`, and run `npm test`
  42. [package-url]: https://npmjs.org/package/array.prototype.reduce
  43. [npm-version-svg]: https://versionbadg.es/es-shims/Array.prototype.reduce.svg
  44. [deps-svg]: https://david-dm.org/es-shims/Array.prototype.reduce.svg
  45. [deps-url]: https://david-dm.org/es-shims/Array.prototype.reduce
  46. [dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.reduce/dev-status.svg
  47. [dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.reduce#info=devDependencies
  48. [npm-badge-png]: https://nodei.co/npm/array.prototype.reduce.png?downloads=true&stars=true
  49. [license-image]: https://img.shields.io/npm/l/array.prototype.reduce.svg
  50. [license-url]: LICENSE
  51. [downloads-image]: https://img.shields.io/npm/dm/array.prototype.reduce.svg
  52. [downloads-url]: https://npm-stat.com/charts.html?package=array.prototype.reduce
  53. [codecov-image]: https://codecov.io/gh/es-shims/Array.prototype.reduce/branch/main/graphs/badge.svg
  54. [codecov-url]: https://app.codecov.io/gh/es-shims/Array.prototype.reduce/
  55. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Array.prototype.reduce
  56. [actions-url]: https://github.com/es-shims/Array.prototype.reduce/actions