Skip to content

Commit

Permalink
Merge pull request #23 from Medium/giannic-ios-enum
Browse files Browse the repository at this point in the history
add TitleCase to enum names to support iOS enum conventions
  • Loading branch information
giannic committed Jul 20, 2015
2 parents b252a15 + 3e3c84c commit 1bfbd7e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/descriptors/EnumDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var util = require('util')
var Descriptor = require('./Descriptor')
var helper = require('../helper')

/**
* @param {string} name Name of the enumeration
Expand Down Expand Up @@ -63,7 +64,11 @@ EnumDescriptor.prototype.getNames = function () {

EnumDescriptor.prototype.getValueForNumber = function (number) {
var name = this._names[number]
return name ? { name: name, number: number } : null
return name ? {
name: name,
titleName: helper.toTitleCase(name),
number: number
} : null
}


Expand Down
3 changes: 2 additions & 1 deletion lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ exports.extend = function (var_args) {
* @return {string}
*/
exports.toCamelCase = function(str) {
return String(str).replace(/\_([a-z])/g, function(all, match) {
var lowerCase = str.toLowerCase()
return String(lowerCase).replace(/\_([a-z])/g, function(all, match) {
return match.toUpperCase()
})
}
Expand Down
7 changes: 4 additions & 3 deletions tests/project_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ exports.testGetProtos = function (test) {
var enums = personProtos[0].toTemplateObject().messages[0].enums
test.equal(1, enums && enums.length)
test.deepEqual(
{name: 'PhoneType', values: [{name: 'MOBILE', number: 0},
{name: 'HOME', number: 1},
{name: 'WORK', number: 2}]},
{name: 'PhoneType', values: [{name: 'MOBILE', titleName: 'Mobile', number: 0},
{name: 'HOME', titleName: 'Home', number: 1},
{name: 'WORK', titleName: 'Work', number: 2},
{name: 'WORK_FAX', titleName: 'WorkFax', number: 3}]},
enums[0])

test.done()
Expand Down
1 change: 1 addition & 0 deletions tests/protos/person.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message Person {
// Comment
HOME = 1;
WORK = 2;
WORK_FAX = 3;
}

message PhoneNumber {
Expand Down

0 comments on commit 1bfbd7e

Please sign in to comment.