Skip to content

Commit

Permalink
[Breaking] rename package to at from item
Browse files Browse the repository at this point in the history
Per 2020.11.17 TC39
  • Loading branch information
ljharb committed Nov 18, 2020
1 parent 5fa8f8a commit ad5633a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 84 deletions.
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# array.prototype.item <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
# array.prototype.at <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

[![Build Status][travis-svg]][travis-url]
[![dependency status][deps-svg]][deps-url]
Expand All @@ -8,27 +8,27 @@

[![npm badge][npm-badge-png]][package-url]

An ESnext spec-compliant `Array.prototype.item` shim/polyfill/replacement that works as far down as ES3.
An ESnext spec-compliant `Array.prototype.at` 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 proposed [spec](https://tc39.es/proposal-item-method/).

Because `Array.prototype.item` depends on a receiver (the `this` value), the main export takes the array to operate on as the first argument.
Because `Array.prototype.at` depends on a receiver (the `this` value), the main export takes the array to operate on as the first argument.

## Getting started

```sh
npm install --save array.prototype.item
npm install --save array.prototype.at
```

## Usage/Examples

```js
var item = require('array.prototype.item');
var at = require('array.prototype.at');
var assert = require('assert');

var arr = [1, [2], [], 3];

var results = item(arr, function (x, i) {
var results = at(arr, function (x, i) {
assert.equal(x, arr[i]);
return x;
});
Expand All @@ -37,43 +37,43 @@ assert.deepEqual(results, [1, 2, 3]);
```

```js
var item = require('array.prototype.item');
var at = require('array.prototype.at');
var assert = require('assert');
/* when Array#item is not present */
delete Array.prototype.item;
var shimmedFlatMap = item.shim();
/* when Array#at is not present */
delete Array.prototype.at;
var shimmedFlatMap = at.shim();

var mapper = function (x) { return [x, 1]; };

assert.equal(shimmedFlatMap, item.getPolyfill());
assert.deepEqual(arr.item(mapper), item(arr, mapper));
assert.equal(shimmedFlatMap, at.getPolyfill());
assert.deepEqual(arr.at(mapper), at(arr, mapper));
```

```js
var item = require('array.prototype.item');
var at = require('array.prototype.at');
var assert = require('assert');
/* when Array#item is present */
var shimmedIncludes = item.shim();
/* when Array#at is present */
var shimmedIncludes = at.shim();

var mapper = function (x) { return [x, 1]; };

assert.equal(shimmedIncludes, Array.prototype.item);
assert.deepEqual(arr.item(mapper), item(arr, mapper));
assert.equal(shimmedIncludes, Array.prototype.at);
assert.deepEqual(arr.at(mapper), at(arr, mapper));
```

## Tests
Simply clone the repo, `npm install`, and run `npm test`

[package-url]: https://npmjs.org/package/array.prototype.item
[npm-version-svg]: http://versionbadg.es/es-shims/Array.prototype.item.svg
[travis-svg]: https://travis-ci.org/es-shims/Array.prototype.item.svg
[travis-url]: https://travis-ci.org/es-shims/Array.prototype.item
[deps-svg]: https://david-dm.org/es-shims/Array.prototype.item.svg
[deps-url]: https://david-dm.org/es-shims/Array.prototype.item
[dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.item/dev-status.svg
[dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.item#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/array.prototype.item.png?downloads=true&stars=true
[license-image]: http://img.shields.io/npm/l/array.prototype.item.svg
[package-url]: https://npmjs.org/package/array.prototype.at
[npm-version-svg]: http://versionbadg.es/es-shims/Array.prototype.at.svg
[travis-svg]: https://travis-ci.org/es-shims/Array.prototype.at.svg
[travis-url]: https://travis-ci.org/es-shims/Array.prototype.at
[deps-svg]: https://david-dm.org/es-shims/Array.prototype.at.svg
[deps-url]: https://david-dm.org/es-shims/Array.prototype.at
[dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.at/dev-status.svg
[dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.at#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/array.prototype.at.png?downloads=true&stars=true
[license-image]: http://img.shields.io/npm/l/array.prototype.at.svg
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/array.prototype.item.svg
[downloads-url]: http://npm-stat.com/charts.html?package=array.prototype.item
[downloads-image]: http://img.shields.io/npm/dm/array.prototype.at.svg
[downloads-url]: http://npm-stat.com/charts.html?package=array.prototype.at
2 changes: 1 addition & 1 deletion implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var ToInteger = require('es-abstract/2020/ToInteger');
var ToObject = require('es-abstract/2020/ToObject');
var ToString = require('es-abstract/2020/ToString');

module.exports = function item(index) {
module.exports = function at(index) {
var O = ToObject(this);

var len = LengthOfArrayLike(O);
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ var getPolyfill = require('./polyfill');
var polyfill = getPolyfill();
var shim = require('./shim');

var boundItemShim = function item(array, index) {
var boundShim = function at(array, index) {
RequireObjectCoercible(array);
return polyfill.call(array, index);
};
define(boundItemShim, {
define(boundShim, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = boundItemShim;
module.exports = boundShim;
10 changes: 5 additions & 5 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import callBind from 'call-bind';
import RequireObjectCoercible from 'es-abstract/2020/RequireObjectCoercible.js';

import getPolyfill from 'array.prototype.item/polyfill';
import getPolyfill from 'array.prototype.at/polyfill';

const bound = callBind(getPolyfill());

export default function item(array, index) {
export default function at(array, index) {
RequireObjectCoercible(array);
return bound(array, index);
}

export { default as getPolyfill } from 'array.prototype.item/polyfill';
export { default as implementation } from 'array.prototype.item/implementation';
export { default as shim } from 'array.prototype.item/shim';
export { default as getPolyfill } from 'array.prototype.at/polyfill';
export { default as implementation } from 'array.prototype.at/implementation';
export { default as shim } from 'array.prototype.at/shim';
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "array.prototype.item",
"version": "1.0.0",
"description": "An ES-spec-compliant (proposed) Array.prototype.item shim/polyfill/replacement that works as far down as ES3",
"name": "array.prototype.at",
"version": "0.0.0",
"description": "An ES-spec-compliant (proposed) Array.prototype.at shim/polyfill/replacement that works as far down as ES3",
"main": "index.js",
"exports": {
".": [
Expand Down Expand Up @@ -52,12 +52,15 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/es-shims/Array.prototype.item.git"
"url": "git+https://github.com/es-shims/Array.prototype.at.git"
},
"keywords": [
"Array.prototype.at",
"Array.prototype.item",
"String.prototype.at",
"String.prototype.item",
"item",
"at",
"array",
"shim",
"polyfill",
Expand All @@ -70,9 +73,9 @@
},
"license": "MIT",
"bugs": {
"url": "https://github.com/es-shims/Array.prototype.item/issues"
"url": "https://github.com/es-shims/Array.prototype.at/issues"
},
"homepage": "https://github.com/es-shims/Array.prototype.item#readme",
"homepage": "https://github.com/es-shims/Array.prototype.at#readme",
"devDependencies": {
"@ljharb/eslint-config": "^17.2.0",
"aud": "^1.1.3",
Expand Down
2 changes: 1 addition & 1 deletion polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
var implementation = require('./implementation');

module.exports = function getPolyfill() {
return Array.prototype.item || implementation;
return Array.prototype.at || implementation;
};
6 changes: 3 additions & 3 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

module.exports = function shimArrayPrototypeItem() {
module.exports = function shimArrayPrototypeAt() {
var polyfill = getPolyfill();
define(
Array.prototype,
{ item: polyfill },
{ item: function () { return Array.prototype.item !== polyfill; } }
{ at: polyfill },
{ at: function () { return Array.prototype.at !== polyfill; } }
);
return polyfill;
};
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

var item = require('..');
var at = require('..');
var test = require('tape');
var runTests = require('./tests');

test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](item.bind(null, undefined, function () {}), TypeError, 'undefined is not an object');
st['throws'](item.bind(null, null, function () {}), TypeError, 'null is not an object');
st['throws'](at.bind(null, undefined, function () {}), TypeError, 'undefined is not an object');
st['throws'](at.bind(null, null, function () {}), TypeError, 'null is not an object');
st.end();
});

runTests(item, t);
runTests(at, t);

t.end();
});
20 changes: 10 additions & 10 deletions test/index.mjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import item from 'array.prototype.item';
import * as itemModule from 'array.prototype.item';
import at from 'array.prototype.at';
import * as atModule from 'array.prototype.at';
import test from 'tape';
import runTests from './tests.js';

test('as a function', (t) => {
t.test('bad array/this value', (st) => {
st.throws(() => item(undefined), TypeError, 'undefined is not an object');
st.throws(() => item(null), TypeError, 'null is not an object');
st.throws(() => at(undefined), TypeError, 'undefined is not an object');
st.throws(() => at(null), TypeError, 'null is not an object');
st.end();
});

runTests(item, t);
runTests(at, t);

t.end();
});

test('named exports', async (t) => {
t.deepEqual(
Object.keys(itemModule).sort(),
Object.keys(atModule).sort(),
['default', 'shim', 'getPolyfill', 'implementation'].sort(),
'has expected named exports'
);

const { shim, getPolyfill, implementation } = itemModule;
t.equal(await import('array.prototype.item/shim'), shim, 'shim named export matches deep export');
t.equal(await import('array.prototype.item/implementation'), implementation, 'implementation named export matches deep export');
t.equal(await import('array.prototype.item/polyfill'), getPolyfill, 'getPolyfill named export matches deep export');
const { shim, getPolyfill, implementation } = atModule;
t.equal(await import('array.prototype.at/shim'), shim, 'shim named export matches deep export');
t.equal(await import('array.prototype.at/implementation'), implementation, 'implementation named export matches deep export');
t.equal(await import('array.prototype.at/polyfill'), getPolyfill, 'getPolyfill named export matches deep export');

t.end();
});
12 changes: 6 additions & 6 deletions test/shimmed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ var hasStrictMode = require('has-strict-mode')();
var runTests = require('./tests');

test('shimmed', function (t) {
t.equal(Array.prototype.item.length, 1, 'Array#item has a length of 1');
t.equal(Array.prototype.at.length, 1, 'Array#at has a length of 1');
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(Array.prototype.item.name, 'item', 'Array#item has name "item"');
st.equal(Array.prototype.at.name, 'at', 'Array#at has name "at"');
st.end();
});

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(Array.prototype, 'item'), 'Array#item is not enumerable');
et.equal(false, isEnumerable.call(Array.prototype, 'at'), 'Array#at is not enumerable');
et.end();
});

t.test('bad array/this value', { skip: !hasStrictMode }, function (st) {
st['throws'](function () { return Array.prototype.item.call(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { return Array.prototype.item.call(null, 'a'); }, TypeError, 'null is not an object');
st['throws'](function () { return Array.prototype.at.call(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { return Array.prototype.at.call(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});

runTests(bind.call(Function.call, Array.prototype.item), t);
runTests(bind.call(Function.call, Array.prototype.at), t);

t.end();
});
32 changes: 16 additions & 16 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
'use strict';

module.exports = function (item, t) {
t.test('item', function (st) {
module.exports = function (at, t) {
t.test('at', function (st) {
var arr = [1, [2], [3, 4]];

st.equal(item(arr, 0), arr[0]);
st.equal(item(arr, -3), arr[0]);
st.equal(at(arr, 0), arr[0]);
st.equal(at(arr, -3), arr[0]);

st.deepEqual(item(arr, 1), arr[1]);
st.deepEqual(item(arr, -2), arr[1]);
st.deepEqual(at(arr, 1), arr[1]);
st.deepEqual(at(arr, -2), arr[1]);

st.deepEqual(item(arr, 2), arr[2]);
st.deepEqual(item(arr, -1), arr[2]);
st.deepEqual(at(arr, 2), arr[2]);
st.deepEqual(at(arr, -1), arr[2]);

st.equal(item(arr, 3), undefined);
st.equal(item(arr, -4), undefined);
st.equal(at(arr, 3), undefined);
st.equal(at(arr, -4), undefined);

st.equal(item([], 0), undefined);
st.equal(item([], -1), undefined);
st.equal(at([], 0), undefined);
st.equal(at([], -1), undefined);

st.end();
});

t.test('sparse arrays', function (st) {
// eslint-disable-next-line no-sparse-arrays
var arr = [, [1]];
st.equal(item(arr, 0), undefined);
st.equal(item(arr, -2), undefined);
st.equal(at(arr, 0), undefined);
st.equal(at(arr, -2), undefined);

st.deepEqual(item(arr, 1), [1]);
st.deepEqual(item(arr, -1), [1]);
st.deepEqual(at(arr, 1), [1]);
st.deepEqual(at(arr, -1), [1]);

st.end();
});
Expand Down

0 comments on commit ad5633a

Please sign in to comment.