ts-case-convert is a zero dependency implementation for converting object keys between camelCase, snake_case, PascalCase, and SCREAMING_SNAKE_CASE while preserving TypeScript type information, code completion, and type validation. See tests for detailed conversion tests.
const camel = objectToCamel({
hello_world: 'helloWorld',
a_number: 5,
an_array: [1, 2, 4],
null_object: null,
undef_object: undefined,
an_array_of_objects: [{ a_b: 'ab', a_c: 'ac' }],
an_object: {
a_1: 'a1',
a_2: 'a2',
},
});
type CheckCamel = typeof camel.anArrayOfObjects[0]['aB']; // -> 'string'
const ab: CheckCamel = camel.anArrayOfObjects[0]['aB']; // -> valid
console.log(camel.anArrayOfObjects.aB); // -> 'ab'
const snake = objectToSnake({
helloWorld: 'helloWorld',
aNumber: 5,
anArray: [1, 2, 4],
nullObject: null,
undefObject: undefined,
anArrayOfObjects: [{ aB: 'ab', aC: 'ac' }],
anObject: {
A1: 'a_1',
A2: 'a_2',
},
});
type CheckSnake = typeof snake.an_array_of_objects[0]['a_b']; // -> 'string'
const ab: CheckSnake = snake.an_array_of_objects[0]['a_b']; // -> valid
console.log(snake.an_array_of_objects.a_b); // -> 'ab'
const screamingSnake = objectToScreamingSnake({
helloWorld: 'helloWorld',
s3Id: 'id',
nestedObject: {
apiVersion: 'v1',
},
});
type CheckScreamingSnake = typeof screamingSnake.NESTED_OBJECT.API_VERSION; // -> 'string'
console.log(screamingSnake.S3_ID); // -> 'id'The default snake case conversion splits lowercase letters from following numbers:
import { toSnake } from 'ts-case-convert';
console.log(toSnake('myItem1')); // -> 'my_item_1'Use the no-split-numbers import path when numbers should stay attached to the preceding word. It exports the same runtime function and type names as the default entry point.
import {
objectToScreamingSnake,
objectToSnake,
toScreamingSnake,
toSnake,
} from 'ts-case-convert/no-split-numbers';
console.log(toSnake('myItem1')); // -> 'my_item1'
console.log(toScreamingSnake('myItem1')); // -> 'MY_ITEM1'
const output = objectToSnake({
myItem1: 'value',
nestedObject: {
apiVersion2: 'v2',
},
});
console.log(output.my_item1); // -> 'value'
const screaming = objectToScreamingSnake({
myItem1: 'value',
});
console.log(screaming.MY_ITEM1); // -> 'value'pnpm run testSee tests, bug regression tests, and no-split-numbers tests.
Copyright © 2021 Ross Williams.
This project is Apache--2.0 licensed.