Skip to content

Camel case conversion bug with numbers in field names #1009

Open
@allenhartwig

Description

@allenhartwig

protobuf.js version: 6.8.6

The snake to camel case conversion should remove all underscores from the field name and match the behavior of lodash's _.camelCase method. Currently, if there is a protobuf field with the name of street_1, protobufjs's camel casing of this field results in street_1 while lodash results in street1. I see lodash being correct here as a camel cased string should have no _ left in it after conversion.

const _ = require('lodash');
const text = "street_1"

var camelCaseRe = /_([a-z])/g;
const camelCase = function camelCase(str) {
    return str.substring(0, 1)
         + str.substring(1)
               .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
};
console.log(camelCase(text));
console.log(_.camelCase(text));

camelCase function from https://github.com/dcodeIO/protobuf.js/blob/c482a5b76fd57769eae4308793e3ff8725264664/src/util.js#L90-L101

While I feel the current behavior is a bug, fixing it could introduce a breaking change. It may be preferable to introduce a new argument to the load function so that a custom case transformation could be passed in to be used in lieu of util.camelCase.

I am happy to submit a PR, but would like to know what the desired resolution would be, to fix the behavior of util.camelCase or allow for custom case transformations via a load argument.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions