Defined base/string.js

String utilities

Source
{
    pad: pad,
    truncate: truncate,
    format: format,
    toArray: toArray,
    multiply: multiply,
    style: style,
    escapeHtml: escapeHtml
}
    

escapeHtml Static Function Public


Defined base/string.js

Escapes an HTML string by replacing <>&" characters.

Example
comb.string.escapeHtml('<script>alert("test")</script>'); // &lt;script&gt;alert("test")&lt;/script&gt;
        
Arguments Source
function (str){
   return String(str)
       .replace(/&/g, '&amp;')
       .replace(/&lt;/g, '&lt;')
       .replace(/>/g, '&gt;')
       .replace(/"/g, '&quot;')
       .replace(/'/g, '&#39;');
}
    

format Static Function Public


Defined base/string.js

Formats a string with the specified format

Format Strings

comb.string.format("%s", "Hello"); // "Hello"
comb.string.format("%10s", "Hello"); // "     Hello"
comb.string.format("%-10s", "Hello"); // ""Hello     ""
comb.string.format('%.10s', "Hello"); //".....Hello"
comb.string.format('%-!10s', "Hello"); //"Hello!!!!!"
comb.string.format("%-.10s%s!", "Hello", "World"); //"Hello.....World!"

Formatting Numbers

comb.string.format('%d', 10); //"10"

//setting precision
comb.string.format('%.2d', 10); //"10.00"

//specifying width
comb.string.format('%5d', 10); //"   10"

//Signed
comb.string.format('%+d', 10);  //"+10"
comb.string.format('%+d', -10); //"-10"

comb.string.format('% d', 10);  //" 10"
comb.string.format('% d', -10); //"-10"

//width
comb.string.format('%5d', 10); //"   10"

//width and precision
comb.string.format('%6.2d', 10); //" 10.00"

//signed, width and precision
comb.string.format('%+ 7.2d', 10);  //" +10.00"
comb.string.format('%+ 7.2d', -10); //" -10.00"
comb.string.format('%+07.2d', 10);  //"+010.00"
comb.string.format('%+07.2d', -10); //"-010.00"
comb.string.format('%  7.2d', 10);  //"  10.00"
comb.string.format('%  7.2d', -10); //" -10.00"
comb.string.format('% 7.2d', 10);   //"  10.00"
comb.string.format('% 7.2d', -10);  //" -10.00"

//use a 0 as padding
comb.string.format('%010d', 10); //"0000000010"

//use an ! as padding
comb.string.format('%!10d', 10); //"!!!!!!!!10"

//left justify signed ! as padding and a width of 10
comb.string.format('%-+!10d', 10); //"+10!!!!!!!"

Formatting dates

comb.string.format("%[h:mm a]D", new Date(2014, 05, 04, 7,6,1)); // 7:06 AM - local -
comb.string.format("%[h:mm a]Z", new Date(2014, 05, 04, 7,6,1)); //12:06 PM - UTC
comb.string.format("%[yyyy-MM-dd]D", new Date(2014, 05, 04, 7,6,1)); // 2014-06-04 - local
comb.string.format("%[yyyy-MM-dd]Z", new Date(2014, 05, 04, 7,6,1)); // 2014-06-04 - UTC

Formatting Objects

//When using object formats they must be in an array otherwise
//format will try to interpolate the properties into the string.
comb.string.format("%j", [{a : "b"}]) // '{"a":"b"}'

//Specifying spacing
comb.string.format("%4j", [{a : "b"}]) // '{\n    "a": "b"\n}'

String interpolation

comb.string.format("{hello}, {world}", {hello : "Hello", world : "World"); //"Hello, World";
comb.string.format("{[.2]min}...{[.2]max}", {min: 1, max: 10}); //"1.00...10.00"

Arguments Returns Source
function (str,obj){
   if (obj instanceof Array) {
       var i = 0, len = obj.length;
       //find the matches
       return str.replace(FORMAT_REGEX, function (m, format, type) {
           var replacer, ret;
           if (i &lt; len) {
               replacer = obj[i++];
           } else {
               //we are out of things to replace with so
               //just return the match?
               return m;
           }
           if (m === "%s" || m === "%d" || m === "%D") {
               //fast path!
               ret = replacer + "";
           } else if (m === "%Z") {
               ret = replacer.toUTCString();
           } else if (m === "%j") {
               try {
                   ret = JSON.stringify(replacer);
               } catch (e) {
                   throw new Error("comb.string.format : Unable to parse json from ", replacer);
               }
           } else {
               format = format.replace(/^\[|\]$/g, "");
               switch (type) {
               case "s":
                   ret = formatString(replacer, format);
                   break;
               case "d":
                   ret = formatNumber(replacer, format);
                   break;
               case "j":
                   ret = formatObject(replacer, format);
                   break;
               case "D":
                   ret = getDate().date.format(replacer, format);
                   break;
               case "Z":
                   ret = getDate().date.format(replacer, format, true);
                   break;
               }
           }
           return ret;
       });
   } else if (isHash(obj)) {
       return str.replace(INTERP_REGEX, function (m, format, value) {
           value = obj[value];
           if (!misc.isUndefined(value)) {
               if (format) {
                   if (comb.isString(value)) {
                       return formatString(value, format);
                   } else if (typeof value === "number") {
                       return formatNumber(value, format);
                   } else if (getDate().isDate(value)) {
                       return getDate().date.format(value, format);
                   } else if (typeof value === "object") {
                       return formatObject(value, format);
                   }
               } else {
                   return "" + value;
               }
           }
           return m;
       });
   } else {
       var args = pSlice.call(arguments).slice(1);
       return format(str, args);
   }
}
    

multiply Static Function Public


Defined base/string.js

Returns a string duplicated n times;

Example
comb.string.multiply("HELLO", 5) => "HELLOHELLOHELLOHELLOHELLO"
        
Arguments Source
function (str,times){
   var ret = [];
   if (times) {
       for (var i = 0; i &lt; times; i++) {
           ret.push(str);
       }
   }
   return ret.join("");
}
    

pad Static Function Public


Defined base/string.js

Pads a string

Example
comb.string.pad("STR", 5, " ", true) => "STR  "
comb.string.pad("STR", 5, "$") => "$$STR"
        
Arguments Returns Source
function (string,length,ch,end){
   string = "" + string; //check for numbers
   ch = ch || " ";
   var strLen = string.length;
   while (strLen &lt; length) {
       if (end) {
           string += ch;
       } else {
           string = ch + string;
       }
       strLen++;
   }
   return string;
}
    

style Static Function Public


Defined base/string.js

Styles a string according to the specified styles.

Example
//style a string red
comb.string.style('myStr', 'red');
//style a string red and bold
comb.string.style('myStr', ['red', bold]);
        
Arguments Source
function (str,options){
   var ret = str;
   if (options) {
       if (ret instanceof Array) {
           ret = ret.map(function (s) {
               return style(s, options);
           });
       } else if (options instanceof Array) {
           options.forEach(function (option) {
               ret = style(ret, option);
           });
       } else if (options in styles) {
           ret = '\x1B[' + styles[options] + 'm' + str + '\x1B[0m';
       }
   }
   return ret;
}
    

toArray Static Function Public


Defined base/string.js

Converts a string to an array

Example
comb.string.toArray("a|b|c|d", "|") => ["a","b","c","d"]
comb.string.toArray("a", "|") => ["a"]
comb.string.toArray("", "|") => []
        
Arguments Source
function (testStr,delim){
   var ret = [];
   if (testStr) {
       if (testStr.indexOf(delim) > 0) {
           ret = testStr.replace(/\s+/g, "").split(delim);
       } else {
           ret = [testStr];
       }
   }
   return ret;
}
    

truncate Static Function Public


Defined base/string.js

Truncates a string to the specified length.

Example
//from the beginning
comb.string.truncate("abcdefg", 3) => "abc";
//from the end
comb.string.truncate("abcdefg", 3,true) => "efg"
//omit the length
comb.string.truncate("abcdefg") => "abcdefg"
        
Arguments Returns Source
function (string,length,end){
   var ret = string;
   if (comb.isString(ret)) {
       if (string.length > length) {
           if (end) {
               var l = string.length;
               ret = string.substring(l - length, l);
           } else {
               ret = string.substring(0, length);
           }
       }
   } else {
       ret = truncate("" + ret, length);
   }
   return ret;
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta