Skip to content

Commit

Permalink
Merge pull request #89 from timjrobinson/label-text
Browse files Browse the repository at this point in the history
Made label text for camel case or dash separated field names format nicely
  • Loading branch information
ljharb committed Jan 15, 2014
2 parents 57688e6 + 0b89ad4 commit e9b5fbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var forms = require('./forms'),
return copy;
}, {});
},
underscoreRegExp = /_/g;
nameSeparatorRegExp = /[_-]/g;


exports.string = function (opt) {
Expand Down Expand Up @@ -70,7 +70,7 @@ exports.string = function (opt) {
return this.error ? '<p class="' + htmlEscape(['error_msg'].concat(classes).join(' ')) + '">' + this.error + '</p>' : '';
};
f.labelText = function (name) {
return this.label || (name ? name[0].toUpperCase() + name.substr(1).replace(underscoreRegExp, ' ') : '');
return this.label || (name ? name[0].toUpperCase() + name.substr(1).replace(nameSeparatorRegExp, ' ').replace(/([a-z])([A-Z])/g, function(match, firstLetter, secondLetter) { return firstLetter + " " + secondLetter.toLowerCase()}) : '');
};
f.labelHTML = function (name, id) {
if (this.widget.type === 'hidden') { return ''; }
Expand Down
8 changes: 8 additions & 0 deletions test/test-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ exports['string parse'] = function (test) {
test.done();
};

exports['string labelText'] = function (test) {
test.equals(stringField.labelText('name'), 'Name');
test.equals(stringField.labelText('first_name'), 'First name');
test.equals(stringField.labelText('first-name'), 'First name');
test.equals(stringField.labelText('firstName'), 'First name');
test.done();
}

exports['string toHTML'] = function (test) {
test.expect(3);
test.equals(
Expand Down

0 comments on commit e9b5fbc

Please sign in to comment.