Skip to content

Commit ef976ce

Browse files
committed
fix merge
1 parent 1a2f306 commit ef976ce

1 file changed

Lines changed: 70 additions & 47 deletions

File tree

src/utils/merge.ts

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import cloneDeep from 'lodash.clonedeep';
33
import { patch } from '@n1ru4l/json-patch-plus';
44

55
type HawkEvent = {
6-
payload: {
7-
[key: string]: any
8-
}
6+
payload: {
7+
[key: string]: any
8+
}
99
}
1010

1111
type HawkEventRepetition = {
12-
payload: {
13-
[key: string]: any
14-
}
15-
delta: string;
12+
payload: {
13+
[key: string]: any
14+
}
15+
delta: string;
1616
}
1717

1818
/**
@@ -27,31 +27,46 @@ type HawkEventRepetition = {
2727
*/
2828
export function repetitionAssembler(originalEvent: Object, repetition: { [key: string]: any }): any {
2929
const customizer = (originalParam: any, repetitionParam: any): any => {
30-
if (repetitionParam === null) {
31-
return originalParam;
32-
}
33-
34-
35-
if (typeof repetitionParam === 'object' && typeof originalParam === 'object') {
36-
/**
37-
* If original event has null but repetition has some value, we need to return repetition value
38-
*/
39-
if (originalParam === null) {
40-
return repetitionParam;
41-
/**
42-
* Otherwise, we need to recursively merge original and repetition values
43-
*/
44-
} else {
45-
return repetitionAssembler(originalParam, repetitionParam);
30+
if (repetitionParam === null) {
31+
return originalParam;
4632
}
47-
}
48-
49-
return repetitionParam;
33+
34+
35+
if (typeof repetitionParam === 'object' && typeof originalParam === 'object') {
36+
/**
37+
* If original event has null but repetition has some value, we need to return repetition value
38+
*/
39+
if (originalParam === null) {
40+
return repetitionParam;
41+
/**
42+
* Otherwise, we need to recursively merge original and repetition values
43+
*/
44+
} else {
45+
return repetitionAssembler(originalParam, repetitionParam);
46+
}
47+
}
48+
49+
return repetitionParam;
5050
};
51-
51+
5252
return mergeWith(cloneDeep(originalEvent), cloneDeep(repetition), customizer);
53-
}
54-
53+
}
54+
55+
function parsePayloadField(payload: any, field: string) {
56+
if (payload && payload[field] && typeof payload[field] === 'string') {
57+
payload[field] = JSON.parse(payload[field]);
58+
}
59+
60+
return payload;
61+
}
62+
63+
function stringifyPayloadField(payload: any, field: string) {
64+
if (payload && payload[field]) {
65+
payload[field] = JSON.stringify(payload[field]);
66+
}
67+
68+
return payload;
69+
}
5570

5671
/**
5772
* Helps to merge original event and repetition due to delta format,
@@ -64,43 +79,51 @@ export function repetitionAssembler(originalEvent: Object, repetition: { [key: s
6479
*/
6580
export function composeFullRepetitionEvent(originalEvent: HawkEvent, repetition: HawkEventRepetition | undefined): HawkEvent {
6681

67-
console.log('originalEvent', originalEvent);
68-
console.log('repetition', repetition);
69-
7082
/**
7183
* Make a deep copy of the original event, because we need to avoid mutating the original event
7284
*/
7385
const event = cloneDeep(originalEvent);
74-
86+
7587
if (!repetition) {
76-
return event;
88+
return event;
7789
}
78-
90+
7991
/**
8092
* New delta format (repetition.delta is not null)
8193
*/
8294
if (repetition.delta) {
83-
event.payload = patch({
84-
left: event.payload,
85-
delta: JSON.parse(repetition.delta)
86-
});
87-
88-
return event;
95+
/**
96+
* Parse addons and context fields from string to object before patching
97+
*/
98+
event.payload = parsePayloadField(event.payload, 'addons');
99+
event.payload = parsePayloadField(event.payload, 'context');
100+
101+
event.payload = patch({
102+
left: event.payload,
103+
delta: JSON.parse(repetition.delta)
104+
});
105+
106+
/**
107+
* Stringify addons and context fields from object to string after patching
108+
*/
109+
event.payload = stringifyPayloadField(event.payload, 'addons');
110+
event.payload = stringifyPayloadField(event.payload, 'context');
111+
112+
return event;
89113
}
90-
114+
91115
/**
92116
* New delta format (repetition.payload is null) and repetition.delta is null (there is no delta between original and repetition)
93117
*/
94118
if (!repetition.payload) {
95-
return event;
119+
return event;
96120
}
97-
121+
98122
/**
99123
* Old delta format (repetition.payload is not null)
100124
* @todo remove after July 5 2025
101125
*/
102126
event.payload = repetitionAssembler(event.payload, repetition.payload);
103-
127+
104128
return event;
105-
}
106-
129+
}

0 commit comments

Comments
 (0)