Last active
March 29, 2016 08:51
-
-
Save think49/908b8d5f08c9945beea7 to your computer and use it in GitHub Desktop.
es6-string-prototype-startswith.js: String.prototype.startsWith の Polyfill (ES6 規定)
This file contains hidden or 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
/** | |
* es6-string-prototype-startswith.js | |
* String.prototype.startsWith (ECMA-262 6th Edition / ECMAScript 2015) | |
* | |
* | |
* @version 1.0.0 | |
* @author think49 | |
* @url https://gist.github.com/think49/908b8d5f08c9945beea7 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
* @see <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.startswith">21.1.3.18 String.prototype.startsWith – ECMA-262 6th Edition</a> | |
*/ | |
'use strict'; | |
if (typeof String.prototype.startsWith !== 'function') { | |
(function (Object, String, RegExp, max, min) { | |
Object.defineProperty(this, 'startsWith', | |
{ | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
value: function startsWith (searchString /* [, position] */) { | |
var thisArg, position, length; | |
if (this === null || typeof this === 'undefined') { | |
throw new TypeError('String.prototype.startsWith called on null or undefined'); | |
} | |
if (searchString instanceof RegExp) { | |
throw new TypeError('First argument to String.prototype.startsWith must not be a regular expression'); | |
} | |
thisArg = String(this); | |
searchString = String(searchString); | |
length = searchString.length; | |
position = arguments.length < 2 ? 0 : min(max(~~arguments[1], 0), length); | |
if (position + length > thisArg.length) { | |
return false; | |
} | |
return thisArg.lastIndexOf(searchString, position) === position; | |
} | |
}); | |
}.call(String.prototype, Object, String, RegExp, Math.max, Math.min)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment