Skip to content

snake_case to camelCase fails to match _[letter][number] #714

@brycethomas

Description

@brycethomas

issue in the master revision at time of writing: https://github.com/dcodeIO/protobuf.js/blob/2a74fbf551e934b3212273e6a28ad65ac4436faf/src/parse.js

camelCase(time_t0) --> timeT0 (expected)
camelCase(time_t0) --> time_t0 (actual)

Repro

From parse.js, copy this snippet into Chrome dev tools:

var camelCaseRe = /_([a-z])(?=[a-z]|$)/g;

function camelCase(str) {
    return str.substring(0,1)
         + str.substring(1)
               .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
}

Then evaluate:

camelCase('time_t0')

Root Cause

The issue is that the second regex capture group doesn't include numbers; note that the current regex works fine where two or more letters precede the number:

camelCase('time_t0') --> time_t0 (incorrect)
camelCase('time_tt0') --> timeTt0 (correct)

Not sure why the current regex checks two characters after the underscore—could it perhaps be reworked to only check the one? cf. Google's C++ implementation, which simply checks that the letter immediately following the underscore is in range a-z: https://github.com/google/protobuf/blob/7f3e23707122f31ebfa58b7280bd56cbe77cb44e/src/google/protobuf/util/field_mask_util.cc#L63

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions