1 /*! http://mths.be/endswith v0.2.0 by @mathias */
2 if (!String.prototype.endsWith) {
4 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
5 var defineProperty = (function() {
6 // IE 8 only supports `Object.defineProperty` on DOM elements
9 var $defineProperty = Object.defineProperty;
10 var result = $defineProperty(object, object, object) && $defineProperty;
14 var toString = {}.toString;
15 var endsWith = function(search) {
19 var string = String(this);
20 if (search && toString.call(search) == '[object RegExp]') {
23 var stringLength = string.length;
24 var searchString = String(search);
25 var searchLength = searchString.length;
26 var pos = stringLength;
27 if (arguments.length > 1) {
28 var position = arguments[1];
29 if (position !== undefined) {
31 pos = position ? Number(position) : 0;
32 if (pos != pos) { // better `isNaN`
37 var end = Math.min(Math.max(pos, 0), stringLength);
38 var start = end - searchLength;
43 while (++index < searchLength) {
44 if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
51 defineProperty(String.prototype, 'endsWith', {
57 String.prototype.endsWith = endsWith;