Skip to content

Commit

Permalink
Move the startsWith/endsWith bugfix code down to right before the…
Browse files Browse the repository at this point in the history
… shim is installed, and ensure it doesn't fail if `startsWith` is not present.

This ensures that on a browser without `String#startsWith`, the shim does not throw on `overrideNative`

Fixes #359.
  • Loading branch information
ljharb committed Oct 23, 2015
1 parent 44fe6d7 commit 30f4bde
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,6 @@
});
};

var startsWithRejectsRegex = function () {
return String.prototype.startsWith && throwsError(function () {
/* throws if spec-compliant */
'/a/'.startsWith(/a/);
});
};
var startsWithHandlesInfinity = (function () {
return String.prototype.startsWith && 'abc'.startsWith('a', Infinity) === false;
}());

var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
Expand All @@ -157,7 +147,6 @@
var globals = getGlobal();
var globalIsFinite = globals.isFinite;
var hasStrictMode = (function () { return this === null; }.call(null));
var startsWithIsCompliant = startsWithRejectsRegex() && startsWithHandlesInfinity;
var _indexOf = Function.call.bind(String.prototype.indexOf);
var _toString = Function.call.bind(Object.prototype.toString);
var _concat = Function.call.bind(Array.prototype.concat);
Expand Down Expand Up @@ -688,6 +677,20 @@
if (String.prototype.includes && 'a'.includes('a', Infinity) !== false) {
overrideNative(String.prototype, 'includes', StringPrototypeShims.includes);
}

if (String.prototype.startsWith && String.prototype.endsWith) {
var startsWithRejectsRegex = throwsError(function () {
/* throws if spec-compliant */
'/a/'.startsWith(/a/);
});
var startsWithHandlesInfinity = 'abc'.startsWith('a', Infinity) === false;
if (!startsWithRejectsRegex || !startsWithHandlesInfinity) {
// Firefox (< 37?) and IE 11 TP have a noncompliant startsWith implementation
overrideNative(String.prototype, 'startsWith', StringPrototypeShims.startsWith);
overrideNative(String.prototype, 'endsWith', StringPrototypeShims.endsWith);
}
}

defineProperties(String.prototype, StringPrototypeShims);

var hasStringTrimBug = '\u0085'.trim().length !== 1;
Expand Down Expand Up @@ -738,12 +741,6 @@
return new StringIterator(this);
});

if (!startsWithIsCompliant) {
// Firefox (< 37?) and IE 11 TP have a noncompliant startsWith implementation
overrideNative(String.prototype, 'startsWith', StringPrototypeShims.startsWith);
overrideNative(String.prototype, 'endsWith', StringPrototypeShims.endsWith);
}

var ArrayShims = {
from: function from(items) {
var C = this;
Expand Down

0 comments on commit 30f4bde

Please sign in to comment.