Skip to content

Commit b95b0fb

Browse files
authored
Merge pull request #175 from cginternals/issue-174-IE-support
support IE by providing polyfills for String.includes(), String.trimLeft(), String.trimRight()
2 parents efcd5be + b5402d4 commit b95b0fb

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

source/polyfill.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
if (String.prototype.repeat === undefined) {
66
// tslint:disable-next-line:space-before-function-paren
77
String.prototype.repeat = function (count): string {
8-
'use strict';
98
if (this === null) {
109
throw new TypeError('can\'t convert ' + this + ' to object');
1110
}
@@ -51,7 +50,6 @@ if (String.prototype.startsWith === undefined) {
5150
};
5251
}
5352

54-
5553
/**
5654
* IE11 polyfill for string.endsWith function, from
5755
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
@@ -66,6 +64,46 @@ if (String.prototype.endsWith === undefined) {
6664
};
6765
}
6866

67+
/**
68+
* IE11 polyfill for string.includes function, from
69+
* https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes
70+
*/
71+
if (String.prototype.includes === undefined) {
72+
// tslint:disable-next-line: space-before-function-paren
73+
String.prototype.includes = function (search, start): boolean {
74+
if (typeof start !== 'number') {
75+
start = 0;
76+
}
77+
78+
if (start + search.length > this.length) {
79+
return false;
80+
} else {
81+
return this.indexOf(search, start) !== -1;
82+
}
83+
};
84+
}
85+
86+
/**
87+
* IE11 polyfill for string.trimLeft function, from
88+
* https://stackoverflow.com/a/2308168
89+
*/
90+
if (String.prototype.trimLeft === undefined) {
91+
// tslint:disable-next-line: space-before-function-paren
92+
String.prototype.trimLeft = function (): string {
93+
return this.replace(/^\s+/, '');
94+
};
95+
}
96+
97+
/**
98+
* IE11 polyfill for string.trimLeft function, from
99+
* https://stackoverflow.com/a/2308168
100+
*/
101+
if (String.prototype.trimRight === undefined) {
102+
// tslint:disable-next-line: space-before-function-paren
103+
String.prototype.trimRight = function (): string {
104+
return this.replace(/^\s+/, '');
105+
};
106+
}
69107

70108
/**
71109
* IE11 polyfill for Array.forEach function, from ...

0 commit comments

Comments
 (0)