1 | ;
|
2 | /**
|
3 | * Copyright 2020 Google LLC
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.snakeToCamelCase = exports.camelToSnakeCase = void 0;
|
19 | /**
|
20 | * Converts a given string from camelCase (used by protobuf.js and in JSON)
|
21 | * to snake_case (normally used in proto definitions).
|
22 | */
|
23 | function camelToSnakeCase(str) {
|
24 | // Keep the first position capitalization, otherwise decapitalize with underscore.
|
25 | return str.replace(/(?!^)[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
26 | }
|
27 | exports.camelToSnakeCase = camelToSnakeCase;
|
28 | /**
|
29 | * Capitalizes the first character of the given string.
|
30 | */
|
31 | function capitalize(str) {
|
32 | if (str.length === 0) {
|
33 | return str;
|
34 | }
|
35 | return str[0].toUpperCase() + str.slice(1);
|
36 | }
|
37 | /**
|
38 | * Converts a given string from snake_case (normally used in proto definitions) to
|
39 | * camelCase (used by protobuf.js)
|
40 | */
|
41 | function snakeToCamelCase(str) {
|
42 | // split on spaces, non-alphanumeric, or capital letters
|
43 | const splitted = str
|
44 | .split(/(?=[A-Z])|[\s\W_]+/)
|
45 | .filter(w => w.length > 0)
|
46 | // Keep the capitalization for the first split.
|
47 | .map((word, index) => (index === 0 ? word : word.toLowerCase()));
|
48 | if (splitted.length === 0) {
|
49 | return str;
|
50 | }
|
51 | return [splitted[0], ...splitted.slice(1).map(capitalize)].join('');
|
52 | }
|
53 | exports.snakeToCamelCase = snakeToCamelCase;
|
54 | //# sourceMappingURL=util.js.map |
\ | No newline at end of file |