-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix to 'startsWith' javascript error when using IE
- Loading branch information
Showing
4 changed files
with
61 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
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,55 @@ | ||
/*! https://mths.be/startswith v0.2.0 by @mathias */ | ||
if (!String.prototype.startsWith) { | ||
(function() { | ||
'use strict'; // needed to support `apply`/`call` with `undefined`/`null` | ||
var defineProperty = (function() { | ||
// IE 8 only supports `Object.defineProperty` on DOM elements | ||
try { | ||
var object = {}; | ||
var $defineProperty = Object.defineProperty; | ||
var result = $defineProperty(object, object, object) && $defineProperty; | ||
} catch(error) {} | ||
return result; | ||
}()); | ||
var toString = {}.toString; | ||
var startsWith = function(search) { | ||
if (this == null) { | ||
throw TypeError(); | ||
} | ||
var string = String(this); | ||
if (search && toString.call(search) == '[object RegExp]') { | ||
throw TypeError(); | ||
} | ||
var stringLength = string.length; | ||
var searchString = String(search); | ||
var searchLength = searchString.length; | ||
var position = arguments.length > 1 ? arguments[1] : undefined; | ||
// `ToInteger` | ||
var pos = position ? Number(position) : 0; | ||
if (pos != pos) { // better `isNaN` | ||
pos = 0; | ||
} | ||
var start = Math.min(Math.max(pos, 0), stringLength); | ||
// Avoid the `indexOf` call if no match is possible | ||
if (searchLength + start > stringLength) { | ||
return false; | ||
} | ||
var index = -1; | ||
while (++index < searchLength) { | ||
if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
if (defineProperty) { | ||
defineProperty(String.prototype, 'startsWith', { | ||
'value': startsWith, | ||
'configurable': true, | ||
'writable': true | ||
}); | ||
} else { | ||
String.prototype.startsWith = startsWith; | ||
} | ||
}()); | ||
} |
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