super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
35 lines (34 loc) • 837 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.snakeCase = snakeCase;
/**
* Converts string to snake case.
*
* @param string - The string to convert
* @returns The snake cased string
*
* @example
* ```ts
* snakeCase('Foo Bar');
* // => 'foo_bar'
*
* snakeCase('fooBar');
* // => 'foo_bar'
*
* snakeCase('--FOO-BAR--');
* // => 'foo_bar'
* ```
*/
function snakeCase(string) {
if (!string) {
return '';
}
// Convert camelCase to snake_case
const camelToSnake = string.replace(/([a-z0-9])([A-Z])/g, '$1_$2');
// Convert to lowercase and remove special characters
return camelToSnake
.toLowerCase()
.replace(/[\\s\\-]+/g, '_')
.replace(/[^a-z0-9_]/g, '')
.replace(/^_+|_+$/g, ''); // Remove leading/trailing underscores
}