util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.camelToSnakeCase = camelToSnakeCase;
  4. exports.toCamelCase = toCamelCase;
  5. exports.toLowerCamelCase = toLowerCamelCase;
  6. exports.makeUUID = makeUUID;
  7. /**
  8. * Copyright 2020 Google LLC
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. const uuid_1 = require("uuid");
  23. function words(str, normalize = false) {
  24. if (normalize) {
  25. // strings like somethingABCSomething are special case for protobuf.js,
  26. // they should be split as "something", "abc", "something".
  27. // Deal with sequences of capital letters first.
  28. str = str.replace(/([A-Z])([A-Z]+)([A-Z])/g, (str) => {
  29. return (str[0] +
  30. str.slice(1, str.length - 1).toLowerCase() +
  31. str[str.length - 1]);
  32. });
  33. }
  34. // split on spaces, non-alphanumeric, or capital letters
  35. // note: we keep the capitalization of the first word (special case: IPProtocol)
  36. return str
  37. .split(/(?=[A-Z])|[^A-Za-z0-9.]+/)
  38. .filter(w => w.length > 0)
  39. .map((w, index) => (index === 0 ? w : w.toLowerCase()));
  40. }
  41. /**
  42. * Converts the first character of the given string to lower case.
  43. */
  44. function lowercase(str) {
  45. if (str.length === 0) {
  46. return str;
  47. }
  48. return str[0].toLowerCase() + str.slice(1);
  49. }
  50. /**
  51. * Converts a given string from camelCase (used by protobuf.js and in JSON)
  52. * to snake_case (normally used in proto definitions).
  53. */
  54. function camelToSnakeCase(str) {
  55. // Keep the first position capitalization, otherwise decapitalize with underscore.
  56. const wordsList = words(str);
  57. if (wordsList.length === 0) {
  58. return str;
  59. }
  60. const result = [wordsList[0]];
  61. result.push(...wordsList.slice(1).map(lowercase));
  62. return result.join('_');
  63. }
  64. /**
  65. * Capitalizes the first character of the given string.
  66. */
  67. function capitalize(str) {
  68. if (str.length === 0) {
  69. return str;
  70. }
  71. return str[0].toUpperCase() + str.slice(1);
  72. }
  73. /**
  74. * Converts a given string from snake_case (normally used in proto definitions) or
  75. * PascalCase (also used in proto definitions) to camelCase (used by protobuf.js).
  76. * Preserves capitalization of the first character.
  77. */
  78. function toCamelCase(str) {
  79. const wordsList = words(str, /*normalize:*/ true);
  80. if (wordsList.length === 0) {
  81. return str;
  82. }
  83. const result = [wordsList[0]];
  84. result.push(...wordsList.slice(1).map(w => {
  85. if (w.match(/^\d+$/)) {
  86. return '_' + w;
  87. }
  88. return capitalize(w);
  89. }));
  90. return result.join('');
  91. }
  92. /**
  93. * Converts a given string to lower camel case (forcing the first character to be
  94. * in lower case).
  95. */
  96. function toLowerCamelCase(str) {
  97. const camelCase = toCamelCase(str);
  98. if (camelCase.length === 0) {
  99. return camelCase;
  100. }
  101. return camelCase[0].toLowerCase() + camelCase.slice(1);
  102. }
  103. /**
  104. * Converts a given string to lower camel case (forcing the first character to be
  105. * in lower case).
  106. */
  107. function makeUUID() {
  108. return (0, uuid_1.v4)();
  109. }
  110. //# sourceMappingURL=util.js.map