Skip to content

Commit e732632

Browse files
authored
Merge pull request #43 from hapinessjs/enhance-prepare-object
prepareUpdateObject should be recursive
2 parents 2ce18be + 5b35310 commit e732632

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hapiness/mongo",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Hapiness Module for MongoDB usage",
55
"main": "commonjs/index.js",
66
"types": "index.d.ts",

src/module/services/mongo-utils.service.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,13 @@ export class MongoUtils {
1010
}
1111
}
1212

13-
public static prepareUpdateObject(dto: any): any {
14-
if (!dto || !Object.keys(dto).length) {
15-
return {};
16-
}
17-
18-
const preparedObject: any = {};
19-
20-
Object
21-
.keys(dto)
22-
.forEach(rootKey => {
23-
if (!!dto[rootKey] && typeof dto[rootKey] === 'object') {
24-
Object
25-
.keys(dto[rootKey])
26-
.forEach(
27-
childKey => {
28-
preparedObject[`${rootKey}.${childKey}`] = dto[rootKey][childKey];
29-
}
30-
);
31-
} else {
32-
preparedObject[rootKey] = dto[rootKey];
33-
}
34-
});
35-
36-
return preparedObject;
13+
public static prepareUpdateObject(dto: any, prefix?: string): any {
14+
return Object.entries(dto || {}).reduce((acc, [key, value]) => {
15+
const _key = [prefix, key].filter(item => item).join('.');
16+
return !Array.isArray(value) && value && typeof value === 'object' && Object.keys(value).length ?
17+
Object.assign(MongoUtils.prepareUpdateObject(value, _key), acc) :
18+
Object.assign({ [_key]: value }, acc);
19+
}, {});
3720
}
3821

3922
static filterFindCondition(condition: any): any {

test/unit/mongo-utils.service.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,54 @@ export class MongoUtilTest {
129129
});
130130
}
131131

132+
/**
133+
* `MongoUtils` function `prepareUpdateObject` should return an unfold object (3)
134+
*/
135+
@test('- `MongoUtils` function `prepareUpdateObject` should return an unfold object (3)')
136+
testMongoUtilPrepareUpdateObjecUnfoldObject3() {
137+
const dateObject = new Date();
138+
unit
139+
.object(
140+
MongoUtils
141+
.prepareUpdateObject(
142+
{
143+
test1: 'value',
144+
test2: {
145+
key1: 'value1'
146+
},
147+
test3: {
148+
key1: {
149+
key2: {
150+
key3: {
151+
key4: {
152+
key5: 'value2'
153+
}
154+
},
155+
key6: 'value3'
156+
}
157+
},
158+
key7: {
159+
key8: 'value4'
160+
},
161+
key8: 'value5',
162+
key9: dateObject,
163+
key10: [1, 2, 3, 4, 5, 6]
164+
}
165+
}
166+
)
167+
)
168+
.is({
169+
test1: 'value',
170+
'test2.key1': 'value1',
171+
'test3.key1.key2.key3.key4.key5': 'value2',
172+
'test3.key1.key2.key6': 'value3',
173+
'test3.key7.key8': 'value4',
174+
'test3.key8': 'value5',
175+
'test3.key9': dateObject,
176+
'test3.key10': [1, 2, 3, 4, 5, 6]
177+
});
178+
}
179+
132180
/**
133181
* `MongoUtils` function `filterFindCondition` without id or _id should return the object
134182
*/

0 commit comments

Comments
 (0)