Last active
December 26, 2018 02:01
-
-
Save aMarCruz/7892c05992d9ba559c99 to your computer and use it in GitHub Desktop.
String startsWith & endsWith polyfills
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
;(function (sp) { | |
if (!sp.startsWith) | |
sp.startsWith = function (str) { | |
return !!(str && this) && !this.lastIndexOf(str, 0) | |
} | |
if (!sp.endsWith) | |
sp.endsWith = function (str) { | |
var offset = str && this ? this.length - str.length : -1 | |
return offset >= 0 && this.lastIndexOf(str, offset) === offset | |
} | |
})(String.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this one by coincidence. Nice, minimalistic code from a Riot.js contributor. Thanks!