Files
rpg-companion-sillytavern/src/utils/transformations.js
T
2026-02-25 23:58:02 +01:00

17 lines
680 B
JavaScript

const toSnake = (str) => str
// replace any sequence of non-alphanumeric characters with a single underscore
.replace(/[^0-9A-Za-z]+/g, '_')
// insert underscore between a lower-case letter/digit and an upper-case letter (but not between consecutive uppers)
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
// collapse multiple underscores
.replace(/_+/g, '_')
// trim leading/trailing underscores
.replace(/^_+|_+$/g, '')
// finally, lowercase the result
.toLowerCase();
export const safeToSnake = (str) => {
const res = toSnake(str);
return (res.length >= 2) ? res : str; // considering element with one symbol is too short to be safe
};