Skip to content

Commit

Permalink
update the method name: .item -> .at
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 22, 2020
1 parent 68be466 commit 104a4d4
Show file tree
Hide file tree
Showing 38 changed files with 220 additions and 239 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Changelog
##### Unreleased
- Added [`.item()` method on all the built-in indexables stage 3 proposal](https://github.com/tc39/proposal-relative-indexing-method)
- Added [relative indexing method stage 3 proposal](https://github.com/tc39/proposal-relative-indexing-method)
- `Array#at`
- `%TypedArray%#at`
- Added [`Number.range` stage 1 proposal](https://github.com/tc39/proposal-Number.range)
- `Number.range`
- `BigInt.range`
Expand Down
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1777,15 +1777,13 @@ Promise.any([
core-js(-pure)/stage/3
```

##### [`.item()` method on all the built-in indexables](https://github.com/tc39/proposal-relative-indexing-method)[⬆](#index)
Modules [`esnext.array.item`](https://github.com/zloirock/core-js/blob/v3.7.0/packages/core-js/modules/esnext.array.item.js), [`esnext.string.item`](https://github.com/zloirock/core-js/blob/v3.7.0/packages/core-js/modules/esnext.string.item.js) and [`esnext.typed-array.item`](https://github.com/zloirock/core-js/blob/v3.7.0/packages/core-js/modules/esnext.typed-array.item.js)
##### [Relative indexing method](https://github.com/tc39/proposal-relative-indexing-method)[⬆](#index)
Modules [`esnext.array.at`](https://github.com/zloirock/core-js/blob/v3.7.0/packages/core-js/modules/esnext.array.at.js) and [`esnext.typed-array.at`](https://github.com/zloirock/core-js/blob/v3.7.0/packages/core-js/modules/esnext.typed-array.at.js)
> **Warning! Because of the conflict with another proposal, this method is not available on `String.prototype` in this version.**

```js
class Array {
item(index: int): any;
}
class String {
item(index: int): string | undefined;
at(index: int): any;
}
class [
Expand All @@ -1799,21 +1797,19 @@ class [
Float32Array,
Float64Array,
] {
item(index: int): any;
at(index: int): any;
}
```
[*CommonJS entry points:*](#commonjs-api)
```
core-js/proposals/relative-indexing-method
core-js(-pure)/features/array/item
core-js(-pure)/features/string/item
core-js(-pure)/features/typed-array/item
core-js(-pure)/features/array/at
core-js(-pure)/features/typed-array/at
```
[*Examples*](http://es6.zloirock.ru/#log(%5B1%2C%202%2C%203%5D.item(1))%3B%20%20%2F%2F%20%3D%3E%202%0Alog(%5B1%2C%202%2C%203%5D.item(-1))%3B%20%2F%2F%20%3D%3E%203%0Alog('123'.item(-1))%3B%20%20%20%20%20%2F%2F%20%3D%3E%20'3'):
[*Examples*](http://es6.zloirock.ru/#log(%5B1%2C%202%2C%203%5D.at(1))%3B%20%20%2F%2F%20%3D%3E%202%0Alog(%5B1%2C%202%2C%203%5D.at(-1))%3B%20%2F%2F%20%3D%3E%203):
```js
[1, 2, 3].item(1); // => 2
[1, 2, 3].item(-1); // => 3
'123'.item(-1); // => '3'
[1, 2, 3].at(1); // => 2
[1, 2, 3].at(-1); // => 3
```

#### Stage 2 proposals[⬆](#index)
Expand Down
8 changes: 3 additions & 5 deletions packages/core-js-compat/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ const data = {
},
'esnext.array.is-template-object': {
},
'esnext.array.item': {
'esnext.array.at': {
},
'esnext.array.last-index': {
},
Expand Down Expand Up @@ -1480,8 +1480,6 @@ const data = {
},
'esnext.string.code-points': {
},
'esnext.string.item': {
},
// TODO: Remove from `core-js@4`
'esnext.string.match-all': null,
'esnext.string.replace-all': null,
Expand All @@ -1496,9 +1494,9 @@ const data = {
// TODO: Remove from `core-js@4`
'esnext.symbol.replace-all': {
},
'esnext.typed-array.filter-out': {
'esnext.typed-array.at': {
}
'esnext.typed-array.item': {
'esnext.typed-array.filter-out': {
},
'esnext.weak-map.delete-all': {
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ module.exports = {
'esnext.weak-map.emplace',
],
3.8: [
'esnext.array.at',
'esnext.array.filter-out',
'esnext.array.unique-by',
'esnext.bigint.range',
'esnext.number.range',
'esnext.typed-array.at',
'esnext.typed-array.filter-out',
],
};
4 changes: 4 additions & 0 deletions packages/core-js/features/array/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/esnext.array.at');
var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('Array', 'at');
2 changes: 1 addition & 1 deletion packages/core-js/features/array/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var parent = require('../../es/array');
require('../../modules/es.map');
require('../../modules/esnext.array.at');
require('../../modules/esnext.array.filter-out');
require('../../modules/esnext.array.is-template-object');
require('../../modules/esnext.array.item');
require('../../modules/esnext.array.last-item');
require('../../modules/esnext.array.last-index');
require('../../modules/esnext.array.unique-by');
Expand Down
4 changes: 0 additions & 4 deletions packages/core-js/features/array/item.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/core-js/features/array/virtual/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../../modules/esnext.array.at');
var entryVirtual = require('../../../internals/entry-virtual');

module.exports = entryVirtual('Array').at;
3 changes: 2 additions & 1 deletion packages/core-js/features/array/virtual/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var parent = require('../../../es/array/virtual');
require('../../../modules/esnext.array.at');
require('../../../modules/esnext.array.filter-out');
require('../../../modules/esnext.array.item');
require('../../../modules/esnext.array.unique-by');

module.exports = parent;
4 changes: 0 additions & 4 deletions packages/core-js/features/array/virtual/item.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/core-js/features/instance/at.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var at = require('../string/virtual/at');
var arrayAt = require('../array/virtual/at');
var stringAt = require('../string/virtual/at');

var ArrayPrototype = Array.prototype;
var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.at;
return typeof it === 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.at) ? at : own;
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.at)) return arrayAt;
if (typeof it === 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.at)) {
return stringAt;
} return own;
};
13 changes: 0 additions & 13 deletions packages/core-js/features/instance/item.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core-js/features/string/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var parent = require('../../es/string');
require('../../modules/esnext.string.at');
// TODO: disabled by default because of the conflict with another proposal
// require('../../modules/esnext.string.at-alternative');
require('../../modules/esnext.string.code-points');
require('../../modules/esnext.string.item');
// TODO: remove from `core-js@4`
require('../../modules/esnext.string.match-all');
require('../../modules/esnext.string.replace-all');
Expand Down
4 changes: 0 additions & 4 deletions packages/core-js/features/string/item.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core-js/features/string/virtual/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var parent = require('../../../es/string/virtual');
require('../../../modules/esnext.string.at');
// TODO: disabled by default because of the conflict with another proposal
// require('../../../modules/esnext.string.at-alternative');
require('../../../modules/esnext.string.code-points');
require('../../../modules/esnext.string.item');
// TODO: remove from `core-js@4`
require('../../../modules/esnext.string.match-all');
require('../../../modules/esnext.string.replace-all');
Expand Down
4 changes: 0 additions & 4 deletions packages/core-js/features/string/virtual/item.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/core-js/features/typed-array/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../../modules/esnext.typed-array.at');
2 changes: 1 addition & 1 deletion packages/core-js/features/typed-array/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var parent = require('../../es/typed-array');
require('../../modules/esnext.typed-array.at');
require('../../modules/esnext.typed-array.filter-out');
require('../../modules/esnext.typed-array.item');

module.exports = parent;
1 change: 0 additions & 1 deletion packages/core-js/features/typed-array/item.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ var toLength = require('../internals/to-length');
var toInteger = require('../internals/to-integer');
var addToUnscopables = require('../internals/add-to-unscopables');

// `Array.prototype.item` method
// `Array.prototype.at` method
// https://github.com/tc39/proposal-relative-indexing-method
$({ target: 'Array', proto: true }, {
item: function item(index) {
at: function at(index) {
var O = toObject(this);
var len = toLength(O.length);
var relativeIndex = toInteger(index);
Expand All @@ -17,4 +17,4 @@ $({ target: 'Array', proto: true }, {
}
});

addToUnscopables('item');
addToUnscopables('at');
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// TODO: disabled by default because of the conflict with another proposal
'use strict';
var $ = require('../internals/export');
var requireObjectCoercible = require('../internals/require-object-coercible');
var toLength = require('../internals/to-length');
var toInteger = require('../internals/to-integer');

// `String.prototype.item` method
// `String.prototype.at` method
// https://github.com/tc39/proposal-relative-indexing-method
$({ target: 'String', proto: true }, {
item: function item(index) {
at: function at(index) {
var S = String(requireObjectCoercible(this));
var len = toLength(S.length);
var relativeIndex = toInteger(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var toInteger = require('../internals/to-integer');
var aTypedArray = ArrayBufferViewCore.aTypedArray;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;

// `%TypedArray%.prototype.item` method
// `%TypedArray%.prototype.at` method
// https://github.com/tc39/proposal-relative-indexing-method
exportTypedArrayMethod('item', function item(index) {
exportTypedArrayMethod('at', function at(index) {
var O = aTypedArray(this);
var len = toLength(O.length);
var relativeIndex = toInteger(index);
Expand Down
7 changes: 4 additions & 3 deletions packages/core-js/proposals/relative-indexing-method.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// https://github.com/tc39/proposal-relative-indexing-method
require('../modules/esnext.array.item');
require('../modules/esnext.string.item');
require('../modules/esnext.typed-array.item');
require('../modules/esnext.array.at');
// TODO: disabled by default because of the conflict with another proposal
// require('../modules/esnext.string.at-alternative');
require('../modules/esnext.typed-array.at');
22 changes: 8 additions & 14 deletions tests/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('features/array/is-template-object') === 'function');
ok(Array.isArray(load('features/array/from')('qwe')));
ok(Array.isArray(load('features/array/of')('q', 'w', 'e')));
ok(load('features/array/at')([1, 2, 3], -2) === 2);
ok(load('features/array/join')('qwe', 1) === 'q1w1e');
ok(load('features/array/slice')('qwe', 1)[1] === 'e');
ok(load('features/array/sort')([1, 3, 2])[1] === 2);
Expand All @@ -71,7 +72,6 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('features/array/filter-out') === 'function');
ok(typeof load('features/array/flat') === 'function');
ok(typeof load('features/array/flat-map') === 'function');
ok(load('features/array/item')([1, 2, 3], -2) === 2);
ok(typeof load('features/array/some') === 'function');
ok(typeof load('features/array/every') === 'function');
ok(typeof load('features/array/reduce') === 'function');
Expand All @@ -93,6 +93,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(load('features/array/includes')([1, 2, 3], 2));
ok('next' in load('features/array/iterator')([]));
ok(typeof load('features/array/unique-by') === 'function');
ok(load('features/array/virtual/at').call([1, 2, 3], -2) === 2);
ok(load('features/array/virtual/join').call('qwe', 1) === 'q1w1e');
ok(load('features/array/virtual/slice').call('qwe', 1)[1] === 'e');
ok(load('features/array/virtual/splice').call([1, 2, 3], 1, 2)[0] === 2);
Expand All @@ -103,7 +104,6 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('features/array/virtual/filter-out') === 'function');
ok(typeof load('features/array/virtual/flat') === 'function');
ok(typeof load('features/array/virtual/flat-map') === 'function');
ok(load('features/array/virtual/item').call([1, 2, 3], -2) === 2);
ok(typeof load('features/array/virtual/some') === 'function');
ok(typeof load('features/array/virtual/every') === 'function');
ok(typeof load('features/array/virtual/reduce') === 'function');
Expand Down Expand Up @@ -212,7 +212,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(load('features/string/code-point-at')('a', 0) === 97);
ok(load('features/string/ends-with')('qwe', 'we'));
ok(load('features/string/includes')('qwe', 'w'));
ok(load('features/string/item')('123', -2) === '2');
// ok(load('features/string/at-alternative')('123', -2) === '2');
ok(load('features/string/repeat')('q', 3) === 'qqq');
ok(load('features/string/starts-with')('qwe', 'qw'));
ok(typeof load('features/string/anchor') === 'function');
Expand Down Expand Up @@ -242,7 +242,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(load('features/string/virtual/code-point-at').call('a', 0) === 97);
ok(load('features/string/virtual/ends-with').call('qwe', 'we'));
ok(load('features/string/virtual/includes').call('qwe', 'w'));
ok(load('features/string/virtual/item').call('123', -2) === '2');
// ok(load('features/string/virtual/at-alternative').call('123', -2) === '2');
ok(load('features/string/virtual/repeat').call('q', 3) === 'qqq');
ok(load('features/string/virtual/starts-with').call('qwe', 'qw'));
ok(typeof load('features/string/virtual/anchor') === 'function');
Expand Down Expand Up @@ -1004,8 +1004,10 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
const instanceAt = load('features/instance/at');
ok(typeof instanceAt === 'function');
ok(instanceAt({}) === undefined);
ok(typeof instanceAt([]) === 'function');
ok(typeof instanceAt('') === 'function');
ok(instanceAt('').call('abc', 1) === 'b');
ok(instanceAt([]).call([1, 2, 3], 2) === 3);
ok(instanceAt('').call('123', 2) === '3');

let instanceBind = load('features/instance/bind');
ok(typeof instanceBind === 'function');
Expand Down Expand Up @@ -1275,14 +1277,6 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(instanceIncludes([]).call([1, 2, 3], 2));
ok(instanceIncludes('').call('123', '2'));

const instanceItem = load('features/instance/item');
ok(typeof instanceItem === 'function');
ok(instanceItem({}) === undefined);
ok(typeof instanceItem([]) === 'function');
ok(typeof instanceItem('') === 'function');
ok(instanceItem([]).call([1, 2, 3], 2) === 3);
ok(instanceItem('').call('123', 2) === '3');

let instanceIndexOf = load('features/instance/index-of');
ok(typeof instanceIndexOf === 'function');
ok(instanceIndexOf({}) === undefined);
Expand Down Expand Up @@ -1666,6 +1660,7 @@ ok(typeof load('features/typed-array/int32-array') === 'function');
ok(typeof load('features/typed-array/uint32-array') === 'function');
ok(typeof load('features/typed-array/float32-array') === 'function');
ok(typeof load('features/typed-array/float64-array') === 'function');
load('features/typed-array/at');
load('features/typed-array/copy-within');
load('features/typed-array/entries');
load('features/typed-array/every');
Expand All @@ -1678,7 +1673,6 @@ load('features/typed-array/for-each');
load('features/typed-array/from');
load('features/typed-array/includes');
load('features/typed-array/index-of');
load('features/typed-array/item');
load('features/typed-array/iterator');
load('features/typed-array/join');
load('features/typed-array/keys');
Expand Down

0 comments on commit 104a4d4

Please sign in to comment.