File tree Expand file tree Collapse file tree 3 files changed +74
-1
lines changed Expand file tree Collapse file tree 3 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,10 @@ var fmt = Ember.String.fmt,
1818 capitalize = Ember . String . capitalize ,
1919 classify = Ember . String . classify ;
2020
21+ if ( Ember . FEATURES . isEnabled ( "string-humanize" ) ) {
22+ var humanize = Ember . String . humanize ;
23+ }
24+
2125if ( Ember . EXTEND_PROTOTYPES === true || Ember . EXTEND_PROTOTYPES . String ) {
2226
2327 /**
@@ -110,5 +114,17 @@ if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.String) {
110114 return capitalize ( this ) ;
111115 } ;
112116
117+ if ( Ember . FEATURES . isEnabled ( "string-humanize" ) ) {
118+ /**
119+ See [Ember.String.humanize](/api/classes/Ember.String.html#method_humanize).
120+
121+ @method humanize
122+ @for String
123+ */
124+ String . prototype . humanize = function ( ) {
125+ return humanize ( this ) ;
126+ } ;
127+ }
128+
113129}
114130
Original file line number Diff line number Diff line change @@ -245,5 +245,31 @@ Ember.String = {
245245 capitalize : function ( str ) {
246246 return str . charAt ( 0 ) . toUpperCase ( ) + str . substr ( 1 ) ;
247247 }
248-
249248} ;
249+
250+ if ( Ember . FEATURES . isEnabled ( "string-humanize" ) ) {
251+ /**
252+ Returns the Humanized form of a string
253+
254+ Replaces underscores with spaces, and capitializes first character
255+ of string. Also strips "_id" suffixes.
256+
257+ ```javascript
258+ 'first_name'.humanize() // 'First name'
259+ 'user_id'.humanize() // 'User'
260+ ```
261+
262+ @method humanize
263+ @param {String } str The string to humanize.
264+ @return {String } The humanized string.
265+ */
266+
267+ Ember . String . humanize = function ( str ) {
268+ return str . replace ( / _ i d $ / , '' ) .
269+ replace ( / _ / g, ' ' ) .
270+ replace ( / ^ \w / g, function ( s ) {
271+ return s . toUpperCase ( ) ;
272+ } ) ;
273+ } ;
274+ }
275+
Original file line number Diff line number Diff line change 1+ if ( Ember . FEATURES . isEnabled ( "string-humanize" ) ) {
2+ module ( 'Ember.String.humanize' ) ;
3+
4+ test ( "with underscored string" , function ( ) {
5+ deepEqual ( Ember . String . humanize ( 'first_name' ) , 'First name' ) ;
6+ if ( Ember . EXTEND_PROTOTYPES ) {
7+ deepEqual ( 'first_name' . humanize ( ) , 'First name' ) ;
8+ }
9+ } ) ;
10+
11+ test ( "with multiple underscored string" , function ( ) {
12+ deepEqual ( Ember . String . humanize ( 'first_and_last_name' ) , 'First and last name' ) ;
13+ if ( Ember . EXTEND_PROTOTYPES ) {
14+ deepEqual ( 'first_and_last_name' . humanize ( ) , 'First and last name' ) ;
15+ }
16+ } ) ;
17+
18+ test ( "with underscored id string" , function ( ) {
19+ deepEqual ( Ember . String . humanize ( 'user_id' ) , 'User' ) ;
20+ if ( Ember . EXTEND_PROTOTYPES ) {
21+ deepEqual ( 'user_id' . humanize ( ) , 'User' ) ;
22+ }
23+ } ) ;
24+
25+ test ( "with humanized string" , function ( ) {
26+ deepEqual ( Ember . String . humanize ( 'First name' ) , 'First name' ) ;
27+ if ( Ember . EXTEND_PROTOTYPES ) {
28+ deepEqual ( 'First name' . humanize ( ) , 'First name' ) ;
29+ }
30+ } ) ;
31+ }
You can’t perform that action at this time.
0 commit comments