Permalink
Browse files

Improved the trim function to use a regexp function to catch more whi…

…tespace types
  • Loading branch information...
Sembiance committed Jul 15, 2014
1 parent 3318b2d commit 8ccc4b58e24c37ea907af3d51e00be5485430768
Showing with 12 additions and 17 deletions.
  1. +4 −16 String.js
  2. +1 −1 package.json
  3. +7 −0 test.js
View
@@ -45,24 +45,12 @@ if(!String.prototype.contains)
String.prototype.trim = function(chars)
{
if(!chars)
- chars = " \t\n\r\u00a0"; // 00a0 is hex for 160 which is nbsp
+ chars = "\\s\\n\\r";
- var text = this;
- var c;
- var i;
- for(i=0,c=text.charAt(i);i<text.length && chars.indexOf(c)!==-1;i++,c=text.charAt(i))
- {
- }
-
- text = text.substring(i);
-
- for(i=(text.length-1),c=text.charAt(i);i>=0 && chars.indexOf(c)!==-1;i--,c=text.charAt(i))
- {
- }
-
- text = text.substring(0, (i+1));
+ if(Array.isArray(chars))
+ chars = chars.join("");
- return text;
+ return this.replace(new RegExp("^[" + chars + "]+|[" + chars + "]+$", "g"), "");
};
if(!String.prototype.replaceAll)
View
@@ -1,6 +1,6 @@
{
"name" : "xbase",
- "version" : "1.1.7",
+ "version" : "1.1.8",
"author" : "Robert Schultz <robert@cosmicrealms.com>",
"description" : "common JS files used in all my JS projects",
"private" : true,
View
@@ -0,0 +1,7 @@
+"use strict";
+
+var xbase = require("./base.js");
+
+console.log("[%s]", " \t\n\r \t this is just a test \t\t of the system \t \n \t\t\t ".trim().innerTrim());
+console.log("[%s]", " this is just a test \t\t of the system ".trim("thm ").innerTrim());
+console.log("[%s]", " this is just a test \t\t of the system \t ".trim(["t", "h", "m", " ", "\t"]).innerTrim());

0 comments on commit 8ccc4b5

Please sign in to comment.