Skip to content

Commit 7f191f7

Browse files
committed
Initial implementation, tests, readme
1 parent cb52829 commit 7f191f7

23 files changed

+488
-5
lines changed

.eslintrc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
6+
"rules": {
7+
"id-length": 0,
8+
"new-cap": [2, {
9+
"capIsNewExceptions": [
10+
"DeletePropertyOrThrow",
11+
"Get",
12+
"HasProperty",
13+
"LengthOfArrayLike",
14+
"Set",
15+
"ToObject",
16+
"ToString",
17+
]
18+
}],
19+
"no-magic-numbers": 0,
20+
},
21+
22+
"overrides": [
23+
{
24+
"files": "test/**",
25+
"rules": {
26+
"max-len": 0,
27+
},
28+
},
29+
],
30+
}

.github/FUNDING.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: [ljharb]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: npm/array.prototype.reverse
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.github/workflows/node-aught.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Tests: node.js < 10'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/node.yml@main
8+
with:
9+
range: '< 10'
10+
type: minors
11+
command: npm run tests-only
12+
13+
node:
14+
name: 'node < 10'
15+
needs: [tests]
16+
runs-on: ubuntu-latest
17+
steps:
18+
- run: 'echo tests completed'

.github/workflows/node-pretest.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Tests: pretest/posttest'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/pretest.yml@main

.github/workflows/node-tens.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: 'Tests: node.js 10 - 20'
2+
3+
on: [pull_request, push]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
tests:
10+
uses: ljharb/actions/.github/workflows/node.yml@main
11+
with:
12+
range: '>= 10 < 20'
13+
type: minors
14+
command: npm run tests-only
15+
16+
node:
17+
name: 'node 10 - 20'
18+
needs: [tests]
19+
runs-on: ubuntu-latest
20+
steps:
21+
- run: true

.github/workflows/node-twenties.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: 'Tests: node.js >= 20'
2+
3+
on: [pull_request, push]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
tests:
10+
uses: ljharb/actions/.github/workflows/node.yml@main
11+
with:
12+
range: '>= 20'
13+
type: minors
14+
command: npm run tests-only
15+
16+
node:
17+
name: 'node >= 20'
18+
needs: [tests]
19+
runs-on: ubuntu-latest
20+
steps:
21+
- run: true

.github/workflows/rebase.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Automatic Rebase
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
uses: ljharb/actions/.github/workflows/rebase.yml@main
8+
secrets:
9+
token: ${{ secrets.GITHUB_TOKEN }}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Require “Allow Edits”
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
name: "Require “Allow Edits”"
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: ljharb/require-allow-edits@main

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ dist
133133
npm-shrinkwrap.json
134134
package-lock.json
135135
yarn.lock
136+
137+
.npmignore

.npmrc

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
package-lock=false
2+
allow-same-version=true
3+
message=v%s

.nycrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"all": true,
3+
"check-coverage": false,
4+
"reporter": ["text-summary", "text", "html", "json"],
5+
"exclude": [
6+
"coverage",
7+
"test"
8+
]
9+
}

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

README.md

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

auto.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
require('./shim')();

implementation.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var DeletePropertyOrThrow = require('es-abstract/2024/DeletePropertyOrThrow');
4+
var floor = require('es-abstract/2024/floor');
5+
var Get = require('es-abstract/2024/Get');
6+
var HasProperty = require('es-abstract/2024/HasProperty');
7+
var LengthOfArrayLike = require('es-abstract/2024/LengthOfArrayLike');
8+
var Set = require('es-abstract/2024/Set');
9+
var ToObject = require('es-object-atoms/ToObject');
10+
var ToString = require('es-abstract/2024/ToString');
11+
12+
var $TypeError = require('es-errors/type');
13+
14+
module.exports = function reverse() {
15+
var O = ToObject(this); // step 1
16+
var len = LengthOfArrayLike(O); // step 2
17+
18+
var middle = floor(len / 2); // step 3
19+
20+
var lower = 0; // step 4
21+
22+
while (lower !== middle) { // step 5
23+
var upper = len - lower - 1; // step 5.a
24+
var upperP = ToString(upper); // step 5.b
25+
var lowerP = ToString(lower); // step 5.c
26+
var lowerExists = HasProperty(O, lowerP); // step 5.d
27+
if (lowerExists) { // step 5.e
28+
var lowerValue = Get(O, lowerP); // step 5.e.i
29+
}
30+
var upperExists = HasProperty(O, upperP); // step 5.f
31+
if (upperExists) { // step 5.g
32+
var upperValue = Get(O, upperP); // step 5.g.i
33+
}
34+
if (lowerExists && upperExists) { // step 5.h
35+
Set(O, lowerP, upperValue, true); // step 5.h.i
36+
Set(O, upperP, lowerValue, true); // step 5.h.ii
37+
} else if (!lowerExists && upperExists) { // step 5.i
38+
Set(O, lowerP, upperValue, true); // step 5.i.i
39+
DeletePropertyOrThrow(O, upperP); // step 5.i.ii
40+
} else if (lowerExists && !upperExists) { // step 5.j
41+
DeletePropertyOrThrow(O, lowerP); // step 5.j.i
42+
Set(O, upperP, lowerValue, true); // step 5.j.ii
43+
} else { // step 5.k
44+
// istanbul ignore if
45+
if (lowerExists || upperExists) { // eslint-disable-line no-lonely-if
46+
throw new $TypeError('Assertion failed: this should be unreachable'); // step 5.k.i
47+
}
48+
// NOTE: No action is required. // step 5.k.ii
49+
}
50+
lower += 1; // step 5.l
51+
}
52+
return O; // step 6
53+
};

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var define = require('define-properties');
4+
var callBind = require('call-bind');
5+
6+
var implementation = require('./implementation');
7+
var getPolyfill = require('./polyfill');
8+
var shim = require('./shim');
9+
10+
var bound = callBind(getPolyfill());
11+
define(bound, {
12+
getPolyfill: getPolyfill,
13+
implementation: implementation,
14+
shim: shim
15+
});
16+
17+
module.exports = bound;

package.json

+62-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
{
22
"name": "array.prototype.reverse",
33
"version": "0.0.0",
4-
"description": "Array.prototype.reverse spec-compliant polyfill",
4+
"description": "An ES5 spec-compliant `Array.prototype.reverse` shim/polyfill/replacement that works as far down as ES3.",
55
"main": "index.js",
66
"exports": {
7+
".": "./index.js",
8+
"./auto": "./auto.js",
9+
"./polyfill": "./polyfill.js",
10+
"./implementation": "./implementation.js",
11+
"./shim": "./shim.js",
712
"./package.json": "./package.json"
813
},
914
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"prepack": "npmignore --auto --commentLines=autogenerated",
16+
"prepublish": "not-in-publish || npm run prepublishOnly",
17+
"prepublishOnly": "safe-publish-latest",
18+
"preversion": "auto-changelog",
19+
"prelint": "evalmd README.md",
20+
"lint": "eslint --ext=js,mjs .",
21+
"postlint": "es-shim-api --bound",
22+
"pretest": "npm run lint",
23+
"test": "npm run tests-only",
24+
"posttest": "npx npm@\">= 10.2\" audit --production",
25+
"tests-only": "nyc tape 'test/**/*.js'",
26+
"version": "auto-changelog && git add CHANGELOG.md",
27+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
1128
},
1229
"repository": {
1330
"type": "git",
@@ -19,13 +36,55 @@
1936
"polyfill",
2037
"shim",
2138
"array",
39+
"Array.prototype.reverse",
2240
"reverse",
41+
"ES5",
2342
"es-shim API"
2443
],
2544
"author": "Jordan Harband <[email protected]>",
45+
"funding": {
46+
"url": "https://github.com/sponsors/ljharb"
47+
},
2648
"license": "MIT",
2749
"bugs": {
2850
"url": "https://github.com/es-shims/Array.prototype.reverse/issues"
2951
},
30-
"homepage": "https://github.com/es-shims/Array.prototype.reverse#readme"
52+
"homepage": "https://github.com/es-shims/Array.prototype.reverse#readme",
53+
"dependencies": {
54+
"call-bind": "^1.0.8",
55+
"define-properties": "^1.2.1",
56+
"es-abstract": "^1.23.9",
57+
"es-errors": "^1.3.0",
58+
"es-object-atoms": "^1.1.1"
59+
},
60+
"devDependencies": {
61+
"@es-shims/api": "^2.5.1",
62+
"@ljharb/eslint-config": "^21.1.1",
63+
"auto-changelog": "^2.5.0",
64+
"eslint": "=8.8.0",
65+
"evalmd": "^0.0.19",
66+
"functions-have-names": "^1.2.3",
67+
"has-strict-mode": "^1.1.0",
68+
"in-publish": "^2.0.1",
69+
"npmignore": "^0.3.1",
70+
"nyc": "^10.3.2",
71+
"safe-publish-latest": "^2.0.0",
72+
"tape": "^5.9.0"
73+
},
74+
"engines": {
75+
"node": ">= 0.4"
76+
},
77+
"auto-changelog": {
78+
"output": "CHANGELOG.md",
79+
"template": "keepachangelog",
80+
"unreleased": false,
81+
"commitLimit": false,
82+
"backfillLimit": false,
83+
"hideCredit": true
84+
},
85+
"publishConfig": {
86+
"ignore": [
87+
".github/workflows"
88+
]
89+
}
3190
}

0 commit comments

Comments
 (0)