Skip to content

Commit

Permalink
add code points / code units explicit feature detection for String#at
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 24, 2020
1 parent 1b27991 commit 9c07cee
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- `%TypedArray%#filterOut`
- Added [array deduplication stage 1 proposal](https://github.com/tc39/proposal-array-unique)
- `Array#uniqueBy`
- Added code points / code units explicit feature detection in `String#at` for preventing breakage code which use obsolete `String#at` proposal polyfill
- Added the missed `(es|stable)/instance/replace-all` entries
- Updated compat data mapping for Opera - from Opera 69, the difference with Chrome versions increased to 14
- Compat data mapping for modern Android WebView to Chrome moved from targets parser directly to compat data
Expand Down
7 changes: 6 additions & 1 deletion packages/core-js/modules/esnext.string.at-alternative.js
Expand Up @@ -4,10 +4,15 @@ var $ = require('../internals/export');
var requireObjectCoercible = require('../internals/require-object-coercible');
var toLength = require('../internals/to-length');
var toInteger = require('../internals/to-integer');
var fails = require('../internals/fails');

var FORCED = fails(function () {
return '𠮷'.at(0) !== '\uD842';
});

// `String.prototype.at` method
// https://github.com/tc39/proposal-relative-indexing-method
$({ target: 'String', proto: true }, {
$({ target: 'String', proto: true, forced: FORCED }, {
at: function at(index) {
var S = String(requireObjectCoercible(this));
var len = toLength(S.length);
Expand Down
7 changes: 6 additions & 1 deletion packages/core-js/modules/esnext.string.at.js
@@ -1,10 +1,15 @@
'use strict';
var $ = require('../internals/export');
var charAt = require('../internals/string-multibyte').charAt;
var fails = require('../internals/fails');

var FORCED = fails(function () {
return '𠮷'.at(0) !== '𠮷';
});

// `String.prototype.at` method
// https://github.com/mathiasbynens/String.prototype.at
$({ target: 'String', proto: true }, {
$({ target: 'String', proto: true, forced: FORCED }, {
at: function at(pos) {
return charAt(this, pos);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/compat/tests.js
Expand Up @@ -1425,7 +1425,7 @@ GLOBAL.tests = {
return Set.prototype.union;
},
'esnext.string.at': function () {
return String.prototype.at;
return '𠮷'.at(0) === '𠮷';
},
'esnext.string.code-points': function () {
return String.prototype.codePoints;
Expand Down
1 change: 1 addition & 0 deletions tests/pure/esnext.string.at-alternative.js
Expand Up @@ -19,6 +19,7 @@ QUnit.test('String#at', assert => {
assert.same('1', at('1', NaN));
assert.same('1', at('1'));
assert.same('1', at('123', -0));
assert.same('\uD842', at('𠮷'));
assert.same('1', at({ toString() { return '123'; } }, 0));
if (STRICT) {
assert.throws(() => at(null, 0), TypeError);
Expand Down
1 change: 1 addition & 0 deletions tests/tests/esnext.string.at-alternative.js
Expand Up @@ -22,6 +22,7 @@ QUnit.test('String#at', assert => {
assert.same('1', '1'.at(NaN));
assert.same('1', '1'.at());
assert.same('1', '123'.at(-0));
assert.same('\uD842', '𠮷'.at());
assert.same('1', at.call({ toString() { return '123'; } }, 0));
if (STRICT) {
assert.throws(() => at.call(null, 0), TypeError);
Expand Down

0 comments on commit 9c07cee

Please sign in to comment.