Skip to content

Commit 606fe8f

Browse files
committed
Added titleCase()-Method
1 parent 64fb3c6 commit 606fe8f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,24 @@ S('*').times(3).s //'***'
786786
```
787787

788788

789+
### - titleCase() ###
790+
791+
Returns a string with the first letter of each word uppercased, including hyphenated words
792+
793+
Example:
794+
795+
```javascript
796+
S('Like ice in the sunshine').titleCase().s // 'Like Ice In The Sunshine'
797+
S('data_rate').titleCase().s // 'Data_Rate'
798+
S('background-color').titleCase().s // 'Background-Color'
799+
S('-moz-something').titleCase().s // '-Moz-Something'
800+
S('_car_speed_').titleCase().s // '_Car_Speed_'
801+
S('yes_we_can').titleCase().s // 'Yes_We_Can
802+
803+
S(' capitalize dash-CamelCase_underscore trim ').humanize().titleCase().s // 'Capitalize Dash Camel Case Underscore Trim'
804+
```
805+
806+
789807
### - toBoolean() / toBool()
790808

791809
Converts a a logical truth string to boolean. That is: `true`, `1`, `'true'`, `'on'`, or `'yes'`.
@@ -1082,6 +1100,7 @@ If you contribute to this library, just modify `string.js`, `string.test.js`, an
10821100
- [*] [James Manning](https://github.com/jamesmanning)
10831101
- [*] [Nathan Friedly](https://github.com/nfriedly)
10841102
- [*] [Alison Rowland](https://github.com/arowla)
1103+
- [*] [Pascal Bihler](https://github.com/pbihler)
10851104

10861105

10871106

lib/string.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,18 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
451451
times: function(n) {
452452
return new this.constructor(new Array(n + 1).join(this.s));
453453
},
454+
455+
titleCase: function() {
456+
var s = this.s;
457+
if (s) {
458+
s = s.replace(/(^[a-z]| [a-z]|-[a-z]|_[a-z])/g,
459+
function($1){
460+
return $1.toUpperCase();
461+
}
462+
);
463+
}
464+
return new this.constructor(s);
465+
},
454466

455467
toBoolean: function() {
456468
if (typeof this.orig === 'string') {

test/string.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,31 @@
614614
T (S('*').times(3).s === '***');
615615
})
616616
})
617+
618+
describe('- titleCase()', function() {
619+
it('should upperCase all words in a camel cased string', function() {
620+
EQ (S('dataRate').titleCase().s, 'DataRate')
621+
EQ (S('CarSpeed').titleCase().s, 'CarSpeed')
622+
EQ (S('yesWeCan').titleCase().s, 'YesWeCan')
623+
EQ (S('backgroundColor').titleCase().s, 'BackgroundColor')
624+
})
625+
it('should upperCase all words in a string with spaces, underscores, or dashes', function() {
626+
EQ (S('Like ice in the sunshine').titleCase().s, 'Like Ice In The Sunshine')
627+
EQ (S('data_rate').titleCase().s, 'Data_Rate')
628+
EQ (S('background-color').titleCase().s, 'Background-Color')
629+
EQ (S('-moz-something').titleCase().s, '-Moz-Something')
630+
EQ (S('_car_speed_').titleCase().s, '_Car_Speed_')
631+
EQ (S('yes_we_can').titleCase().s, 'Yes_We_Can')
632+
})
633+
it('can be combined with humanize to create nice titles out of ugly developer strings', function() {
634+
EQ (S(' capitalize dash-CamelCase_underscore trim ').humanize().titleCase().s, 'Capitalize Dash Camel Case Underscore Trim')
635+
})
636+
it('does not fail on edge cases', function () {
637+
EQ (S('').titleCase().s,'')
638+
EQ (S(null).titleCase().s,null)
639+
EQ (S(undefined).titleCase().s,undefined)
640+
})
641+
})
617642

618643
describe('- toFloat([precision])', function() {
619644
it('should return the float value, wraps parseFloat', function() {

0 commit comments

Comments
 (0)