-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
73 lines (65 loc) · 2.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// <reference path="./typedefs.js" />
/**
* Interprets its input as a mutation to be rearranged
* into 3 labels/properties it can return: $add, $update, $remove
* Open the test for examples
* @param {Doc} doc - The input document
* @param {Mutation} mutation - The mutation to execute
* @returns {Object}
*/
function generateUpdateStatement(doc, mutation) {
const opsNames = ["$remove", "$update", "$add"];
const props = ["text", "value"];
const iterable = mutation.posts ? "posts" : "mentions";
let ops = {};
mutation[iterable].forEach((change) => {
// $add handling
if (!change._id) {
ops.$add = ops.$add || {};
ops.$add[iterable] = ops.$add[iterable] || [];
Object.keys(change).forEach((prop) => {
if (!props.includes(prop)) {
throw new Error(`Unsupported property found: ${prop}`);
}
});
ops.$add[iterable].push(change);
return;
}
// $remove handling
let elementIndex = doc[iterable].findIndex((e) => e._id === change._id);
if (change._delete) {
ops.$remove = ops.$remove || {};
ops.$remove[`${iterable}.${elementIndex}`] = true;
return;
}
// $update handling
for (let index = 0; index < props.length; index++) {
const prop = props[index];
if (change[prop]) {
ops.$update = ops.$update || {};
ops.$update[`${iterable}.${elementIndex}.${prop}`] = change[prop];
return;
}
}
// 'mentions' property handling (using recursion)
if (change.mentions) {
let mention = generateUpdateStatement(
doc[iterable][elementIndex],
change
);
opsNames.forEach((opName) => {
if (mention[opName]) {
for (const [key, value] of Object.entries(mention[opName])) {
ops[opName] = ops[opName] || {};
ops[opName][`posts.${elementIndex}.${key}`] = value;
}
}
});
return;
}
// If it reaches here something went wrong!
throw new Error("Unsupported update mutation: " + JSON.stringify(change));
});
return ops;
}
module.exports = { generateUpdateStatement };