Skip to content

Convert between camelCase, snake_case, and PascalCase while preserving type information

License

Notifications You must be signed in to change notification settings

filipemir/typescript-case-convert

This branch is 6 commits ahead of, 22 commits behind RossWilliams/ts-case-convert:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ee9b6e1 · Jan 6, 2022

History

52 Commits
Jul 2, 2021
Jul 2, 2021
Jun 8, 2021
Jan 5, 2022
Jan 5, 2022
Jun 22, 2021
Jul 2, 2021
Jul 2, 2021
Jun 8, 2021
Jun 8, 2021
Mar 14, 2021
Jan 5, 2022
Jun 8, 2021
Jan 6, 2022
Jan 5, 2022
Jun 8, 2021
Jun 8, 2021
Jun 22, 2021
Mar 31, 2021
Jul 2, 2021

Repository files navigation

typescript-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

This repo is a fork of ts-case-convert, which no longer seems to be maintained. All credit for the work should go to its author @RossWilliams.

typescript-case-convert converts object keys between camelCase, snake_case, and PascalCase 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'

Run tests

yarn run test

Documentation

See tests.

About

Convert between camelCase, snake_case, and PascalCase while preserving type information

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • TypeScript 94.5%
  • JavaScript 5.5%