-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.js
221 lines (146 loc) Β· 4.37 KB
/
helpers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Helpers for Async Handlers
*/
const helpers = {};
helpers.isRecipeOutputValid = (output, targetSchema = 'aiAssist') => {
let schemaOptions = {
aiAssist: [
{
prop: "ingredients",
type: Object
},
{
prop: "diet",
type: String
},
{
prop: "allergies",
type: Array
},
{
prop: "intro",
type: String
},
{
prop: "desc",
type: String
},
{
prop: "health_score",
type: Number
},
{
prop: "health_reason",
type: String
},
{
prop: "prompt",
type: String
}
],
userUpdate: [
{
prop: "ingredients",
type: Object
},
{
prop: "diet",
type: String
},
{
prop: "intro",
type: String
},
{
prop: "desc",
type: String
},
{
prop: "name",
type: String
},
{
prop: "cooking_time",
type: Number
},
{
prop: "steps",
type: Array
}
],
insights: [
{
prop: "allergies",
type: Array
},
{
prop: "health_score",
type: Number
},
{
prop: "health_reason",
type: String
}
]
}
try {
let schema = schemaOptions[targetSchema];
for (let schemaItem of schema) {
if (!Object.hasOwn(output, schemaItem.prop)) return false;
else if (!typeof(output[schemaItem.prop]) === schemaItem.type) return false;
else return true;
}
} catch (error) {
console.log("[at helpers.isRecipeOutputValid]: " + error);
return false;
}
}
helpers.validateIngredients = async(ingredients) => {
return new Promise (resolve => {
// Validation
const schema = ['name', 'steps', 'category'];
if (!typeof(ingredients) === Array) {
resolve({valid: false});
return;
}
for (let property of schema) {
if (!Object.hasOwn(ingredients[0], property)) {
resolve({valid: false});
return;
}
}
if (!typeof(ingredients.steps) === Array) {
resolve({valid: false});
return;
}
resolve({valid: true});
})
}
helpers.sanitiseIngredients = async(ingredients, steps) => {
return new Promise (resolve => {
// Sanitisation
let unusedIngredients = [];
for (let i = 0; i < ingredients.length; i++) {
let ingredient = ingredients[i];
let isUsed = false;
for (let step of ingredient.steps) {
if (steps[step-1].toLowerCase().includes(ingredient.name.toLowerCase())) {
isUsed = true;
break;
}
}
if (!isUsed) unusedIngredients.push(i);
}
unusedIngredients.sort((val1, val2) => val2 - val1 );
for (let index of unusedIngredients) ingredients.splice(index, 1);
resolve({valid: true, list: ingredients});
})
}
helpers.getRecipeDietType = (ingredients) => {
const meat = ingredients.filter((ingredient) => ingredient.category.toLowerCase() === 'meat');
const dairy = ingredients.filter((ingredient) => ingredient.category.toLowerCase() === 'dairy');
if (meat.length == 0 && dairy.length == 0) return "Vegan";
if (meat.length == 0) return "Vegetarian";
return "Non-Vegetarian";
}
export default helpers;