added Browser.version to the docs
[mootools/dkf.git] / Source / Types / String.js
blob8bc9cd7e8866d6fb49d9f47705668f433bfad186
1 /*
2 ---
4 name: String
6 description: Contains String Prototypes like camelCase, capitalize, test, and toInt.
8 license: MIT-style license.
10 requires: Type
12 provides: String
14 ...
17 String.implement({
19         test: function(regex, params){
20                 return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
21         },
23         contains: function(string, separator){
24                 return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1;
25         },
27         trim: function(){
28                 return this.replace(/^\s+|\s+$/g, '');
29         },
31         clean: function(){
32                 return this.replace(/\s+/g, ' ').trim();
33         },
35         camelCase: function(){
36                 return this.replace(/-\D/g, function(match){
37                         return match.charAt(1).toUpperCase();
38                 });
39         },
41         hyphenate: function(){
42                 return this.replace(/[A-Z]/g, function(match){
43                         return ('-' + match.charAt(0).toLowerCase());
44                 });
45         },
47         capitalize: function(){
48                 return this.replace(/\b[a-z]/g, function(match){
49                         return match.toUpperCase();
50                 });
51         },
53         escapeRegExp: function(){
54                 return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
55         },
57         toInt: function(base){
58                 return parseInt(this, base || 10);
59         },
61         toFloat: function(){
62                 return parseFloat(this);
63         },
65         hexToRgb: function(array){
66                 var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
67                 return (hex) ? hex.slice(1).hexToRgb(array) : null;
68         },
70         rgbToHex: function(array){
71                 var rgb = this.match(/\d{1,3}/g);
72                 return (rgb) ? rgb.rgbToHex(array) : null;
73         },
75         substitute: function(object, regexp){
76                 return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
77                         if (match.charAt(0) == '\\') return match.slice(1);
78                         return (object[name] != null) ? object[name] : '';
79                 });
80         }
82 });