66a22c74d0
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
17 lines
680 B
JavaScript
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
|
|
};
|