Skip to content

Commit 3b1b17b

Browse files
Merge pull request #1 from idapgroup/feature/keys-to-snake-case
add keys to snake case fn
2 parents 87eecaf + 9cf8a51 commit 3b1b17b

8 files changed

+125
-10
lines changed

README.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { isObject } from '@idapgroup/js-object-utils';
2424

2525
const object = {
2626
/**
27-
* key-value mapping
27+
* key: value
2828
* values can be primitives or objects
2929
*/
3030
};
@@ -81,7 +81,7 @@ import { createFormData } from '@idapgroup/js-object-utils';
8181

8282
const object = {
8383
/**
84-
* key-value mapping
84+
* key: value
8585
* values can be primitives or objects
8686
*/
8787
};
@@ -131,7 +131,7 @@ import { createURLParams } from '@idapgroup/js-object-utils';
131131

132132
const object = {
133133
/**
134-
* key-value mapping
134+
* key: value
135135
* values can be primitives or objects
136136
*/
137137
};
@@ -168,7 +168,7 @@ import { cleanObject } from '@idapgroup/js-object-utils';
168168

169169
const object = {
170170
/**
171-
* key-value mapping
171+
* key: value
172172
* values can be primitives or objects
173173
*/
174174
};
@@ -178,3 +178,22 @@ const obj = cleanObject(
178178
options, // optional
179179
);
180180
```
181+
182+
### keysToSnakeCase
183+
184+
Transform object keys to snake case
185+
186+
```js
187+
import { cleanObject } from '@idapgroup/keysToSnakeCase';
188+
189+
const object = {
190+
/**
191+
* key: value
192+
* values can be primitives or objects
193+
*/
194+
};
195+
196+
const obj = keysToSnakeCase(
197+
object
198+
);
199+
```

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "@idapgroup/js-object-utils",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Javascript functions for transform or mutate object",
55
"main": "build/main/index.js",
66
"module": "build/module/index.js",
77
"typings": "build/main/index.d.ts",
88
"repository": {
99
"type": "git",
10-
"url": "https://github.com/idapgroup/js-object-utils.git"
10+
"url": "git+https://github.com/idapgroup/js-object-utils.git"
1111
},
1212
"license": "MIT",
1313
"scripts": {

register.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const tsNode = require('ts-node');
2-
const testTSConfig = require('./test/tsconfig.spec.json');
32

43
tsNode.register({
54
files: true,

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { createURLParams } from './lib/create-url-params';
22
export { createFormData } from './lib/create-form-data';
33
export { cleanObject } from './lib/clean-object';
44
export { isObject } from './lib/is-object';
5+
export { keysToSnakeCase } from './lib/keys-to-snake-case';
56
export * from './types/create-url-params';
67
export * from './types/create-form-data';
78
export * from './types/clean-object';

src/lib/keys-to-snake-case.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const keysToSnakeCase = <T extends Record<string, any>>(
2+
obj: T
3+
): Record<string, any> => {
4+
if (obj === null || obj === undefined) {
5+
return obj;
6+
}
7+
if (Array.isArray(obj)) {
8+
return obj.map((v) => keysToSnakeCase(v));
9+
}
10+
if (typeof obj === 'object') {
11+
return Object.entries(obj).reduce((acc, [k, v]) => {
12+
return Object.assign(Object.assign({}, acc), {
13+
[k.replace(/([A-Z])/g, '_$1').toLowerCase()]: keysToSnakeCase(v),
14+
});
15+
}, {});
16+
}
17+
return obj;
18+
};

test/keys-to-snake-case.spec.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {describe, it} from "mocha";
2+
import {expect, use} from "chai";
3+
import {keysToSnakeCase} from "../src";
4+
import deepEqualInAnyOrder from 'deep-equal-in-any-order';
5+
6+
use(deepEqualInAnyOrder);
7+
describe('keysToSnakeCase function', () => {
8+
it('should be object with snake keys', () => {
9+
const obj = {
10+
name: 'test',
11+
address: { 'street': '123 Street' },
12+
additionalEmails: ['[email protected]', '[email protected]', null],
13+
phones: [
14+
{
15+
countryCode: '123',
16+
phoneNumber: '2345435'
17+
},
18+
{
19+
countryCode: '545',
20+
phoneNumber: '55555',
21+
isConnectable: 1,
22+
hasSocial: true,
23+
socials: ['viber', 'telegram']
24+
}
25+
],
26+
products: [],
27+
hasSubscription: false,
28+
test: [true],
29+
};
30+
expect(keysToSnakeCase(obj)).to.deep.equal({
31+
name: 'test',
32+
address: { 'street': '123 Street' },
33+
additional_emails: ['[email protected]', '[email protected]', null],
34+
phones: [
35+
{
36+
country_code: '123',
37+
phone_number: '2345435'
38+
},
39+
{
40+
country_code: '545',
41+
phone_number: '55555',
42+
is_connectable: 1,
43+
has_social: true,
44+
socials: ['viber', 'telegram']
45+
}
46+
],
47+
products: [],
48+
has_subscription: false,
49+
test: [true],
50+
})
51+
});
52+
it('should be object array with snake keys', () => {
53+
const arr = [
54+
{
55+
countryCode: '123',
56+
phoneNumber: '2345435'
57+
},
58+
{
59+
code: '545',
60+
phoneNumber: '55555',
61+
isConnectable: 1,
62+
socials: ['viber', 'telegram']
63+
}
64+
];
65+
expect(keysToSnakeCase(arr)).to.deep.equal([
66+
{
67+
country_code: '123',
68+
phone_number: '2345435'
69+
},
70+
{
71+
code: '545',
72+
phone_number: '55555',
73+
is_connectable: 1,
74+
socials: ['viber', 'telegram']
75+
}
76+
])
77+
})
78+
})

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"incremental": true,
4-
"target": "ES2015",
4+
"target": "ES2017",
55
"outDir": "build/main",
66
"rootDir": "src",
77
"moduleResolution": "node",
@@ -19,7 +19,7 @@
1919
"listEmittedFiles": false ,
2020
"listFiles": false,
2121
"pretty": true,
22-
"lib": ["ES2015", "dom"],
22+
"lib": ["ES2017", "dom"],
2323
"types": [],
2424
"typeRoots": ["node_modules/@types", "src/types"]
2525
},

tsconfig.module.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"baseUrl": "./",
55
"target": "ESNext",
66
"module": "ESNext",
7-
"lib": ["ES2015", "dom"],
7+
"lib": ["ES2017", "dom"],
88
"outDir": "./build/module",
99
}
1010
}

0 commit comments

Comments
 (0)