diff --git a/src/snakeCase.ts b/src/snakeCase.ts new file mode 100644 index 0000000..cb3d213 --- /dev/null +++ b/src/snakeCase.ts @@ -0,0 +1,8 @@ +const snakeCase: (val: string) => string = (val: string): string => + val + .trim() + .toLowerCase() + .replace(/[^a-zA-Z0-9\p{L}]+/gu, '_') + .replace(/(\s_)/g, (match, p1) => p1.trimStart()); + +export default snakeCase; diff --git a/test/snakeCase.test.ts b/test/snakeCase.test.ts new file mode 100644 index 0000000..63d46fb --- /dev/null +++ b/test/snakeCase.test.ts @@ -0,0 +1,22 @@ +import snakeCase from '../src/snakeCase'; + +test.each([ + [ 'abc def', 'abc_def'], + [ 'abc-def', 'abc_def'], + [ 'abc_def', 'abc_def'], + [ 'abc!def', 'abc_def'], + [ 'abc@def', 'abc_def'], + [ 'abc%def', 'abc_def'], + [ 'abc^def', 'abc_def'], + [ 'abc&def', 'abc_def'], + [ 'abc*def', 'abc_def'], + [ 'abc(def', 'abc_def'], + [ 'abc)def', 'abc_def'], + [ 'a品cd!e f', 'a品cd_e_f'], + [ 'ն !w**sss', 'ն_w_sss'], + [ '0 a s w', '0_a_s_w'], + [ '0 a 1 w', '0_a_1_w'], + [ '0 a 1 w', '0_a_1_w'] +])('it maps the %s object keys entities', (val, expectedVal) => { + expect(snakeCase(val)).toBe(expectedVal); +});