Skip to content

Commit

Permalink
Merge pull request #175 from cginternals/issue-174-IE-support
Browse files Browse the repository at this point in the history
support IE by providing polyfills for String.includes(), String.trimLeft(), String.trimRight()
  • Loading branch information
anne-gropler authored Mar 15, 2019
2 parents efcd5be + b5402d4 commit b95b0fb
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions source/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
if (String.prototype.repeat === undefined) {
// tslint:disable-next-line:space-before-function-paren
String.prototype.repeat = function (count): string {
'use strict';
if (this === null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
Expand Down Expand Up @@ -51,7 +50,6 @@ if (String.prototype.startsWith === undefined) {
};
}


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

/**
* IE11 polyfill for string.includes function, from
* https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes
*/
if (String.prototype.includes === undefined) {
// tslint:disable-next-line: space-before-function-paren
String.prototype.includes = function (search, start): boolean {
if (typeof start !== 'number') {
start = 0;
}

if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}

/**
* IE11 polyfill for string.trimLeft function, from
* https://stackoverflow.com/a/2308168
*/
if (String.prototype.trimLeft === undefined) {
// tslint:disable-next-line: space-before-function-paren
String.prototype.trimLeft = function (): string {
return this.replace(/^\s+/, '');
};
}

/**
* IE11 polyfill for string.trimLeft function, from
* https://stackoverflow.com/a/2308168
*/
if (String.prototype.trimRight === undefined) {
// tslint:disable-next-line: space-before-function-paren
String.prototype.trimRight = function (): string {
return this.replace(/^\s+/, '');
};
}

/**
* IE11 polyfill for Array.forEach function, from ...
Expand Down

0 comments on commit b95b0fb

Please sign in to comment.