From 7b6586d85baa946199fc2c75b5c93705b50d286c Mon Sep 17 00:00:00 2001 From: Tim Robinson Date: Mon, 30 Dec 2013 19:54:11 +1000 Subject: [PATCH 1/2] Made label text for camel case or dash separated field names format nicely. --- lib/fields.js | 4 ++-- test/test-fields.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/fields.js b/lib/fields.js index 47b6b72..b30ee97 100644 --- a/lib/fields.js +++ b/lib/fields.js @@ -14,7 +14,7 @@ var forms = require('./forms'), return copy; }, {}); }, - underscoreRegExp = /_/g; + underscoreRegExp = /[_-]/g; exports.string = function (opt) { @@ -70,7 +70,7 @@ exports.string = function (opt) { return this.error ? '

' + this.error + '

' : ''; }; 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(underscoreRegExp, ' ').replace(/([a-z])([A-Z])/g, function(match, arg1, arg2) { return arg1 + " " + arg2.toLowerCase()}) : ''); }; f.labelHTML = function (name, id) { if (this.widget.type === 'hidden') { return ''; } diff --git a/test/test-fields.js b/test/test-fields.js index 7151c41..b30d609 100644 --- a/test/test-fields.js +++ b/test/test-fields.js @@ -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( From 0b89ad41a0a69f347f39249e61637ddae56f6e5a Mon Sep 17 00:00:00 2001 From: Tim Robinson Date: Mon, 30 Dec 2013 20:19:41 +1000 Subject: [PATCH 2/2] Renaming variables. --- lib/fields.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fields.js b/lib/fields.js index b30ee97..880c872 100644 --- a/lib/fields.js +++ b/lib/fields.js @@ -14,7 +14,7 @@ var forms = require('./forms'), return copy; }, {}); }, - underscoreRegExp = /[_-]/g; + nameSeparatorRegExp = /[_-]/g; exports.string = function (opt) { @@ -70,7 +70,7 @@ exports.string = function (opt) { return this.error ? '

' + this.error + '

' : ''; }; f.labelText = function (name) { - return this.label || (name ? name[0].toUpperCase() + name.substr(1).replace(underscoreRegExp, ' ').replace(/([a-z])([A-Z])/g, function(match, arg1, arg2) { return arg1 + " " + arg2.toLowerCase()}) : ''); + 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 ''; }