Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ts-case-convert

RossWilliams510.6kApache-2.02.1.0TypeScript support: included

Typescript type-preserving conversion of objects between camelCase and snake_case

PascalCase, camel-case, camelCase, conversion, pascal-case, snake-case, snake_case, typescript

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 converts object keys between camelCase and 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'

Run tests

yarn run test

Documentation

See tests.

📝 License

Copyright © 2021 Ross Williams.
This project is Apache--2.0 licensed.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.1.2 (2021-03-31)

1.1.1 (2021-03-31)

Bug Fixes

  • better support optional nested types (ea7e171)

1.1.0 (2021-03-24)

Features

  • export ObjectToCamel and ObjectToSnake types (c48f0f3)

1.0.5 (2021-03-14)

1.0.4 (2021-03-14)

1.0.3 (2021-03-14)

1.0.2 (2021-03-14)

1.0.1 (2021-03-14)

0.1.1 (2021-03-14)