https://github.com/4lessandrodev/object_keys_to_camelcase
Util function to convert object keys from snake_case to camelCase
https://github.com/4lessandrodev/object_keys_to_camelcase
camel-case snakecase-keys-object typescript typescript-library utility-library
Last synced: 3 months ago
JSON representation
Util function to convert object keys from snake_case to camelCase
- Host: GitHub
- URL: https://github.com/4lessandrodev/object_keys_to_camelcase
- Owner: 4lessandrodev
- Created: 2021-07-10T23:14:34.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-31T16:54:27.000Z (over 3 years ago)
- Last Synced: 2025-02-07T15:16:36.756Z (3 months ago)
- Topics: camel-case, snakecase-keys-object, typescript, typescript-library, utility-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/keys-converter
- Size: 1.52 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Snake Case to Camel Case
A lib to convert object keys to camel-case or snake-case.
```sh
npm i keys-converter# or
yarn add keys-converter
```
Use on browser
```html
```
```html
import { objectKeysToCamelCaseV2, objectKeysToSnakeCaseV2 } from 'https://cdn.skypack.dev/keys-converter';```
## Imports
```ts
import { objectKeysToSnakeCaseV2, objectKeysToCamelCaseV2 } from 'keys-converter';
```
---
### How to use it?
`objectKeysToCamelCaseV2` and `objectKeysToSnakeCaseV2`
### Two available functions
- convert object keys from `snake_case` to `camelCase`
```ts
import { objectKeysToCamelCaseV2 } from 'keys-converter';
const user = {
_id: "sf2309sdf0010",
company_name: "some name",
user_password: "some@pass123",
created_at: "2020-01-01",
updated_at: "2020-01-01"
};const result = objectKeysToCamelCaseV2(user);
console.log(result);
> `{
_id: "sf2309sdf0010",
companyName: "some name",
userPassword: "some@pass123",
createdAt: "2020-01-01",
updatedAt: "2020-01-01"
}````
- convert object keys from `camelCase` to `snake_case`
```ts
import { objectKeysToSnakeCaseV2} from 'keys-converter';
const user = {
_id: "sf2309sdf0010",
companyName: "some name",
userPassword: "some@pass123",
createdAt: "2020-01-01",
updatedAt: "2020-01-01"
};const result = objectKeysToSnakeCaseV2(user);
console.log(result);
> `{
_id: "sf2309sdf0010",
company_name: "some name",
user_password: "some@pass123",
created_at: "2020-01-01",
updated_at: "2020-01-01"
}````
The function `objectKeysToCamelCaseV2` receives an object.
You can to infer the return type as argument so the result returned will have types#### Inference `result` and `input`
```ts
const user = {
_id: "sf2309sdf0010",
companyName: "some name",
userPassword: "some@pass123",
createdAt: "2020-01-01",
updatedAt: "2020-01-01"
};/** Dynamic Type */
interface ResultType {
_id: string;
company_name: string,
user_password: string,
created_at: "2020-01-01",
updated_at: "2020-01-01"
}const result = objectKeysToSnakeCaseV2(user);
```
If you provide the input type the function will validate the arguments,
So if you provide the result type the returned value will have "types"
---
## Warning
> - Function does not remove the first underscore for security
example
If your object has a protected prop like `_id` It will keep it