core-services-sdk 1.3.55 → 1.3.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "core-services-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.56",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"google-libphonenumber": "^3.2.42",
|
|
35
35
|
"http-status": "^2.1.0",
|
|
36
36
|
"knex": "^3.1.0",
|
|
37
|
+
"lodash-es": "^4.17.21",
|
|
37
38
|
"mongodb": "^6.18.0",
|
|
38
39
|
"nodemailer": "^7.0.5",
|
|
39
40
|
"p-retry": "^7.0.0",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { mapKeys, snakeCase, camelCase } from 'lodash-es'
|
|
2
|
+
import { sanitizeObjectAllowProps } from './sanitize-objects.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts object keys from camelCase to snake_case.
|
|
6
|
+
*
|
|
7
|
+
* Optionally restricts the conversion to a whitelist of allowed properties.
|
|
8
|
+
* If no allowed properties are provided, all object keys are converted.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} obj - The source object with camelCase keys.
|
|
11
|
+
* @param {...string|string[]} allowedProps - Optional list (or arrays) of allowed property names.
|
|
12
|
+
* @returns {Object} A new object with keys converted to snake_case.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* toSnakeCase({ userId: '1', createdAt: 'now' })
|
|
16
|
+
* // { user_id: '1', created_at: 'now' }
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* toSnakeCase({ userId: '1', createdAt: 'now' }, ['userId'])
|
|
20
|
+
* // { user_id: '1' }
|
|
21
|
+
*/
|
|
22
|
+
export function toSnakeCase(obj, ...allowedProps) {
|
|
23
|
+
const allowedPropsFixed = allowedProps.flat()
|
|
24
|
+
const objFiltered = allowedPropsFixed.length
|
|
25
|
+
? sanitizeObjectAllowProps(obj, allowedPropsFixed)
|
|
26
|
+
: obj
|
|
27
|
+
|
|
28
|
+
return mapKeys(objFiltered, (_value, key) => {
|
|
29
|
+
return snakeCase(key)
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Converts object keys from snake_case to camelCase.
|
|
35
|
+
*
|
|
36
|
+
* Optionally restricts the conversion to a whitelist of allowed properties.
|
|
37
|
+
* If no allowed properties are provided, all object keys are converted.
|
|
38
|
+
*
|
|
39
|
+
* @param {Object} obj - The source object with snake_case keys.
|
|
40
|
+
* @param {...string|string[]} allowedProps - Optional list (or arrays) of allowed property names.
|
|
41
|
+
* @returns {Object} A new object with keys converted to camelCase.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* toCamelCase({ user_id: '1', created_at: 'now' })
|
|
45
|
+
* // { userId: '1', createdAt: 'now' }
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* toCamelCase({ user_id: '1', created_at: 'now' }, ['user_id'])
|
|
49
|
+
* // { userId: '1' }
|
|
50
|
+
*/
|
|
51
|
+
export function toCamelCase(obj, ...allowedProps) {
|
|
52
|
+
const allowedPropsFixed = allowedProps.flat()
|
|
53
|
+
const objFiltered = allowedPropsFixed.length
|
|
54
|
+
? sanitizeObjectAllowProps(obj, allowedPropsFixed)
|
|
55
|
+
: obj
|
|
56
|
+
|
|
57
|
+
return mapKeys(objFiltered, (_value, key) => {
|
|
58
|
+
return camelCase(key)
|
|
59
|
+
})
|
|
60
|
+
}
|
package/src/core/index.js
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { toSnakeCase, toCamelCase } from '../../src/core/case-mapper.js'
|
|
4
|
+
|
|
5
|
+
describe('toSnakeCase', () => {
|
|
6
|
+
it('converts all keys from camelCase to snake_case', () => {
|
|
7
|
+
const input = {
|
|
8
|
+
userId: '1',
|
|
9
|
+
createdAt: 'now',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const result = toSnakeCase(input)
|
|
13
|
+
|
|
14
|
+
expect(result).toEqual({
|
|
15
|
+
user_id: '1',
|
|
16
|
+
created_at: 'now',
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('converts only allowed properties when allowedProps are provided', () => {
|
|
21
|
+
const input = {
|
|
22
|
+
userId: '1',
|
|
23
|
+
createdAt: 'now',
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const result = toSnakeCase(input, ['userId'])
|
|
27
|
+
|
|
28
|
+
expect(result).toEqual({
|
|
29
|
+
user_id: '1',
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('supports allowedProps passed as multiple arguments', () => {
|
|
34
|
+
const input = {
|
|
35
|
+
userId: '1',
|
|
36
|
+
createdAt: 'now',
|
|
37
|
+
updatedAt: 'later',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const result = toSnakeCase(input, 'userId', 'updatedAt')
|
|
41
|
+
|
|
42
|
+
expect(result).toEqual({
|
|
43
|
+
user_id: '1',
|
|
44
|
+
updated_at: 'later',
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('returns an empty object when allowedProps do not match any keys', () => {
|
|
49
|
+
const input = {
|
|
50
|
+
userId: '1',
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const result = toSnakeCase(input, ['nonExistingKey'])
|
|
54
|
+
|
|
55
|
+
expect(result).toEqual({})
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('toCamelCase', () => {
|
|
60
|
+
it('converts all keys from snake_case to camelCase', () => {
|
|
61
|
+
const input = {
|
|
62
|
+
user_id: '1',
|
|
63
|
+
created_at: 'now',
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const result = toCamelCase(input)
|
|
67
|
+
|
|
68
|
+
expect(result).toEqual({
|
|
69
|
+
userId: '1',
|
|
70
|
+
createdAt: 'now',
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('converts only allowed properties when allowedProps are provided', () => {
|
|
75
|
+
const input = {
|
|
76
|
+
user_id: '1',
|
|
77
|
+
created_at: 'now',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const result = toCamelCase(input, ['user_id'])
|
|
81
|
+
|
|
82
|
+
expect(result).toEqual({
|
|
83
|
+
userId: '1',
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('supports allowedProps passed as multiple arguments', () => {
|
|
88
|
+
const input = {
|
|
89
|
+
user_id: '1',
|
|
90
|
+
created_at: 'now',
|
|
91
|
+
updated_at: 'later',
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const result = toCamelCase(input, 'user_id', 'updated_at')
|
|
95
|
+
|
|
96
|
+
expect(result).toEqual({
|
|
97
|
+
userId: '1',
|
|
98
|
+
updatedAt: 'later',
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('returns an empty object when allowedProps do not match any keys', () => {
|
|
103
|
+
const input = {
|
|
104
|
+
user_id: '1',
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const result = toCamelCase(input, ['non_existing_key'])
|
|
108
|
+
|
|
109
|
+
expect(result).toEqual({})
|
|
110
|
+
})
|
|
111
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts object keys from camelCase to snake_case.
|
|
3
|
+
*
|
|
4
|
+
* Optionally restricts the conversion to a whitelist of allowed properties.
|
|
5
|
+
* If no allowed properties are provided, all object keys are converted.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} obj - The source object with camelCase keys.
|
|
8
|
+
* @param {...string|string[]} allowedProps - Optional list (or arrays) of allowed property names.
|
|
9
|
+
* @returns {Object} A new object with keys converted to snake_case.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* toSnakeCase({ userId: '1', createdAt: 'now' })
|
|
13
|
+
* // { user_id: '1', created_at: 'now' }
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* toSnakeCase({ userId: '1', createdAt: 'now' }, ['userId'])
|
|
17
|
+
* // { user_id: '1' }
|
|
18
|
+
*/
|
|
19
|
+
export function toSnakeCase(
|
|
20
|
+
obj: any,
|
|
21
|
+
...allowedProps: (string | string[])[]
|
|
22
|
+
): any
|
|
23
|
+
/**
|
|
24
|
+
* Converts object keys from snake_case to camelCase.
|
|
25
|
+
*
|
|
26
|
+
* Optionally restricts the conversion to a whitelist of allowed properties.
|
|
27
|
+
* If no allowed properties are provided, all object keys are converted.
|
|
28
|
+
*
|
|
29
|
+
* @param {Object} obj - The source object with snake_case keys.
|
|
30
|
+
* @param {...string|string[]} allowedProps - Optional list (or arrays) of allowed property names.
|
|
31
|
+
* @returns {Object} A new object with keys converted to camelCase.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* toCamelCase({ user_id: '1', created_at: 'now' })
|
|
35
|
+
* // { userId: '1', createdAt: 'now' }
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* toCamelCase({ user_id: '1', created_at: 'now' }, ['user_id'])
|
|
39
|
+
* // { userId: '1' }
|
|
40
|
+
*/
|
|
41
|
+
export function toCamelCase(
|
|
42
|
+
obj: any,
|
|
43
|
+
...allowedProps: (string | string[])[]
|
|
44
|
+
): any
|