underscore.strings.js | |
---|---|
| |
| |
TODO
| (function(){
|
Establish the root object, | var root = this;
var strip = function(string) {
if (String.prototype.trim) {
return string.trim();
}
return string.replace(/^\s+/, '').replace(/\s+$/, '');
}
var str = { |
blankString#blank
Check if the string is "blank" - either empty (length of Example | blank: function(string) {
return /^\s*$/.test(string);
},
|
camelizeString#camelize
Converts a string separated by dashes into a camelCase equivalent.
For instance, Example | camelize: function(string) {
return string.replace(/-+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
},
|
capitalizeString#capitalize Capitalizes the first letter of a string and downcases all the others. Examples | capitalize: function(string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
},
|
dasherizeString#dasherize
Replaces every instance of the underscore character Examples | dasherize: function(string) {
return string.replace(/_/g, '-');
},
|
emptyString#empty Checks if the string is empty. Examples | empty: function(string) {
return string == ''
},
|
endsWithString#endsWith Checks if the string ends with substring. Example | endsWith: function (string, pattern) {
var d = string.length - pattern.length; |
We use | return d >= 0 && string.indexOf(pattern, d) === d;
},
|
escapeHTMLString#escapeHTML Converts HTML special characters to their entity equivalents. Examples | escapeHTML: function(string) {
return string.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
},
|
includeString#include Check if the string contains a substring. Examples | include: function(string, pattern) {
return string.indexOf(pattern) > -1;
},
|
toQueryParamsString#toQueryParams Parses a URI-like query string and returns an object composed of parameter/value pairs. Example | toQueryParams: function(string, separator) {
var match = _(string).strip().match(/([^?#]*)(#.*)?$/);
if (!match) return { };
return _(match[1].split(separator || '&')).reduce(function(hash, pair) {
if ((pair = pair.split('='))[0]) {
var key = decodeURIComponent(pair.shift()),
value = pair.length > 1 ? pair.join('=') : pair[0];
if (value != undefined) value = decodeURIComponent(value);
if (key in hash) {
if (!_.isArray(hash[key])) hash[key] = [hash[key]];
hash[key].push(value);
}
else hash[key] = value;
}
return hash;
}, {});
},
|
startsWithString#startsWith
Checks if the string starts with Example | startsWith: function(string, pattern) {
return string.lastIndexOf(pattern, 0) === 0;
},
|
stripString#strip Strips all leading and trailing whitespace from a string. Example | strip: strip,
|
truncateString#truncate Truncates a string to the given length and appends a suffix to it (indicating that it is only an excerpt). Example | truncate: function(string, length, truncation) {
length = length || 30;
truncation = _.isUndefined(truncation) ? '...' : truncation;
return string.length > length ?
string.slice(0, length - truncation.length) + truncation : String(string);
},
|
underscoreString#underscore
Converts a camelized string into a series of words separated by an
underscore Example | underscore: function(string) {
return string.replace(/::/g, '/')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
.replace(/([a-z\d])([A-Z])/g, '$1_$2')
.replace(/-/g, '_')
.toLowerCase();
},
|
unescapeHTMLString#unescapeHTML Converts the entity forms of special HTML characters to their normal form. Examples | unescapeHTML: function(string) {
return string.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
}
};
|
aliases
| str.parseQuery = str.toQueryParams;
root._.mixin(str);
}());
|