typia
Version:
Superfast runtime validators with only one line
54 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._notationSnake = void 0;
const _notationSnakeWord = (str) => {
const indexes = [];
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (65 <= code && code <= 90)
indexes.push(i);
}
for (let i = indexes.length - 1; i > 0; --i) {
const now = indexes[i];
const prev = indexes[i - 1];
if (now - prev === 1)
indexes.splice(i, 1);
}
if (indexes.length !== 0 && indexes[0] === 0)
indexes.splice(0, 1);
if (indexes.length === 0)
return str.toLowerCase();
let ret = "";
for (let i = 0; i < indexes.length; i++) {
const first = i === 0 ? 0 : indexes[i - 1];
const last = indexes[i];
ret += str.substring(first, last).toLowerCase();
ret += "_";
}
ret += str.substring(indexes[indexes.length - 1]).toLowerCase();
return ret;
};
const _notationSnake = (str) => {
if (str.length === 0)
return str;
// PREFIX
let prefix = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === "_")
prefix += "_";
else
break;
}
if (prefix.length !== 0)
str = str.substring(prefix.length);
// SNAKE CASE
//
// Run the case-boundary walk within each underscore-delimited segment, not
// over the whole segment at once. Lowercasing a segment atomically would drop
// the camelCase boundary inside it (`["fooBar", "baz"]` -> `["foobar", "baz"]`
// -> `foobar_baz`), diverging from `SnakeCase<T>`; the per-segment walk keeps
// it (`["foo_bar", "baz"]` -> `foo_bar_baz`).
return `${prefix}${str.split("_").map(_notationSnakeWord).join("_")}`;
};
exports._notationSnake = _notationSnake;
//# sourceMappingURL=_notationSnake.js.map