Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow setWhitespaceChars to override String.prototype methods

  • Loading branch information...
commit c351f287864e14ae03289d73c75902caa346e8ca 1 parent d5b74ec
@unscriptable unscriptable authored
Showing with 28 additions and 15 deletions.
  1. +28 −15 string.js
View
43 string.js
@@ -21,6 +21,7 @@ define (['./lib/_base'], function (base) {
var proto = String.prototype,
featureMap,
+ has,
toString;
featureMap = {
@@ -29,11 +30,15 @@ define (['./lib/_base'], function (base) {
'string-trimright': 'trimRight'
};
- function has (feature) {
+ function checkFeature (feature) {
var prop = featureMap[feature];
return base.isFunction(proto[prop]);
}
+ function neg () { return false; }
+
+ has = checkFeature;
+
// compressibility helper
function remove (str, rx) {
return str.replace(rx, '');
@@ -46,28 +51,36 @@ define (['./lib/_base'], function (base) {
trimRightRx = /\s+$/;
trimLeftRx = /^\s+/;
- if (!has('string-trim')) {
- proto.trim = function trim () {
- return remove(remove(toString(this), trimLeftRx), trimRightRx);
- };
- }
+ function checkShims () {
+ if (!has('string-trim')) {
+ proto.trim = function trim () {
+ return remove(remove(toString(this), trimLeftRx), trimRightRx);
+ };
+ }
- if (!has('string-trimleft')) {
- proto.trimLeft = function trimLeft () {
- return remove(toString(this), trimLeftRx);
- };
- }
+ if (!has('string-trimleft')) {
+ proto.trimLeft = function trimLeft () {
+ return remove(toString(this), trimLeftRx);
+ };
+ }
+
+ if (!has('string-trimright')) {
+ proto.trimRight = function trimRight () {
+ return remove(toString(this), trimRightRx);
+ };
+ }
- if (!has('string-trimright')) {
- proto.trimRight = function trimRight () {
- return remove(toString(this), trimRightRx);
- };
}
+ checkShims();
+
return {
setWhitespaceChars: function (wsc) {
trimRightRx = new RegExp(wsc + '$');
trimLeftRx = new RegExp('^' + wsc);
+ // fail all has() checks and check shims again
+ has = neg;
+ checkShims();
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.