Skip to content

Commit

Permalink
Merge pull request #25 from Medium/nick-camel
Browse files Browse the repository at this point in the history
Tweak how we handle casing
  • Loading branch information
nicks committed Jul 23, 2015
2 parents 717a4af + 37a3761 commit 3dae409
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ exports.extend = function (var_args) {
* @return {string}
*/
exports.toCamelCase = function(str) {
var lowerCase = str.toLowerCase()
return String(lowerCase).replace(/\_([a-z])/g, function(all, match) {
// If it's all upper-case, convert it to lower case
// (for constant names like TWO_WORDS)
if (str.toUpperCase() == str) {
str = str.toLowerCase()
}
return String(str).replace(/\_([a-z])/g, function(all, match) {
return match.toUpperCase()
})
}
Expand Down
54 changes: 54 additions & 0 deletions tests/casing_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

/**
* @fileoverview Tests casing rulesthat parsing works end-to-end.
*/

var fs = require('fs')
var parser = require('../lib/parser')
var path = require('path')


exports.testCasingMessage = function (test) {
var proto = parseFile('conventions.proto')

test.equal(proto.getPackage(), 'conventions')

var msg = proto.getMessage('Casing')

test.ok(msg.getField('normal_case'))
test.equal('normal_case', msg.getField('normal_case').toTemplateObject().name)
test.equal('NormalCase', msg.getField('normal_case').toTemplateObject().titleName)

test.ok(msg.getField('normal_case_ios'))
test.equal('normal_case_ios', msg.getField('normal_case_ios').toTemplateObject().name)
test.equal('NormalCaseIos', msg.getField('normal_case_ios').toTemplateObject().titleName)

test.ok(msg.getField('camelCase'))
test.equal('camelCase', msg.getField('camelCase').toTemplateObject().name)
test.equal('CamelCase', msg.getField('camelCase').toTemplateObject().titleName)

test.ok(msg.getField('camelCaseIOS'))
test.equal('camelCaseIOS', msg.getField('camelCaseIOS').toTemplateObject().name)
test.equal('CamelCaseIOS', msg.getField('camelCaseIOS').toTemplateObject().titleName)

test.ok(msg.getField('TitleCase'))
test.ok(msg.getField('TitleCaseIOS'))
test.ok(msg.getField('Weird_Case'))

test.done()
}

exports.testCasingEnum = function (test) {
var proto = parseFile('conventions.proto')

test.equal(proto.getPackage(), 'conventions')

var e = proto.getEnum('CasingEnum')
test.equal('TWO_WORDS', e.getValueForNumber(1).name)
test.equal('TwoWords', e.getValueForNumber(1).titleName)
test.done()
}

function parseFile(file) {
return parser(file, fs.readFileSync(path.join(__dirname, 'protos', file), 'utf8'))
}
16 changes: 16 additions & 0 deletions tests/protos/conventions.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

package conventions;

message Casing {
optional string normal_case = 1;
optional string normal_case_ios = 2;
optional string camelCase = 3;
optional string camelCaseIOS = 4;
optional string TitleCase = 5;
optional string TitleCaseIOS = 6;
optional string Weird_Case = 7;
}

enum CasingEnum {
TWO_WORDS = 1;
}

0 comments on commit 3dae409

Please sign in to comment.