chushihua

This commit is contained in:
li
2026-01-26 23:20:48 +08:00
commit 7b5aab0206
22521 changed files with 3300090 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
root = true
[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 150
[CHANGELOG.md]
indent_style = space
indent_size = 2
[*.json]
max_line_length = off
[Makefile]
max_line_length = off
+37
View File
@@ -0,0 +1,37 @@
{
"root": true,
"extends": "@ljharb",
"rules": {
"array-bracket-newline": 0,
"id-length": 0,
"new-cap": [2, {
"capIsNewExceptions": [
"ArraySpeciesCreate",
"Call",
"CreateDataPropertyOrThrow",
"Get",
"HasProperty",
"IsCallable",
"LengthOfArrayLike",
"RequireObjectCoercible",
"ToObject",
"ToString",
],
}],
"no-magic-numbers": 0,
},
"overrides": [
{
"files": "test/**",
"rules": {
"max-lines-per-function": 0,
"max-params": 0,
"no-invalid-this": 1,
"strict": 0,
},
},
],
}
+9
View File
@@ -0,0 +1,9 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"exclude": [
"coverage",
"test"
]
}
+96
View File
@@ -0,0 +1,96 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.0.5](https://github.com/es-shims/Array.prototype.reduce/compare/v1.0.4...v1.0.5) - 2022-11-03
### Commits
- [meta] use `npmignore` to autogenerate an npmignore file [`a2333c6`](https://github.com/es-shims/Array.prototype.reduce/commit/a2333c6370c478238b72d01d2b53b423ebb321c5)
- [Deps] update `es-abstract` [`86a838f`](https://github.com/es-shims/Array.prototype.reduce/commit/86a838f1faebb4fc6d2f7269fa186535121fd270)
- [actions] update rebase action to use reusable workflow [`4710394`](https://github.com/es-shims/Array.prototype.reduce/commit/4710394340a9cfe26758971559fd4b7b523827c5)
- [Dev Deps] update `aud`, `tape` [`e2bcb57`](https://github.com/es-shims/Array.prototype.reduce/commit/e2bcb57270484bbfdbb25d8d9d9a880061cf8e84)
- [Deps] update `define-properties`, `es-abstract` [`4c693aa`](https://github.com/es-shims/Array.prototype.reduce/commit/4c693aa96a66c374489afc2c0b2e8c0f433a0efe)
- [Deps] update `es-abstract` [`ca5557c`](https://github.com/es-shims/Array.prototype.reduce/commit/ca5557ce5a7b8b71a079687383e58f21c566bc6d)
- [Dev Deps] update `functions-have-names` [`dcb2d35`](https://github.com/es-shims/Array.prototype.reduce/commit/dcb2d3577b1a489d5b69e35b2e878dc4a3a242e0)
## [v1.0.4](https://github.com/es-shims/Array.prototype.reduce/compare/v1.0.3...v1.0.4) - 2022-04-11
### Commits
- [actions] reuse common workflows [`42fe1e2`](https://github.com/es-shims/Array.prototype.reduce/commit/42fe1e2f8182227dc46c8a4af82fee0ee949144b)
- [Fix] properly handle a large negative length [`b24d3c4`](https://github.com/es-shims/Array.prototype.reduce/commit/b24d3c47b1ddbd2296000c4483ad6c987127df90)
- [Fix] properly handle only-holes array [`823a890`](https://github.com/es-shims/Array.prototype.reduce/commit/823a89004496524c8915fc436b4c6279031db7fa)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `@es-shims/api`, `safe-publish-latest`, `tape` [`235ae27`](https://github.com/es-shims/Array.prototype.reduce/commit/235ae27d0561122eb771095a2f791bf024b12985)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `auto-changelog`, `tape` [`7f720c6`](https://github.com/es-shims/Array.prototype.reduce/commit/7f720c66825ce7f5d1e8c21efc051d834fc9da00)
- [actions] update codecov uploader [`9d6eff0`](https://github.com/es-shims/Array.prototype.reduce/commit/9d6eff0e66c6d61db869e2fb7eb6c1f764f8dfd8)
- [Deps] update `es-abstract` [`c14d421`](https://github.com/es-shims/Array.prototype.reduce/commit/c14d421406593cc4208af26787b830c83a014bb7)
- [Deps] update `es-abstract` [`5288168`](https://github.com/es-shims/Array.prototype.reduce/commit/528816886ba56b5a0dabffa389708c1ecde09a18)
## [v1.0.3](https://github.com/es-shims/Array.prototype.reduce/compare/v1.0.2...v1.0.3) - 2021-10-01
### Commits
- [actions] use `node/install` instead of `node/run`; use `codecov` action [`bbd7913`](https://github.com/es-shims/Array.prototype.reduce/commit/bbd791365c6aaaf9bf65ea408fa24145dae37e96)
- [Deps] update `es-abstract` [`6f05f53`](https://github.com/es-shims/Array.prototype.reduce/commit/6f05f530335550a8b05d1a7e8b9ff9f9a1f36fa2)
- [readme] remove travis badge [`fc5c01c`](https://github.com/es-shims/Array.prototype.reduce/commit/fc5c01cb4168a7478b64f07d79dec2cd3d72cb30)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `@es-shims/api`, `aud`, `auto-changelog`, `tape` [`7eb4251`](https://github.com/es-shims/Array.prototype.reduce/commit/7eb4251db41fc6350fcc8b4e45fe0161a8d43c4b)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `functions-have-names`, `has-strict-mode`, `tape` [`b6d02ba`](https://github.com/es-shims/Array.prototype.reduce/commit/b6d02ba6579bef59150af4d37d867a8e3e6d74f9)
- [actions] update workflows [`403b142`](https://github.com/es-shims/Array.prototype.reduce/commit/403b1423722ebcf236d8b0f34d9e8cd4f3820221)
- [readme] add github actions/codecov badges [`e070a24`](https://github.com/es-shims/Array.prototype.reduce/commit/e070a2455b14ac47ef133aca8119e7cbd114f2b8)
- [actions] update workflows [`9657169`](https://github.com/es-shims/Array.prototype.reduce/commit/96571694e39f29a70d4ab222715096b21dd0359f)
- [Dep] move `is-string` to prod deps [`1eb89d6`](https://github.com/es-shims/Array.prototype.reduce/commit/1eb89d620d4493988824aade117f3f66c42a82c7)
- [Dev Deps] update `eslint`, `tape` [`70049c6`](https://github.com/es-shims/Array.prototype.reduce/commit/70049c65f794931d586b8cbe07b722048f457211)
- [Deps] update `call-bind`, `es-abstract` [`e49aea9`](https://github.com/es-shims/Array.prototype.reduce/commit/e49aea9752701d79fbc8606a172fb7dbc4d179cc)
- [meta] use `prepublishOnly` script for npm 7+ [`e2bbb8b`](https://github.com/es-shims/Array.prototype.reduce/commit/e2bbb8b440389573b900b0c084b5149fb8a487e7)
- [Tests] increase coverage [`4b9b02c`](https://github.com/es-shims/Array.prototype.reduce/commit/4b9b02ca6d5ebca08c68442ec410252ca7403fac)
- [Deps] update `es-abstract` [`e349e01`](https://github.com/es-shims/Array.prototype.reduce/commit/e349e0133314f6d2443ca7cc6451f0403fa147c0)
## [v1.0.2](https://github.com/es-shims/Array.prototype.reduce/compare/v1.0.1...v1.0.2) - 2020-11-19
### Commits
- [Tests] migrate tests to Github Actions [`397cded`](https://github.com/es-shims/Array.prototype.reduce/commit/397cded4df1918965948f178060a9842d91f11ed)
- [meta] do not publish github action workflow files [`7ac0e80`](https://github.com/es-shims/Array.prototype.reduce/commit/7ac0e80845bf9dcf97d36ebe0fea1f11f4faf0ff)
- [Deps] update `es-abstract`; use `call-bind` where applicable; remove `function-bind` [`3470ed1`](https://github.com/es-shims/Array.prototype.reduce/commit/3470ed1ed6474c92dcc547929ab101322de9ee12)
- [Tests] run `nyc` on all tests [`6efa838`](https://github.com/es-shims/Array.prototype.reduce/commit/6efa838535903d2260b46bcc73a7e0c8c8c22e18)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `auto-changelog`, `tape` [`eae319e`](https://github.com/es-shims/Array.prototype.reduce/commit/eae319ef283bad89ab807e73c931eb714ce06279)
- [actions] add "Allow Edits" workflow [`87ed4f8`](https://github.com/es-shims/Array.prototype.reduce/commit/87ed4f86ea68a88bd6e4b72c5ee6cccf2ac6ce02)
- [Tests] add `implementation` test; run `es-shim-api` in postlint; use `tape` runner [`9bfea52`](https://github.com/es-shims/Array.prototype.reduce/commit/9bfea523756dbd8dc9308916145f8d3a2fe46103)
- [Dev Deps] update `@ljharb/eslint-config`, `tape`, `has-strict-mode`, `functions-have-names` [`fdac028`](https://github.com/es-shims/Array.prototype.reduce/commit/fdac028b1e46873180cfae102d409d148fb3e6f7)
- [Dev Deps] update `auto-changelog`, `tape` [`f595f6a`](https://github.com/es-shims/Array.prototype.reduce/commit/f595f6a31eabeef2d92895855c818507fe6e0219)
- [Dev Deps] update `auto-changelog`; add `aud` [`9d3d84c`](https://github.com/es-shims/Array.prototype.reduce/commit/9d3d84c030f0350f5e98bd320ead76392b252240)
- [actions] switch Automatic Rebase workflow to `pull_request_target` event [`2c05596`](https://github.com/es-shims/Array.prototype.reduce/commit/2c055961d27ebcde19f5dde83e4e14eab945b924)
- [Tests] only audit prod deps [`89c3610`](https://github.com/es-shims/Array.prototype.reduce/commit/89c36102d528b1b17a56cf7ef0395cec83196e6f)
- [Deps] update `es-abstract` [`65bdd68`](https://github.com/es-shims/Array.prototype.reduce/commit/65bdd68d942a2d82e9c5548c25944b2bebb9a52d)
- [Deps] update `es-abstract` [`ad19dd9`](https://github.com/es-shims/Array.prototype.reduce/commit/ad19dd9a1008a05381bab87b79444cc93b28ac3b)
- [Dev Deps] update `eslint` [`ce28412`](https://github.com/es-shims/Array.prototype.reduce/commit/ce28412065431e3dc21010e6c7c981739676161e)
## [v1.0.1](https://github.com/es-shims/Array.prototype.reduce/compare/v1.0.0...v1.0.1) - 2019-12-20
### Commits
- [Tests] use shared travis-ci configs [`cc10c0e`](https://github.com/es-shims/Array.prototype.reduce/commit/cc10c0e89f27b1f1985ad927a98eb8f61f548645)
- [Refactor] use split-up `es-abstract` (77% bundle size decrease) [`1d34e2b`](https://github.com/es-shims/Array.prototype.reduce/commit/1d34e2b786eef0a699d15c2ade3afbbcd79b697a)
- [meta] update `auto-changelog` [`0d4c1a7`](https://github.com/es-shims/Array.prototype.reduce/commit/0d4c1a77ceb359ea57c6f4721bd2ac27029837f7)
- [actions] add automatic rebasing / merge commit blocking [`3c5a9aa`](https://github.com/es-shims/Array.prototype.reduce/commit/3c5a9aa612533f9288f092c48559100f0c7c0d51)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `is-string`, `tape` [`37b79b1`](https://github.com/es-shims/Array.prototype.reduce/commit/37b79b12e55fa1751a3d50e17822d85039c7d51d)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `safe-publish-latest`, `functions-have-names` [`f5c5499`](https://github.com/es-shims/Array.prototype.reduce/commit/f5c5499e6c41fd318b7e7149b240bdbb36e3542d)
- [Deps] update `es-abstract` [`bd187af`](https://github.com/es-shims/Array.prototype.reduce/commit/bd187af654f13a6f2a761e6919a35aa8e21ba0fb)
- [meta] add `funding` field [`8992db6`](https://github.com/es-shims/Array.prototype.reduce/commit/8992db65687dd6a0cb2a84feafe0405e564a4c1a)
- [Deps] update `es-abstract` [`eda5044`](https://github.com/es-shims/Array.prototype.reduce/commit/eda50449d54ab75b01754b61b72d51d1ca9a4dc7)
## v1.0.0 - 2019-09-30
### Commits
- Tests [`49c8a44`](https://github.com/es-shims/Array.prototype.reduce/commit/49c8a449b43cb6cc2784ff77cf10a1fee819e14f)
- Implementation [`877b95a`](https://github.com/es-shims/Array.prototype.reduce/commit/877b95a04f941d18dbc6081e026a49e14950a151)
- Initial commit [`06b97be`](https://github.com/es-shims/Array.prototype.reduce/commit/06b97bed9f8b2da62133d65449b6dc86bf0db0ee)
- readme [`1049385`](https://github.com/es-shims/Array.prototype.reduce/commit/1049385c90a912bd5196f5ecf2efb6e2ee942c0b)
- npm init [`57807e8`](https://github.com/es-shims/Array.prototype.reduce/commit/57807e8507a732cc0539e5f8154571b6952e30a4)
- [meta] add `auto-changelog`, `safe-publish-latest` [`74a5ff3`](https://github.com/es-shims/Array.prototype.reduce/commit/74a5ff39b29cd01ae6b25f32d2629bb53fef8380)
- Only apps should have lockfiles [`50da8f2`](https://github.com/es-shims/Array.prototype.reduce/commit/50da8f2127c825bdebbac921f38237fd495f5b65)
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Jordan Harband
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+68
View File
@@ -0,0 +1,68 @@
# array.prototype.reduce <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![npm badge][npm-badge-png]][package-url]
An ES5 spec-compliant `Array.prototype.reduce` shim/polyfill/replacement that works as far down as ES3.
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/).
Because `Array.prototype.reduce` depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.
## Example
```js
var reduce = require('array.prototype.reduce');
var assert = require('assert');
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }), 6);
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }, 1), 7);
```
```js
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is not present */
delete Array.prototype.reduce;
var shimmed = reduce.shim();
assert.equal(shimmed, reduce.getPolyfill());
var arr = [1, 2, 3];
var sum = function (a, b) { return a + b; };
assert.equal(arr.reduce(sum), reduce(arr, sum));
assert.equal(arr.reduce(sum), 6);
assert.equal(arr.reduce(sum, 1), 7);
```
```js
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is present */
var shimmed = reduce.shim();
assert.equal(shimmed, Array.prototype.reduce);
assert.equal(arr.reduce(sum), reduce(arr, sum));
```
## Tests
Simply clone the repo, `npm install`, and run `npm test`
[package-url]: https://npmjs.org/package/array.prototype.reduce
[npm-version-svg]: https://versionbadg.es/es-shims/Array.prototype.reduce.svg
[deps-svg]: https://david-dm.org/es-shims/Array.prototype.reduce.svg
[deps-url]: https://david-dm.org/es-shims/Array.prototype.reduce
[dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.reduce/dev-status.svg
[dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.reduce#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/array.prototype.reduce.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/array.prototype.reduce.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/array.prototype.reduce.svg
[downloads-url]: https://npm-stat.com/charts.html?package=array.prototype.reduce
[codecov-image]: https://codecov.io/gh/es-shims/Array.prototype.reduce/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/es-shims/Array.prototype.reduce/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Array.prototype.reduce
[actions-url]: https://github.com/es-shims/Array.prototype.reduce/actions
+3
View File
@@ -0,0 +1,3 @@
'use strict';
require('./shim')();
+67
View File
@@ -0,0 +1,67 @@
'use strict';
var Call = require('es-abstract/2022/Call');
var Get = require('es-abstract/2022/Get');
var HasProperty = require('es-abstract/2022/HasProperty');
var IsCallable = require('es-abstract/2022/IsCallable');
var LengthOfArrayLike = require('es-abstract/2022/LengthOfArrayLike');
var ToObject = require('es-abstract/2022/ToObject');
var ToString = require('es-abstract/2022/ToString');
var callBound = require('call-bind/callBound');
var isString = require('is-string');
var $TypeError = TypeError;
// Check failure of by-index access of string characters (IE < 9) and failure of `0 in boxedString` (Rhino)
var boxedString = Object('a');
var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
var strSplit = callBound('%String.prototype.split%');
module.exports = function reduce(callbackfn) {
var O = ToObject(this);
var self = splitString && isString(O) ? strSplit(O, '') : O;
var len = LengthOfArrayLike(self);
// If no callback function or if callback is not a callable function
if (!IsCallable(callbackfn)) {
throw new $TypeError('Array.prototype.reduce callback must be a function');
}
if (len === 0 && arguments.length < 2) {
throw new $TypeError('reduce of empty array with no initial value');
}
var k = 0;
var accumulator;
var Pk, kPresent;
if (arguments.length > 1) {
accumulator = arguments[1];
} else {
kPresent = false;
while (!kPresent && k < len) {
Pk = ToString(k);
kPresent = HasProperty(O, Pk);
if (kPresent) {
accumulator = Get(O, Pk);
}
k += 1;
}
if (!kPresent) {
throw new $TypeError('reduce of empty array with no initial value');
}
}
while (k < len) {
Pk = ToString(k);
kPresent = HasProperty(O, Pk);
if (kPresent) {
var kValue = Get(O, Pk);
accumulator = Call(callbackfn, void undefined, [accumulator, kValue, k, O]);
}
k += 1;
}
return accumulator;
};
+28
View File
@@ -0,0 +1,28 @@
'use strict';
var define = require('define-properties');
var RequireObjectCoercible = require('es-abstract/2022/RequireObjectCoercible');
var callBind = require('call-bind');
var callBound = require('call-bind/callBound');
var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var polyfill = callBind.apply(getPolyfill());
var shim = require('./shim');
var $slice = callBound('%Array.prototype.slice%');
// eslint-disable-next-line no-unused-vars
var boundShim = function reduce(array, callbackfn) {
RequireObjectCoercible(array);
return polyfill(array, $slice(arguments, 1));
};
define(boundShim, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});
module.exports = boundShim;
+84
View File
@@ -0,0 +1,84 @@
{
"name": "array.prototype.reduce",
"version": "1.0.5",
"description": "An ES5 spec-compliant `Array.prototype.reduce` shim/polyfill/replacement that works as far down as ES3.",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"",
"prelint": "evalmd README.md",
"lint": "eslint --ext=js,mjs .",
"postlint": "es-shim-api --bound",
"pretest": "npm run lint",
"test": "npm run tests-only",
"posttest": "aud --production",
"tests-only": "nyc tape 'test/**/*.js'",
"testling": "npx testling --html > test.html"
},
"repository": {
"type": "git",
"url": "git+https://github.com/es-shims/Array.prototype.reduce.git"
},
"keywords": [
"Array.prototype.reduce",
"reduce",
"array",
"ES5",
"shim",
"polyfill",
"es-shim API"
],
"author": "Jordan Harband <ljharb@gmail.com>",
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/es-shims/Array.prototype.reduce/issues"
},
"homepage": "https://github.com/es-shims/Array.prototype.reduce#readme",
"engines": {
"node": ">= 0.4"
},
"dependencies": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.4",
"es-abstract": "^1.20.4",
"es-array-method-boxes-properly": "^1.0.0",
"is-string": "^1.0.7"
},
"devDependencies": {
"@es-shims/api": "^2.2.3",
"@ljharb/eslint-config": "^21.0.0",
"aud": "^2.0.1",
"auto-changelog": "^2.4.0",
"eslint": "=8.8.0",
"evalmd": "^0.0.19",
"functions-have-names": "^1.2.3",
"has-strict-mode": "^1.0.1",
"in-publish": "^2.0.1",
"npmignore": "^0.3.0",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.6.1"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
},
"publishConfig": {
"ignore": [
".github/workflows"
]
}
}
+10
View File
@@ -0,0 +1,10 @@
'use strict';
var arrayMethodBoxesProperly = require('es-array-method-boxes-properly');
var implementation = require('./implementation');
module.exports = function getPolyfill() {
var method = Array.prototype.reduce;
return arrayMethodBoxesProperly(method) ? method : implementation;
};
+14
View File
@@ -0,0 +1,14 @@
'use strict';
var define = require('define-properties');
var getPolyfill = require('./polyfill');
module.exports = function shimArrayPrototypeReduce() {
var polyfill = getPolyfill();
define(
Array.prototype,
{ reduce: polyfill },
{ reduce: function () { return Array.prototype.reduce !== polyfill; } }
);
return polyfill;
};
+16
View File
@@ -0,0 +1,16 @@
var implementation = require('../implementation');
var callBind = require('call-bind');
var test = require('tape');
var runTests = require('./tests');
test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](callBind(implementation, null, undefined, 'a'), TypeError, 'undefined is not an object');
st['throws'](callBind(implementation, null, null, 'a'), TypeError, 'null is not an object');
st.end();
});
runTests(callBind(implementation), t);
t.end();
});
+18
View File
@@ -0,0 +1,18 @@
'use strict';
var implementation = require('../');
var callBind = require('call-bind');
var test = require('tape');
var runTests = require('./tests');
test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](callBind(implementation, null, undefined, 'a'), TypeError, 'undefined is not an object');
st['throws'](callBind(implementation, null, null, 'a'), TypeError, 'null is not an object');
st.end();
});
runTests(implementation, t);
t.end();
});
+35
View File
@@ -0,0 +1,35 @@
require('../auto');
var test = require('tape');
var defineProperties = require('define-properties');
var callBind = require('call-bind');
var isEnumerable = Object.prototype.propertyIsEnumerable;
var functionsHaveNames = require('functions-have-names')();
var runTests = require('./tests');
test('shimmed', function (t) {
t.equal(Array.prototype.reduce.length, 1, 'Array#reduce has a length of 1');
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(Array.prototype.reduce.name, 'reduce', 'Array#reduce has name "reduce"');
st.end();
});
t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(Array.prototype, 'reduce'), 'Array#reduce is not enumerable');
et.end();
});
var supportsStrictMode = (function () { return typeof this === 'undefined'; }());
t.test('bad array/this value', { skip: !supportsStrictMode }, function (st) {
st['throws'](function () { return Array.prototype.reduce.call(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { return Array.prototype.reduce.call(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});
runTests(callBind(Array.prototype.reduce), t);
t.end();
});
+218
View File
@@ -0,0 +1,218 @@
var hasStrictMode = require('has-strict-mode')();
var global = Function('return this')(); // eslint-disable-line no-new-func
var identity = function (x) { return x; };
var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false.
var undefinedIfNoSparseBug = canDistinguishSparseFromUndefined ? undefined : { valueOf: function () { return 0; } };
var createArrayLikeFromArray = function createArrayLike(arr) {
var o = {};
for (var i = 0; i < arr.length; i += 1) {
if (i in arr) {
o[i] = arr[i];
}
}
o.length = arr.length;
return o;
};
module.exports = function (reduce, t) {
t.test('passes the correct values to the callback', function (st) {
st.plan(7);
var expectedValue = {};
var initialValue = {};
var expectedResult = {};
var arr = [expectedValue];
var result = reduce(
arr,
function (accumulator, value, key, list) {
st.equal(arguments.length, 4);
st.equal(accumulator, initialValue, 'first argument is the accumulator');
st.equal(value, expectedValue, 'second argument is the value');
st.equal(key, 0, 'third argument is the index');
st.equal(list, arr, 'fourth argument is the array being iterated');
st.equal(this, global, 'sloppy: receiver is the expected value');
return expectedResult;
},
initialValue
);
st.equal(result, expectedResult, 'result is last return value of accumulator');
st.end();
});
t.test('strict mode callback receiver', { skip: !hasStrictMode }, function (st) {
reduce(
[null],
function () {
'use strict';
st.equal(this, undefined, 'strict: receiver is the expected value');
}
);
st.end();
});
t.test('starts with the right initialValue', function (st) {
var firstValue = {};
var secondValue = {};
reduce(
[firstValue, secondValue],
function (accumulator, value) {
st.equal(accumulator, firstValue, 'accumulator starts out as the first value when initialValue is omitted');
st.equal(value, secondValue, 'value starts out as the second value when initialValue is omitted');
}
);
reduce(
[secondValue],
function (accumulator, value) {
st.equal(accumulator, firstValue, 'accumulator starts out as the initialValue when provided');
st.equal(value, secondValue, 'value starts out as the first value when initialValue is provided');
},
firstValue
);
st.end();
});
t.test('does not visit elements added to the array after it has begun', function (st) {
st.plan(8);
var arr = [1, 2, 3];
var i = 0;
reduce(arr, function (acc, v) {
i += 1;
arr.push(v + 3);
});
st.deepEqual(arr, [1, 2, 3, 5, 6], 'array has received 3 new elements. initialValue omitted');
st.equal(i, 2, 'reduce callback only called twice');
i = 0;
arr = [1, 2, 3];
reduce(
arr,
function (acc, v) {
i += 1;
arr.push(v + 3);
},
null
);
st.deepEqual(arr, [1, 2, 3, 4, 5, 6], 'array has received 3 new elements. initialValue provided');
st.equal(i, 3, 'reduce callback only called thrice');
var arrayLike = createArrayLikeFromArray([1, 2, 3]);
i = 0;
reduce(arrayLike, function (acc, v) {
i += 1;
arrayLike[arrayLike.length] = v + 3;
arrayLike.length += 1;
});
st.deepEqual(Array.prototype.slice.call(arrayLike), [1, 2, 3, 5, 6], 'arrayLike has received 3 new elements. initialValue omitted');
st.equal(i, 2, 'reduce callback only called twice');
arrayLike = createArrayLikeFromArray([1, 2, 3]);
i = 0;
reduce(
arrayLike,
function (acc, v) {
i += 1;
arrayLike[arrayLike.length] = v + 3;
arrayLike.length += 1;
},
null
);
st.deepEqual(Array.prototype.slice.call(arrayLike), [1, 2, 3, 4, 5, 6], 'arrayLike has received 3 new elements. initialValue provided');
st.equal(i, 3, 'reduce callback only called thrice');
st.end();
});
t.test('empty array', function (st) {
var initialValue = {};
var actual = reduce([], identity, initialValue);
st.equal(actual, initialValue, 'empty array returns callback return');
st['throws'](
function () { reduce([], identity); },
TypeError,
'empty array with omitted initialValue throws'
);
var sparse = Array(10);
st['throws'](
function () { reduce(sparse, identity); },
TypeError,
'only-holes array with omitted initialValue throws (test262: 15.4.4.21-8-c-1)'
);
st.end();
});
t.test('skips holes', function (st) {
var arr = [1, undefinedIfNoSparseBug, 3];
var visited = {};
reduce(
arr,
function (a, b) {
if (a) { visited[a] = true; }
if (b) { visited[b] = true; }
return 0;
},
null
);
st.deepEqual(visited, { 1: true, 3: true }, 'only non-holes are visited; initialValue provided');
visited = {};
reduce(
arr,
function (a, b) {
if (a) { visited[a] = true; }
if (b) { visited[b] = true; }
return 0;
}
);
st.deepEqual(visited, { 1: true, 3: true }, 'only non-holes are visited; initialValue omitted');
st.end();
});
t.test('list arg boxing', function (st) {
st.plan(4);
reduce(
'f',
function (acc, item, index, list) {
st.equal(acc, null, 'accumulator matches');
st.equal(item, 'f', 'letter matches');
st.equal(typeof list, 'object', 'primitive list arg is boxed');
st.equal(Object.prototype.toString.call(list), '[object String]', 'boxed list arg is a String');
},
null
);
st.end();
});
t.test('test262: 15.4.4.21-3-12', function (st) {
var obj = {
1: 11,
2: 9,
length: '-4294967294'
};
var cb = function callbackfn(prevVal, curVal, idx, object) {
st.equal(object, obj, '4th argument is receiver');
return curVal === 11 && idx === 1;
};
st.equal(reduce(obj, cb, 1), 1, 'reduce(obj, callbackfn, 1)');
st.end();
});
};