MCPcopy Index your code
hub / github.com/RossWilliams/ts-case-convert

github.com/RossWilliams/ts-case-convert @v2.3.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.3.1 ↗ · + Follow
26 symbols 61 edges 10 files 0 documented · 0% 2 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ts-case-convert

npm version npm bundle size npm type definitions GitHub package.json dependency version (dev dep on branch) Documentation Maintenance License: Apache--2.0

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.

Usage

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'

No Number Splitting

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'

Run tests

pnpm run test

Documentation

See tests, bug regression tests, and no-split-numbers tests.

📝 License

Copyright © 2021 Ross Williams.

This project is Apache--2.0 licensed.

Extension points exported contracts — how you extend this code

I24 (Interface)
(no doc)
test/caseConvert.test.ts
MyObject (Interface)
(no doc)
test/caseConvert.bugs.test.ts
I242 (Interface)
(no doc)
test/caseConvert.test.ts
MyObjectWithArray (Interface)
(no doc)
test/caseConvert.bugs.test.ts
I244 (Interface)
(no doc)
test/caseConvert.test.ts
MyPascalObjectWithArray (Interface)
(no doc)
test/caseConvert.bugs.test.ts
I2442 (Interface)
(no doc)
test/caseConvert.test.ts
ArrayTypes (Interface)
(no doc)
test/caseConvert.bugs.test.ts

Core symbols most depended-on inside this repo

toCamel
called by 48
src/caseConvert.ts
toSnake
called by 29
src/caseConvert.ts
toPascal
called by 23
src/caseConvert.ts
objectToCamel
called by 11
src/caseConvert.ts
objectToSnake
called by 11
src/caseConvert.ts
convertObject
called by 6
src/caseConvert.ts
toScreamingSnake
called by 5
src/caseConvert.ts
toSnakeWithSplitNumbers
called by 4
src/caseConvert.ts

Shape

Function 16
Interface 10

Languages

TypeScript100%

Modules by API surface

src/caseConvert.ts15 symbols
test/caseConvert.test.ts6 symbols
test/caseConvert.bugs.test.ts4 symbols
scripts/bump-version.js1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add ts-case-convert \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page