-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add proposal for a
.item()
method on all the built-in indexables
- Loading branch information
Showing
28 changed files
with
308 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/core-js-pure/override/modules/esnext.typed-array.item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../modules/esnext.array.item'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('Array', 'item'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
var parent = require('../../../es/array/virtual'); | ||
require('../../../modules/esnext.array.item'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../../modules/esnext.array.item'); | ||
var entryVirtual = require('../../../internals/entry-virtual'); | ||
|
||
module.exports = entryVirtual('Array').item; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var arrayItem = require('../array/virtual/item'); | ||
var stringItem = require('../string/virtual/item'); | ||
|
||
var ArrayPrototype = Array.prototype; | ||
var StringPrototype = String.prototype; | ||
|
||
module.exports = function (it) { | ||
var own = it.item; | ||
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.item)) return arrayItem; | ||
if (typeof it === 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.item)) { | ||
return stringItem; | ||
} return own; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../modules/esnext.string.item'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('String', 'item'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../../modules/esnext.string.item'); | ||
var entryVirtual = require('../../../internals/entry-virtual'); | ||
|
||
module.exports = entryVirtual('String').item; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
var parent = require('../../es/typed-array'); | ||
require('../../modules/esnext.typed-array.item'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('../../modules/esnext.typed-array.item'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var toObject = require('../internals/to-object'); | ||
var toLength = require('../internals/to-length'); | ||
var toInteger = require('../internals/to-integer'); | ||
var addToUnscopables = require('../internals/add-to-unscopables'); | ||
|
||
// `Array.prototype.item` method | ||
// https://github.com/tc39/proposal-item-method | ||
$({ target: 'Array', proto: true }, { | ||
item: function item(index) { | ||
var O = toObject(this); | ||
var len = toLength(O.length); | ||
var relativeIndex = toInteger(index); | ||
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex; | ||
return (k < 0 || k >= len) ? undefined : O[k]; | ||
} | ||
}); | ||
|
||
addToUnscopables('item'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'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 | ||
// https://github.com/tc39/proposal-item-method | ||
$({ target: 'String', proto: true }, { | ||
item: function item(index) { | ||
var S = String(requireObjectCoercible(this)); | ||
var len = toLength(S.length); | ||
var relativeIndex = toInteger(index); | ||
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex; | ||
return (k < 0 || k >= len) ? undefined : S.charAt(k); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
var ArrayBufferViewCore = require('../internals/array-buffer-view-core'); | ||
var toLength = require('../internals/to-length'); | ||
var toInteger = require('../internals/to-integer'); | ||
|
||
var aTypedArray = ArrayBufferViewCore.aTypedArray; | ||
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod; | ||
|
||
// `%TypedArray%.prototype.item` method | ||
// https://github.com/tc39/proposal-item-method | ||
exportTypedArrayMethod('item', function item(index) { | ||
var O = aTypedArray(this); | ||
var len = toLength(O.length); | ||
var relativeIndex = toInteger(index); | ||
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex; | ||
return (k < 0 || k >= len) ? undefined : O[k]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// https://github.com/tc39/proposal-item-method | ||
require('../modules/esnext.array.item'); | ||
require('../modules/esnext.string.item'); | ||
require('../modules/esnext.typed-array.item'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require('../proposals/item-method'); | ||
var parent = require('./4'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { STRICT } from '../helpers/constants'; | ||
|
||
import item from 'core-js-pure/features/array/item'; | ||
|
||
QUnit.test('Array#item', assert => { | ||
assert.isFunction(item); | ||
assert.same(1, item([1, 2, 3], 0)); | ||
assert.same(2, item([1, 2, 3], 1)); | ||
assert.same(3, item([1, 2, 3], 2)); | ||
assert.same(undefined, item([1, 2, 3], 3)); | ||
assert.same(3, item([1, 2, 3], -1)); | ||
assert.same(2, item([1, 2, 3], -2)); | ||
assert.same(1, item([1, 2, 3], -3)); | ||
assert.same(undefined, item([1, 2, 3], -4)); | ||
assert.same(1, item([1, 2, 3], 0.4)); | ||
assert.same(1, item([1, 2, 3], 0.5)); | ||
assert.same(1, item([1, 2, 3], 0.6)); | ||
assert.same(1, item([1], NaN)); | ||
assert.same(1, item([1])); | ||
assert.same(1, item([1, 2, 3], -0)); | ||
assert.same(undefined, item(Array(1), 0)); | ||
assert.same(1, item({ 0: 1, length: 1 }, 0)); | ||
if (STRICT) { | ||
assert.throws(() => item(null, 0), TypeError); | ||
assert.throws(() => item(undefined, 0), TypeError); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { STRICT } from '../helpers/constants'; | ||
|
||
import item from 'core-js-pure/features/string/item'; | ||
|
||
QUnit.test('String#item', assert => { | ||
assert.isFunction(item); | ||
assert.same('1', item('123', 0)); | ||
assert.same('2', item('123', 1)); | ||
assert.same('3', item('123', 2)); | ||
assert.same(undefined, item('123', 3)); | ||
assert.same('3', item('123', -1)); | ||
assert.same('2', item('123', -2)); | ||
assert.same('1', item('123', -3)); | ||
assert.same(undefined, item('123', -4)); | ||
assert.same('1', item('123', 0.4)); | ||
assert.same('1', item('123', 0.5)); | ||
assert.same('1', item('123', 0.6)); | ||
assert.same('1', item('1', NaN)); | ||
assert.same('1', item('1')); | ||
assert.same('1', item('123', -0)); | ||
assert.same('1', item({ toString() { return '123'; } }, 0)); | ||
if (STRICT) { | ||
assert.throws(() => item(null, 0), TypeError); | ||
assert.throws(() => item(undefined, 0), TypeError); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { STRICT } from '../helpers/constants'; | ||
|
||
QUnit.test('Array#item', assert => { | ||
const { item } = Array.prototype; | ||
assert.isFunction(item); | ||
assert.arity(item, 1); | ||
assert.name(item, 'item'); | ||
assert.looksNative(item); | ||
assert.nonEnumerable(Array.prototype, 'item'); | ||
assert.same(1, [1, 2, 3].item(0)); | ||
assert.same(2, [1, 2, 3].item(1)); | ||
assert.same(3, [1, 2, 3].item(2)); | ||
assert.same(undefined, [1, 2, 3].item(3)); | ||
assert.same(3, [1, 2, 3].item(-1)); | ||
assert.same(2, [1, 2, 3].item(-2)); | ||
assert.same(1, [1, 2, 3].item(-3)); | ||
assert.same(undefined, [1, 2, 3].item(-4)); | ||
assert.same(1, [1, 2, 3].item(0.4)); | ||
assert.same(1, [1, 2, 3].item(0.5)); | ||
assert.same(1, [1, 2, 3].item(0.6)); | ||
assert.same(1, [1].item(NaN)); | ||
assert.same(1, [1].item()); | ||
assert.same(1, [1, 2, 3].item(-0)); | ||
assert.same(undefined, Array(1).item(0)); | ||
assert.same(1, item.call({ 0: 1, length: 1 }, 0)); | ||
if (STRICT) { | ||
assert.throws(() => item.call(null, 0), TypeError); | ||
assert.throws(() => item.call(undefined, 0), TypeError); | ||
} | ||
}); |
Oops, something went wrong.