9 describe("String Methods", function(){
13 it('should capitalize each word', function(){
14 expect('i like cookies'.capitalize()).toEqual('I Like Cookies');
15 expect('I Like cOOKIES'.capitalize()).toEqual('I Like COOKIES');
20 it('should convert a hyphenated string into a camel cased string', function(){
21 expect('i-like-cookies'.camelCase()).toEqual('iLikeCookies');
22 expect('I-Like-Cookies'.camelCase()).toEqual('ILikeCookies');
27 it('should convert a camel cased string into a hyphenated string', function(){
28 expect('iLikeCookies'.hyphenate()).toEqual('i-like-cookies');
29 expect('ILikeCookies'.hyphenate()).toEqual('-i-like-cookies');
34 it('should clean all extraneous whitespace from the string', function(){
35 expect(' i like cookies '.clean()).toEqual("i like cookies");
36 expect(' i\nlike \n cookies \n\t '.clean()).toEqual("i like cookies");
41 it('should trim left and right whitespace from the string', function(){
42 expect(' i like cookies '.trim()).toEqual('i like cookies');
43 expect(' i \tlike cookies '.trim()).toEqual('i \tlike cookies');
48 it('should return true if the string contains a string otherwise false', function(){
49 expect('i like cookies'.contains('cookies')).toBeTruthy();
50 expect('i,like,cookies'.contains('cookies')).toBeTruthy();
51 expect('mootools'.contains('inefficient javascript')).toBeFalsy();
54 it('should return true if the string constains the string and separator otherwise false', function(){
55 expect('i like cookies'.contains('cookies', ' ')).toBeTruthy();
56 expect('i like cookies'.contains('cookies', ',')).toBeFalsy();
58 expect('i,like,cookies'.contains('cookies', ' ')).toBeFalsy();
59 expect('i,like,cookies'.contains('cookies', ',')).toBeTruthy();
65 it('should return true if the test matches the string otherwise false', function(){
66 expect('i like teh cookies'.test('cookies')).toBeTruthy();
67 expect('i like cookies'.test('ke coo')).toBeTruthy();
68 expect('I LIKE COOKIES'.test('cookie', 'i')).toBeTruthy();
69 expect('i like cookies'.test('cookiez')).toBeFalsy();
72 it('should return true if the regular expression test matches the string otherwise false', function(){
73 expect('i like cookies'.test(/like/)).toBeTruthy();
74 expect('i like cookies'.test(/^l/)).toBeFalsy();
79 it('should convert the string into an integer', function(){
80 expect('10'.toInt()).toEqual(10);
81 expect('10px'.toInt()).toEqual(10);
82 expect('10.10em'.toInt()).toEqual(10);
85 it('should convert the string into an integer with a specific base', function(){
86 expect('10'.toInt(5)).toEqual(5);
91 it('should convert the string into a float', function(){
92 expect('10.11'.toFloat()).toEqual(10.11);
93 expect('10.55px'.toFloat()).toEqual(10.55);
98 it('should convert the string into a CSS hex string', function(){
99 expect('rgb(255,255,255)'.rgbToHex()).toEqual('#ffffff');
100 expect('rgb(255,255,255,0)'.rgbToHex()).toEqual('transparent');
105 it('should convert the CSS hex string into a CSS rgb string', function(){
106 expect('#fff'.hexToRgb()).toEqual('rgb(255,255,255)');
107 expect('ff00'.hexToRgb()).toEqual('rgb(255,0,0)');
108 expect('#000000'.hexToRgb()).toEqual('rgb(0,0,0)');
113 it('should substitute values from objects', function(){
114 expect('This is {color}.'.substitute({'color': 'blue'})).toEqual('This is blue.');
115 expect('This is {color} and {size}.'.substitute({'color': 'blue', 'size': 'small'})).toEqual('This is blue and small.');
118 it('should substitute values from arrays', function(){
119 expect('This is {0}.'.substitute(['blue'])).toEqual('This is blue.');
120 expect('This is {0} and {1}.'.substitute(['blue', 'small'])).toEqual('This is blue and small.');
123 it('should remove undefined values', function(){
124 expect('Checking {0}, {1}, {2}, {3} and {4}.'.substitute([1, 0, undefined, null])).toEqual('Checking 1, 0, , and .');
125 expect('This is {not-set}.'.substitute({})).toEqual('This is .');
128 it('should ignore escaped placeholders', function(){
129 expect('Ignore \\{this} but not {that}.'.substitute({'that': 'the others'})).toEqual('Ignore {this} but not the others.');
132 it('should substitute with a custom regex', function(){
133 var php = (/\$([\w-]+)/g);
134 expect('I feel so $language.'.substitute({'language': 'PHP'}, php)).toEqual('I feel so PHP.');
135 var ror = (/#\{([^}]+)\}/g);
136 expect('I feel so #{language}.'.substitute({'language': 'RoR'}, ror)).toEqual('I feel so RoR.');
139 it('should substitute without goofing up nested curly braces', function(){
140 expect("fred {is {not} very} cool".substitute({ 'is {not':'BROKEN' })).not.toEqual("fred BROKEN very} cool");
141 expect('this {should {break} mo} betta'.substitute({ 'break':'work' })).toEqual('this {should work mo} betta');