-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
401 lines (286 loc) · 12.1 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Super Validator
const superValidator = require('./files/superValidator.js');
const validateTotal = require('./files/validateTotal.js');
const objType = require('./files/objType.js');
module.exports = function (obj, callback) {
return new Promise(function (resolve, reject) {
// Validate Obj
const objValidated = superValidator(obj);
// Validator
if (objValidated.confirmed) {
// Prepare Count
let items = {
error: false,
forceBreak: false,
count: 0,
total: null,
items: []
};
// Error Result
const error_result = function (err) {
// Send Error Reject
items.error = true;
reject(err);
// Complete
return;
};
// Prepare Result
const result = function (isExtra, extraIndex, item, forceBreak = false) {
// Prepare Edit
let item_to_edit = null;
// Not Extra
if (!isExtra) {
item_to_edit = items;
} else {
item_to_edit = extra.list[extraIndex]
}
// Force Break
const forceBreakResult = { isObject: (objType(forceBreak, 'object')) };
// Is Boolean
if (!forceBreakResult.isObject) {
if (!item_to_edit.forceBreak) { forceBreakResult.allowed = (typeof forceBreak === "boolean" && forceBreak); } else {
forceBreakResult.allowed = false;
}
forceBreakResult.dontSendResult = false;
forceBreakResult.forceResult = false;
}
// Object
else {
if (!item_to_edit.forceBreak) { forceBreakResult.allowed = (typeof forceBreak.break === "boolean" && forceBreak.break); } else {
forceBreakResult.allowed = false;
}
forceBreakResult.dontSendResult = (typeof forceBreak.dontSendResult === "boolean" && forceBreak.dontSendResult);
forceBreakResult.forceResult = (typeof forceBreak.forceResult === "boolean" && forceBreak.forceResult);
}
// No Error
if ((!item_to_edit.error && !item_to_edit.forceBreak) || forceBreakResult.forceResult) {
// Count
item_to_edit.count++;
// Normal
if (item !== null) {
// Add Item
item_to_edit.items.push(item);
}
// Set Force Break
if (!item_to_edit.forceBreak && forceBreakResult.allowed) {
item_to_edit.forceBreak = true;
item_to_edit.count = item_to_edit.total + 1;
}
// Complete
if (
// Can Send Results
!forceBreakResult.dontSendResult && (
// Count is Bigger
(item_to_edit.count >= item_to_edit.total) || (
// Type
typeof item_to_edit.type === "string" &&
(
// While
(item_to_edit.type === "while" && item_to_edit.count > 0)
)
)
)
) {
// Normal Result
if (!isExtra) {
if (!extra.enabled) { resolve(items); }
}
// Extra Result
else {
// Check Extra Exist
if (extra.list[extraIndex]) {
// Complete Check
extra.list[extraIndex].complete = true;
// Check List
let confirmation_checked = true;
// Detect Progress
for (const item in extra.list) {
if (!extra.list[item].complete) {
confirmation_checked = false;
break;
}
}
// Complete
if (confirmation_checked) {
// Add Extra Info
items.extra = extra.list;
// Resolve
resolve(items);
}
}
// Nope
else {
items.error = true;
reject(new Error('forAwait Extra Index not found.'));
}
}
}
}
// Return
return;
};
// Run For
const runFor = function (callback, isExtra = false, index = null, new_extra = null) {
// Prepare the Item
let the_item = null;
// Normal
if (!isExtra) {
the_item = obj;
} else {
the_item = new_extra;
}
// Run Script
const runFor_script = function (item = null) {
// No Error
if (!items.error && !items.forceBreak) {
// Try
try {
// Result Function
const result_data = function (forceBreak) { return result(isExtra, index, item, forceBreak); };
// Exist Item
if (item !== null) {
callback(item, result_data, error_result, extra.extra_function);
}
// Nope
else {
callback(result_data, error_result, extra.extra_function);
}
}
// Error
catch (err) {
items.error = true;
reject(err);
return false;
}
// Normal Return
return true;
}
// Error
else {
return false;
}
};
// Start the For
if (typeof the_item.data !== "number") {
// For Object
if (!the_item.type) {
// Start For
let for_used = false;
for (const item in the_item.data) {
for_used = true;
if (!runFor_script(item)) { break; }
}
// Not Used? Confirmed
if (!for_used) {
return result(isExtra, index, null);
}
}
// Type
else {
// Start a While
if (the_item.type === "while") {
// Prepare
const custom_do = function () {
// Validate
if (the_item.checker()) {
// Prepare Edit
let item_to_edit = null;
// Not Extra
if (!isExtra) {
item_to_edit = items;
} else {
item_to_edit = extra.list[index]
}
// Add Total
item_to_edit.total++;
// Callback and Continue
callback(function (forceBreak) { item_to_edit.count++; return result(isExtra, index, null, forceBreak); }, error_result, extra.extra_function);
return custom_do();
}
// Nope
else { return result(isExtra, index, null); }
};
// Start
custom_do();
}
// Nothing
else { items.error = true; reject(new Error('Invalid Function Type!')); }
}
}
// Number Type
else {
// Exist Number
if (the_item.data > 0) {
for (let item = 0; item < the_item.data; item++) {
if (!runFor_script(item)) { break; }
}
}
// Nope
else {
return result(isExtra, index, null);
}
}
return;
};
// Detect Object Module
items.total = validateTotal(obj.data);
// Type
if (objValidated.type) { items.type = objValidated.type; }
// Prepare Extra
const extra = {
// Enabled
enabled: false,
// Extra List
list: [],
// Functions
extra_function: function (new_extra) {
// Validate Obj
const objValidated = superValidator(new_extra);
// Validator
if (objValidated.confirmed) {
// Prepare Extra
extra.enabled = true;
extra.list.push({
complete: false,
forceBreak: false,
count: 0,
total: null,
items: []
});
// Index
const index = extra.list.length - 1;
// Get Total
extra.list[index].total = validateTotal(new_extra.data);
// Type
if (objValidated.type) { extra.list[index].type = objValidated.type; }
// Callback
return {
// Run Extra
run: function (callback) {
// Run For
if (!items.error && !items.forceBreak) { runFor(callback, true, index, new_extra); }
// Complete
return;
}
};
}
// Nope
else {
const err = new Error('Invalid Object Extra Type to start the Script.');
items.error = true;
reject(err);
return null;
}
}
};
// Run For
runFor(callback);
}
// Nope
else {
reject(new Error('Invalid Object Type to start the Script.'));
}
// Complete
return;
});
};