typia
Version:
Superfast runtime validators with only one line
35 lines • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._notationCamel = void 0;
const __notationRename_1 = require("./private/__notationRename");
const _notationCamelSnake = (str) => {
while (str.startsWith("_"))
str = str.substring(1);
if (str.length === 0)
return "";
const index = str.indexOf("_");
if (index < 0)
return str.toLowerCase();
const middle = str[index + 1];
// A trailing underscore has no character to uppercase, so `CamelizeSnakeString`
// falls through to lowercasing the whole key (`foo_` -> `foo_`).
if (middle === undefined)
return str.toLowerCase();
// A doubled underscore collapses to a single one before continuing.
return middle === "_"
? _notationCamelSnake(`${str.substring(0, index)}_${str.substring(index + 2)}`)
: `${str
.substring(0, index)
.toLowerCase()}${middle.toUpperCase()}${_notationCamelSnake(str.substring(index + 2))}`;
};
exports._notationCamel = (0, __notationRename_1.__notationRename)({
// `CamelCase<T>` lowercases an all-caps key and otherwise lowercases only the
// first character of an underscore-free key (`HTTP` -> `http`, `userID` stays).
plain: (str) => str === str.toUpperCase()
? str.toLowerCase()
: `${str[0].toLowerCase()}${str.substring(1)}`,
// With an underscore present, the character after each underscore is
// uppercased and the rest lowercased, matching `CamelizeSnakeString`.
snake: _notationCamelSnake,
});
//# sourceMappingURL=_notationCamel.js.map