Skip to content

Add Ember.String.humanize #3224

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Sep 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/ember-runtime/lib/ext/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var fmt = Ember.String.fmt,
capitalize = Ember.String.capitalize,
classify = Ember.String.classify;

if (Ember.FEATURES.isEnabled("string-humanize")) {
var humanize = Ember.String.humanize;
}

if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.String) {

/**
Expand Down Expand Up @@ -110,5 +114,17 @@ if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.String) {
return capitalize(this);
};

if (Ember.FEATURES.isEnabled("string-humanize")) {
/**
See [Ember.String.humanize](/api/classes/Ember.String.html#method_humanize).

@method humanize
@for String
*/
String.prototype.humanize = function() {
return humanize(this);
};
}

}

28 changes: 27 additions & 1 deletion packages/ember-runtime/lib/system/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,31 @@ Ember.String = {
capitalize: function(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
}

};

if (Ember.FEATURES.isEnabled("string-humanize")) {
/**
Returns the Humanized form of a string

Replaces underscores with spaces, and capitializes first character
of string. Also strips "_id" suffixes.

```javascript
'first_name'.humanize() // 'First name'
'user_id'.humanize() // 'User'
```

@method humanize
@param {String} str The string to humanize.
@return {String} The humanized string.
*/

Ember.String.humanize = function(str) {
return str.replace(/_id$/, '').
replace(/_/g, ' ').
replace(/^\w/g, function(s){
return s.toUpperCase();
});
};
}

31 changes: 31 additions & 0 deletions packages/ember-runtime/tests/system/string/humanize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if (Ember.FEATURES.isEnabled("string-humanize")) {
module('Ember.String.humanize');

test("with underscored string", function() {
deepEqual(Ember.String.humanize('first_name'), 'First name');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('first_name'.humanize(), 'First name');
}
});

test("with multiple underscored string", function() {
deepEqual(Ember.String.humanize('first_and_last_name'), 'First and last name');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('first_and_last_name'.humanize(), 'First and last name');
}
});

test("with underscored id string", function() {
deepEqual(Ember.String.humanize('user_id'), 'User');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('user_id'.humanize(), 'User');
}
});

test("with humanized string", function() {
deepEqual(Ember.String.humanize('First name'), 'First name');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('First name'.humanize(), 'First name');
}
});
}