Skip to content

Commit 6ec8c34

Browse files
committed
Initial implementation, tests, readme
1 parent 0d59dc7 commit 6ec8c34

23 files changed

+516
-9
lines changed

.eslintrc

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
6+
"globals": {
7+
"Float16Array": false,
8+
"Float32Array": false,
9+
"Float64Array": false,
10+
"Int16Array": false,
11+
"Int32Array": false,
12+
"Int8Array": false,
13+
"Uint16Array": false,
14+
"Uint32Array": false,
15+
"Uint8Array": false,
16+
"Uint8ClampedArray": false,
17+
"BigInt64Array": false,
18+
"BigUint64Array": false,
19+
},
20+
21+
"rules": {
22+
"func-style": 0,
23+
"id-length": 0,
24+
"new-cap": [2, {
25+
"capIsNewExceptions": [
26+
"Get",
27+
"ToIntegerOrInfinity",
28+
"ToString",
29+
"TypedArrayLength",
30+
"ValidateTypedArray",
31+
],
32+
}],
33+
"no-magic-numbers": 0,
34+
},
35+
36+
"overrides": [
37+
{
38+
"files": "test/**",
39+
"rules": {
40+
"max-len": 0,
41+
"new-cap": [2, {
42+
"capIsNewExceptions": [
43+
"Z",
44+
],
45+
}],
46+
},
47+
},
48+
],
49+
}

.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/typedarray.prototype.at
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

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

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var Get = require('es-abstract/2024/Get');
4+
var ToIntegerOrInfinity = require('es-abstract/2024/ToIntegerOrInfinity');
5+
var ToString = require('es-abstract/2024/ToString');
6+
var TypedArrayLength = require('es-abstract/2024/TypedArrayLength');
7+
var ValidateTypedArray = require('es-abstract/2024/ValidateTypedArray');
8+
9+
module.exports = function at(index) {
10+
var O = this; // step 1
11+
12+
var taRecord = ValidateTypedArray(O, 'SEQ-CST'); // step 2
13+
14+
var len = TypedArrayLength(taRecord); // step 3
15+
16+
var relativeIndex = ToIntegerOrInfinity(index); // step 4
17+
18+
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex; // step 5-6
19+
20+
if (k < 0 || k >= len) {
21+
return void undefined; // step 7
22+
}
23+
24+
return Get(O, ToString(k)); // step 8
25+
};

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var callBind = require('call-bind');
4+
5+
var getPolyfill = require('./polyfill');
6+
7+
module.exports = callBind(getPolyfill());

package.json

+68-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
{
22
"name": "typedarray.prototype.at",
33
"version": "0.0.0",
4-
"description": "An ES5 spec-compliant `TypedArray.prototype.at` shim/polyfill/replacement that works as far down as ES3",
4+
"description": "An ES5 spec-compliant `%TypedArray%.prototype.at` 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 --type=method",
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",
@@ -16,17 +33,61 @@
1633
"keywords": [
1734
"ecmascript",
1835
"javascript",
19-
"shim.polyfill",
36+
"polyfill",
37+
"shim",
2038
"TypedArray",
21-
"at",
2239
"TypedArray.prototype.at",
23-
"es-shim",
24-
"API"
40+
"at",
41+
"ES5",
42+
"es-shim API"
2543
],
2644
"author": "Jordan Harband <[email protected]>",
45+
"funding": {
46+
"url": "https://github.com/sponsors/ljharb"
47+
},
2748
"license": "MIT",
2849
"bugs": {
2950
"url": "https://github.com/es-shims/TypedArray.prototype.at/issues"
3051
},
31-
"homepage": "https://github.com/es-shims/TypedArray.prototype.at#readme"
52+
"homepage": "https://github.com/es-shims/TypedArray.prototype.at#readme",
53+
"dependencies": {
54+
"call-bind": "^1.0.8",
55+
"define-properties": "^1.2.1",
56+
"es-abstract": "^1.23.9",
57+
"get-proto": "^1.0.1"
58+
},
59+
"devDependencies": {
60+
"@es-shims/api": "^3.0.2",
61+
"@ljharb/eslint-config": "^21.1.1",
62+
"auto-changelog": "^2.5.0",
63+
"encoding": "^0.1.13",
64+
"es-value-fixtures": "^1.7.1",
65+
"eslint": "=8.8.0",
66+
"evalmd": "^0.0.19",
67+
"for-each": "^0.3.5",
68+
"functions-have-names": "^1.2.3",
69+
"has-strict-mode": "^1.1.0",
70+
"in-publish": "^2.0.1",
71+
"npmignore": "^0.3.1",
72+
"nyc": "^10.3.2",
73+
"object-inspect": "^1.13.4",
74+
"safe-publish-latest": "^2.0.0",
75+
"tape": "^5.9.0"
76+
},
77+
"engines": {
78+
"node": ">= 0.4"
79+
},
80+
"auto-changelog": {
81+
"output": "CHANGELOG.md",
82+
"template": "keepachangelog",
83+
"unreleased": false,
84+
"commitLimit": false,
85+
"backfillLimit": false,
86+
"hideCredit": true
87+
},
88+
"publishConfig": {
89+
"ignore": [
90+
".github/workflows"
91+
]
92+
}
3293
}

0 commit comments

Comments
 (0)