Skip to content

Commit

Permalink
fix String.prototype.substr feature detection and compat data
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Sep 20, 2021
1 parent 266e74d commit ed21ba3
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Fixed `String.prototype.substr` feature detection and compat data
- Families of `.at` and `.findLast` methods marked as supported in Safari TP

##### 3.18.0 - 2021.09.20
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ export const data = {
},
'es.string.substr': {
chrome: '1',
ie: '4',
ie: '9',
firefox: '1',
opera: '4',
safari: '1',
Expand Down
5 changes: 4 additions & 1 deletion packages/core-js/modules/es.string.substr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ var slice = ''.slice;
var max = Math.max;
var min = Math.min;

// eslint-disable-next-line unicorn/prefer-string-slice -- required for testing
var FORCED = !''.substr || 'ab'.substr(-1) !== 'b';

// `String.prototype.substr` method
// https://tc39.es/ecma262/#sec-string.prototype.substr
$({ target: 'String', proto: true }, {
$({ target: 'String', proto: true, forced: FORCED }, {
substr: function substr(start, length) {
var that = toString(requireObjectCoercible(this));
var size = that.length;
Expand Down
3 changes: 2 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ GLOBAL.tests = {
},
'es.string.starts-with': createIsRegExpLogicTest('startsWith'),
'es.string.substr': function () {
return ''.substr;
// eslint-disable-next-line unicorn/prefer-string-slice -- required for testing
return 'ab'.substr(-1) === 'b';
},
'es.string.trim': createStringTrimMethodTest('trim'),
'es.string.trim-end': [createStringTrimMethodTest('trimEnd'), function () {
Expand Down
2 changes: 2 additions & 0 deletions tests/pure/es.string.substr.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ QUnit.test('String#substr', assert => {

assert.same(substr('12345', 1, 3), '234');

assert.same(substr('ab', -1), 'b');

/* eslint-disable es/no-symbol -- safe */
if (typeof Symbol === 'function') {
assert.throws(() => substr(Symbol(), 1, 3), 'throws on symbol context');
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/es.string.substr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ QUnit.test('String#substr', assert => {

assert.same('12345'.substr(1, 3), '234');

assert.same('ab'.substr(-1), 'b');

if (typeof Symbol === 'function' && !Symbol.sham) {
assert.throws(() => substr.call(Symbol(), 1, 3), 'throws on symbol context');
}
Expand Down

0 comments on commit ed21ba3

Please sign in to comment.