String utilities
Source{ pad: pad, truncate: truncate, format: format, toArray: toArray, multiply: multiply, style: style, escapeHtml: escapeHtml }
Escapes an HTML string by replacing <>&" characters.
Examplecomb.string.escapeHtml('<script>alert("test")</script>'); // <script>alert("test")</script>Arguments
The string to escape.
function (str){ return String(str) .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, '''); }
Formats a string with the specified format
Format String
s
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
the string to format, if you want to use a spacing character as padding (other than \s) then put your format in brackets.
-
: left justified+
or <space>
: signed number if space is used the number will use a extra <space>
instead of a +
<Char>
: padding character Excludes d,j,sNumber
: width.Number
: specify the precision of the numberthe parameters to replace in the string if an array is passed then the array is used sequentially if an object is passed then the object keys are used if a variable number of args are passed then they are used like an array
String
the formatted string
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 < 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); } }
Returns a string duplicated n times;
Examplecomb.string.multiply("HELLO", 5) => "HELLOHELLOHELLOHELLOHELLO"Arguments
function (str,times){ var ret = []; if (times) { for (var i = 0; i < times; i++) { ret.push(str); } } return ret.join(""); }
Pads a string
Examplecomb.string.pad("STR", 5, " ", true) => "STR " comb.string.pad("STR", 5, "$") => "$$STR"Arguments
the string to pad
the length of the string when padded
" "
] : character to pad the string with
false
] : if true then the padding is added to the end
String
the padded string
function (string,length,ch,end){ string = "" + string; //check for numbers ch = ch || " "; var strLen = string.length; while (strLen < length) { if (end) { string += ch; } else { string = ch + string; } strLen++; } return string; }
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
The string to style.
String|Array
: the style or styles to apply to a string. options include :
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; }
Converts a string to an array
Examplecomb.string.toArray("a|b|c|d", "|") => ["a","b","c","d"] comb.string.toArray("a", "|") => ["a"] comb.string.toArray("", "|") => []Arguments
String
: the string to parse
String
: the delimeter to use
function (testStr,delim){ var ret = []; if (testStr) { if (testStr.indexOf(delim) > 0) { ret = testStr.replace(/\s+/g, "").split(delim); } else { ret = [testStr]; } } return ret; }
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
the string to truncate
false
] : truncate starting at the end of the string
-1
] Number
: the max length of the string, if the string is shorter than the length then the string is returned.
String
the truncated string.
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; }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git