-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
69 lines (63 loc) · 2.06 KB
/
utils.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
let expressionIdCounter = 0;
let animalIdCounter = 0;
const getElementById = (id, elementList) => {
return elementList.find((element) => {
return element.id === Number(id);
});
};
const getIndexById = (id, elementList) => {
return elementList.findIndex((element) => {
return element.id === Number(id);
});
};
const createElement = (elementType, queryArguments) => {
if (queryArguments.hasOwnProperty('emoji') &&
queryArguments.hasOwnProperty('name')) {
let currentId;
if (elementType === 'expressions') {
expressionIdCounter += 1;
currentId = expressionIdCounter;
} else {
animalIdCounter += 1;
currentId = animalIdCounter;
}
return {
'id': currentId,
'emoji': queryArguments.emoji,
'name': queryArguments.name,
};
} else {
return false;
}
};
const updateElement = (id, queryArguments, elementList) => {
const elementIndex = getIndexById(id, elementList);
if (elementIndex === -1) {
throw new Error('updateElement must be called with a valid id parameter');
}
if (queryArguments.id) {
queryArguments.id = Number(queryArguments.id);
}
Object.assign(elementList[elementIndex], queryArguments);
return elementList[elementIndex];
};
const seedElements = (arr, type) => {
if (type === 'expressions') {
arr.push(createElement('expressions', {'emoji': '😀', 'name': 'happy'}));
arr.push(createElement('expressions', {'emoji': '😎', 'name': 'shades'}));
arr.push(createElement('expressions', {'emoji': '😴', 'name': 'sleepy'}));
} else if (type === 'animals') {
arr.push(createElement('animals', {'emoji': '🐶', 'name': 'Pupper'}));
arr.push(createElement('animals', {'emoji': '🐍', 'name': 'Snek'}));
arr.push(createElement('animals', {'emoji': '🐱', 'name': 'Maru'}));
} else {
throw new Error(`seed type must be either 'expression' or 'animal'`);
}
};
module.exports = {
createElement: createElement,
getIndexById: getIndexById,
getElementById: getElementById,
updateElement: updateElement,
seedElements: seedElements,
};