Add polyfills for String.trimLeft/Right for browsers without support.

This commit is contained in:
David Anson 2015-05-03 22:40:34 -07:00
parent dce6024e16
commit 85531ff21e
4 changed files with 57 additions and 1 deletions

26
demo/browser-polyfills.js Normal file
View file

@ -0,0 +1,26 @@
"use strict";
// Polyfills for browsers that do not support String.trimLeft/Right
function trimLeftPolyfill() {
return this.replace(/^\s*/, "");
}
/* istanbul ignore if */
if (!String.prototype.trimLeft) {
String.prototype.trimLeft = trimLeftPolyfill;
}
function trimRightPolyfill() {
return this.replace(/\s*$/, "");
}
/* istanbul ignore if */
if (!String.prototype.trimRight) {
String.prototype.trimRight = trimRightPolyfill;
}
// Export for testing
/* istanbul ignore else */
if ((typeof module !== "undefined") && module.exports) {
module.exports = {
"trimLeftPolyfill": trimLeftPolyfill,
"trimRightPolyfill": trimRightPolyfill
};
}