-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
88 lines (86 loc) · 2.71 KB
/
data.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let cakeData = {
candleFlameColor: {
inputDescription: "Your cake's candle flame color",
inputType: "color",
default: "#ffdd66",
},
candleColor: {
inputDescription: "Your cake's candle color",
default: "#ff0000",
inputType: "color",
allowRainbow: true,
},
cakeBaseColor: {
inputDescription: "Your cake's base color",
default: "#1b8d14",
inputType: "color",
},
cakeBaseCreamColor: {
inputDescription: "Your cake's base cream color",
default: "#ffffff",
inputType: "color",
},
cakeCreamColor: {
inputDescription: "Your cake's frosting color",
default: "#ffffff",
inputType: "color",
},
recipientAge: {
inputDescription: "Your cake recipient's age",
validChoices(n) {
n = Number(n);
return Number.isInteger(n) && n >= 0;
},
default: 18,
inputType: "number",
},
cakeTopping: {
validChoices: [ {
value: "flakes",
description: "Flakes",
}, {
value: "creamCherry",
description: "Creamed Cherries",
}, {
value: "berry",
description: "Raspberries and Blueberries"
}],
default: "flakes",
inputType: "choice",
inputDescription: "Your cake's topping",
},
};
function generateCake(cakeProperties) {
const processedCakeProperties = {};
for (let key in cakeProperties) {
if (!cakeData.hasOwnProperty(key)) {
continue;
}
const defaultValue = cakeData[key].default;
/* Default key if key does not exist in the cake properties. */
if (!cakeProperties.hasOwnProperty(key) || cakeProperties[key] === null || cakeProperties[key] === void 0) {
processedCakeProperties[key] = defaultValue;
continue;
}
/* Validate the cake if there is one. */
const validator = cakeData[key].validChoices;
const curChoice = cakeProperties[key];
if (Array.isArray(validator)) {
if (validator.find(item => item.value === curChoice)) {
processedCakeProperties[key] = curChoice;
} else {
processedCakeProperties[key] = defaultValue;
}
} else if (typeof validator === "function") {
if (validator(curChoice)) {
processedCakeProperties[key] = curChoice;
} else {
processedCakeProperties[key] = defaultValue;
}
} else {
processedCakeProperties[key] = curChoice;
}
}
return processedCakeProperties;
}
module.exports = {cakeData, generateCake};