/lib/includes/js/mootools-core/Source/Types/String.js
JavaScript | 82 lines | 52 code | 15 blank | 15 comment | 6 complexity | 241e1bc97829066f3a7bdd1a5c201227 MD5 | raw file
- /*
- ---
- name: String
- description: Contains String Prototypes like camelCase, capitalize, test, and toInt.
- license: MIT-style license.
- requires: Type
- provides: String
- ...
- */
- String.implement({
- test: function(regex, params){
- return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
- },
- contains: function(string, separator){
- return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1;
- },
- trim: function(){
- return this.replace(/^\s+|\s+$/g, '');
- },
- clean: function(){
- return this.replace(/\s+/g, ' ').trim();
- },
- camelCase: function(){
- return this.replace(/-\D/g, function(match){
- return match.charAt(1).toUpperCase();
- });
- },
- hyphenate: function(){
- return this.replace(/[A-Z]/g, function(match){
- return ('-' + match.charAt(0).toLowerCase());
- });
- },
- capitalize: function(){
- return this.replace(/\b[a-z]/g, function(match){
- return match.toUpperCase();
- });
- },
- escapeRegExp: function(){
- return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
- },
- toInt: function(base){
- return parseInt(this, base || 10);
- },
- toFloat: function(){
- return parseFloat(this);
- },
- hexToRgb: function(array){
- var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
- return (hex) ? hex.slice(1).hexToRgb(array) : null;
- },
- rgbToHex: function(array){
- var rgb = this.match(/\d{1,3}/g);
- return (rgb) ? rgb.rgbToHex(array) : null;
- },
- substitute: function(object, regexp){
- return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
- if (match.charAt(0) == '\\') return match.slice(1);
- return (object[name] != null) ? object[name] : '';
- });
- }
- });