2012-02-09 08:54:30 +00:00
|
|
|
(function() {
|
|
|
|
|
|
2013-05-14 16:34:45 +00:00
|
|
|
/* _ES5_COMPAT_START_ */
|
2010-06-17 14:00:47 +00:00
|
|
|
if (!String.prototype.trim) {
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
|
|
|
|
* Trims a string (removing whitespace from the beginning and the end)
|
2013-04-24 16:58:04 +00:00
|
|
|
* @function external:String#trim
|
2010-10-19 20:27:24 +00:00
|
|
|
* @see <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim">String#trim on MDN</a>
|
2010-10-15 02:16:24 +00:00
|
|
|
*/
|
2010-06-17 14:00:47 +00:00
|
|
|
String.prototype.trim = function () {
|
2010-09-14 22:57:55 +00:00
|
|
|
// this trim is not fully ES3 or ES5 compliant, but it should cover most cases for now
|
|
|
|
|
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '');
|
2010-06-17 14:00:47 +00:00
|
|
|
};
|
|
|
|
|
}
|
2013-05-14 16:34:45 +00:00
|
|
|
/* _ES5_COMPAT_END_ */
|
2010-06-17 14:00:47 +00:00
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
|
|
|
|
* Camelizes a string
|
|
|
|
|
* @memberOf fabric.util.string
|
|
|
|
|
* @param {String} string String to camelize
|
|
|
|
|
* @return {String} Camelized version of a string
|
|
|
|
|
*/
|
2010-06-17 14:00:47 +00:00
|
|
|
function camelize(string) {
|
|
|
|
|
return string.replace(/-+(.)?/g, function(match, character) {
|
|
|
|
|
return character ? character.toUpperCase() : '';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
|
|
|
|
* Capitalizes a string
|
|
|
|
|
* @memberOf fabric.util.string
|
|
|
|
|
* @param {String} string String to capitalize
|
2013-11-03 12:09:49 +00:00
|
|
|
* @param {Boolean} [firstLetterOnly] If true only first letter is capitalized and other letters stay untouched,
|
|
|
|
|
* if false first letter is capitalized and other letters are converted to lowercase.
|
2010-10-15 02:16:24 +00:00
|
|
|
* @return {String} Capitalized version of a string
|
|
|
|
|
*/
|
2013-11-03 12:09:49 +00:00
|
|
|
function capitalize(string, firstLetterOnly) {
|
|
|
|
|
return string.charAt(0).toUpperCase() + (firstLetterOnly ? string.slice(1) : string.slice(1).toLowerCase());
|
2010-06-17 14:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Escapes XML in a string
|
|
|
|
|
* @memberOf fabric.util.string
|
|
|
|
|
* @param {String} string String to escape
|
|
|
|
|
* @return {String} Escaped version of a string
|
|
|
|
|
*/
|
2012-02-09 08:54:30 +00:00
|
|
|
function escapeXml(string) {
|
2012-06-07 13:24:00 +00:00
|
|
|
return string.replace(/&/g, '&')
|
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>');
|
2012-02-09 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-24 16:58:04 +00:00
|
|
|
/**
|
|
|
|
|
* String utilities
|
|
|
|
|
* @namespace fabric.util.string
|
|
|
|
|
*/
|
2010-07-10 01:50:13 +00:00
|
|
|
fabric.util.string = {
|
2010-06-17 14:00:47 +00:00
|
|
|
camelize: camelize,
|
2012-02-09 08:54:30 +00:00
|
|
|
capitalize: capitalize,
|
|
|
|
|
escapeXml: escapeXml
|
|
|
|
|
};
|
|
|
|
|
}());
|