-
Notifications
You must be signed in to change notification settings - Fork 5
/
polyfill.js
32 lines (29 loc) · 1.19 KB
/
polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(function () {
//Copy from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
polyfill('trim', function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
});
//Copy from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
polyfill('startsWith', function (searchString, position) {
position = position || 0;
return this.substr(position, searchString.length) === searchString;
});
//Copy from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
polyfill('endsWith', function (searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.lastIndexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
});
/**
* @param {string} name
* @param {Function} func
*/
function polyfill(name, func) {
if (!String.prototype[name])
String.prototype[name] = func;
}
})();