-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
504 lines (464 loc) · 16.5 KB
/
index.js
File metadata and controls
504 lines (464 loc) · 16.5 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
require("none")()
/* eslint-disable perfectionist/sort-objects, max-lines */
const GetIntrinsic = require("es-intrinsic-cache")
const SimpleCache = require("simple-lru-cache")
const Fruit = require("jsfruit")
const Vegetable = require("libvegetable")
const Person = require("libperson")
const Guacamole = require("libguacamole")
const isEqual = require("@10xly/strict-equals")
const isdash = require("is-")
const noop = require("noop-enterprise")
const noop10 = require("noop10")
const bail = require("bail")
const construct = require("construct-new")
const toStr = require("@rightpad/convert2string")
const attempt = require("attempt-statement")
// eslint-disable-next-line no-inline-comments
const asArray = (value) => [value], // Put it behind bars
trueValue = require("true-value")
const isString = require("@is-(unknown)/is-string")
const repeating = require("repeating")
// eslint-disable-next-line no-inline-comments
const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node, and the only 10x one.
const concat = require("@rightpad/concat")
const NEWLINE = require("fizzbuzz-enterprise/source/main/constants/strings/delimiters/Newline")
const emptyString = require("empty-string")
const falseValue = require("false-value"),
sleep = require("@redux-saga/delay-p").default
const uncurry = require("call-bind-enterprise")
const join = require("array.prototype.join")
const at = require("string.prototype.at")
const replaceAll = require("str-replaceallof-es")
const split = require("string.prototype.split")
const length = require("utf8-byte-length")
const bind = uncurry(require("function-bind")),
call = require("node-call.then")
const just = require("basic-functions"),
// eslint-disable-next-line sort-vars
Null = require("qc-core").nullFn
const entries = require("object.entries-ponyfill")
const arrayGetMember = uncurry(require("array-get-member").arrayGetMember),
stubArray = require("lodash.stubarray")
require("get-member")()
// eslint-disable-next-line one-var
const objectGetMember = uncurry(
require("object.prototype-intrinsic-ai").getMember,
),
// eslint-disable-next-line sort-vars
ParseFloat = require("numero").parseFloat
const unicodePo = require("unicode/category/Po")
const subtract = require("subtract")
const forEach = require("for-each")
const head = require("@extra-array/head")
const last = require("@extra-array/last"),
unicodePoArray = objectGetMember(
just,
"call",
)(() => {
// THIS IS NOT SHADOWING!!!
// eslint-disable-next-line no-shadow
const unicodePoArray = stubArray()
forEach(entries(unicodePo), (entry) => {
const key = head(entry),
// eslint-disable-next-line new-cap
numberKey = ParseFloat(key),
value = last(entry)
unicodePoArray[numberKey] = value
})
return unicodePoArray
})
const coalesce = require("es-logical-nullish-coalescing-operator")
const isFalse = require("@is-(unknown)/is-false")
const isPositiveZero = require("positive-zero")
const isNegativeZero = require("@is-(unknown)/is-negative-zero")
const isNullOrUndefined = require("@is-(unknown)/is-nil")
const isNull = require("@is-(unknown)/is-null")
const isUndefined = require("@is-(unknown)/is-undefined")
// eslint-disable-next-line sonarjs/no-globals-shadowing
const isNaN = require("@is-(unknown)/is-nan")
const use = require("use-unused-vars")
const assign = require("@frosted/object.assign")
const global = require("@10xly/global")
const getFnName = require("name-of-function")
const { TernaryCompare } = require("important-extremely-useful-classes")
const multiply = require("lolite.__private.multiplyfallback")
const nullvalue = require("primitive-value-null")
const nanvalue = require("primitive-value-nan")
const undef = require("primitive-value-undefined")
const $BigInt = require("bigint-intrinsic-ai")
const negativeZero = require("@negative-numbers/zero")
const zero = require("@positive-numbers/zero"),
zeroBigint = $BigInt(zero)
const one = require("@positive-numbers/one")
const two = require("@positive-numbers/two")
const three = require("@positive-numbers/three")
const four = require("@positive-numbers/four")
const five = require("@positive-numbers/five")
const six = require("@positive-numbers/six")
const seven = require("@positive-numbers/seven")
const eight = require("@positive-numbers/eight")
const nine = require("@positive-numbers/nine")
const ten = require("@positive-numbers/ten")
const eleven = require("@positive-numbers/eleven")
const seventeen = require("@positive-numbers/seventeen")
const twentyFive = require("@positive-numbers/twenty-five")
const twentyNine = require("@positive-numbers/twenty-nine")
const thirtyThree = require("@positive-numbers/thirty-three")
const oneHundred = require("fizzbuzz-enterprise/source/main/constants/magic-numbers/Hundred")
const negative87 = require("@negative-numbers/eighty-seven")
// eslint-disable-next-line id-length
const E = require("@uppercase-letters/e")
// eslint-disable-next-line id-length
const O = require("@uppercase-letters/o")
const EXCLAMATION_POINT = arrayGetMember(unicodePoArray, thirtyThree).symbol,
// eslint-disable-next-line id-length
R = require("@uppercase-letters/r")
const $BaseError = require("es-error-intrinsics/Error")
const $EvalError = require("es-error-intrinsics/EvalError")
const $RangeError = require("es-error-intrinsics/RangeError")
const $ReferenceError = require("es-error-intrinsics/ReferenceError")
const $SyntaxError = require("es-error-intrinsics/SyntaxError")
const $TypeError = require("es-error-intrinsics/TypeError")
const $URIError = require("es-error-intrinsics/URIError")
// eslint-disable-next-line new-cap
const captureStackTrace = GetIntrinsic(
"%Error.captureStackTrace%",
trueValue(),
),
// eslint-disable-next-line camelcase
default_error = concat(E, R, R, O, R, EXCLAMATION_POINT),
// eslint-disable-next-line sort-vars
ErrorMap = construct({
target: SimpleCache,
// eslint-disable-next-line sort-keys
args: asArray({ maxSize: oneHundred }),
}),
// eslint-disable-next-line sort-vars
ErrorType = deepFreeze({
BaseError: zero,
EvalError: one,
RangeError: two,
ReferenceError: three,
SyntaxError: four,
TypeError: five,
URIError: six,
// eslint-disable-next-line sort-keys
FruitConsumptionError: seven,
VegetablesDoNotTalkError: eight,
// eslint-disable-next-line sort-keys
PersonNotHungryError: nine,
PortionsError: ten,
// eslint-disable-next-line sort-keys
FalseJSValidationFailedToPassError: eleven,
}),
// eslint-disable-next-line sort-vars
FalseJSValidationFailedToPassError = require("@falsejs/validation-failed-to-pass-error")
// eslint-disable-next-line no-underscore-dangle
function target_(value) {
return concat(
at(toStr(noop10), zero),
toStr(() => ({ target: value })),
at(toStr(noop10), one),
at(toStr(noop10), zero),
at(toStr(noop10), one),
)
}
// eslint-disable-next-line max-statements
function createObjectWithTargetKey(value) {
const name = construct({
target: TernaryCompare,
// eslint-disable-next-line sort-keys
args: [isString(value), `"${value}"`, getFnName(value)],
}).compare()
global[name] = value
let string = toStr(target_)
string = arrayGetMember(
// eslint-disable-next-line unicorn/prefer-string-slice
split(string, toStr(target_).substr(zero, twentyFive)),
one,
)
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(one)
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(one)
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(one)
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(zero, subtract(length(string), one))
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(zero, subtract(length(string), one))
// eslint-disable-next-line unicorn/prefer-string-slice
string = string.substr(zero, subtract(length(string), one))
string = replaceAll(
string,
// eslint-disable-next-line unicorn/prefer-string-slice
toStr(target_).substr(seventeen, five),
name,
)
// eslint-disable-next-line unicorn/prefer-string-slice, one-var
const array = split(string, toStr(target_).substr(twentyNine, six))
array.shift()
// eslint-disable-next-line unicorn/prefer-string-slice, no-eval, sonarjs/code-eval
return eval(eval(join(array, toStr(target_).substr(twentyNine, six))))
}
// eslint-disable-next-line init-declarations
let noFruitLeftMessage,
// eslint-disable-next-line init-declarations
personIsNotHungryAndCannotBeFedMessage,
// eslint-disable-next-line init-declarations
portionSizeExpectedToBeAPositiveIntegerMessage,
// eslint-disable-next-line init-declarations
vegetablesCanNotTalkMessage
objectGetMember(
just,
"call",
// eslint-disable-next-line max-lines-per-function, max-statements
)(() => {
ErrorMap.set(ErrorType.BaseError, $BaseError)
ErrorMap.set(ErrorType.EvalError, $EvalError)
ErrorMap.set(ErrorType.RangeError, $RangeError)
ErrorMap.set(ErrorType.ReferenceError, $ReferenceError)
ErrorMap.set(ErrorType.SyntaxError, $SyntaxError)
ErrorMap.set(ErrorType.TypeError, $TypeError)
ErrorMap.set(ErrorType.URIError, $URIError)
ErrorMap.set(
ErrorType.FruitConsumptionError,
objectGetMember(
just,
"call",
)(() => {
const fruit = construct(createObjectWithTargetKey(Fruit))
use(fruit)
// eslint-disable-next-line init-declarations
let result
attempt(() => {
// eslint-disable-next-line sonarjs/code-eval, no-eval
eval(repeating(concat("fruit.eat()", NEWLINE), eleven))
})
.rescue((error) => {
result = error.constructor
noFruitLeftMessage = error.message
})
.else(noop)
.ensure(noop)
.end()
return result
}),
)
ErrorMap.set(
ErrorType.VegetablesDoNotTalkError,
objectGetMember(
just,
"call",
)(() => {
const vegetable = construct(createObjectWithTargetKey(Vegetable))
// eslint-disable-next-line init-declarations
let result
attempt(() => {
vegetable.greet()
})
.rescue((error) => {
result = error.constructor
vegetablesCanNotTalkMessage = error.message
})
.else(noop)
.ensure(noop)
.end()
return result
}),
)
ErrorMap.set(
ErrorType.PersonNotHungryError,
objectGetMember(
just,
"call",
)(() => {
const person = construct(createObjectWithTargetKey(Person))
person.hungry = falseValue()
// eslint-disable-next-line init-declarations
let result
attempt(() => {
person.feed()
})
.rescue((error) => {
result = error.constructor
personIsNotHungryAndCannotBeFedMessage = replaceAll(
error.message,
"undefined",
"%",
)
})
.else(noop)
.ensure(noop)
.end()
return result
}),
)
ErrorMap.set(
ErrorType.PortionsError,
objectGetMember(
just,
"call",
)(() => {
// eslint-disable-next-line init-declarations
let result
attempt(() => {
construct({
target: Guacamole,
// eslint-disable-next-line sort-keys
args: [negative87],
})
})
.rescue((error) => {
result = error.constructor
portionSizeExpectedToBeAPositiveIntegerMessage = error.message
})
.else(noop)
.ensure(noop)
.end()
return result
}),
)
ErrorMap.set(
ErrorType.FalseJSValidationFailedToPassError,
FalseJSValidationFailedToPassError,
)
})
function CreateSleepFunction(delay) {
// eslint-disable-next-line new-cap
return bind(sleep, Null(), delay)
}
function CreateError(error, message) {
return construct(
assign(createObjectWithTargetKey(error), {
args: asArray(message),
}),
)
}
exports.immediateError = function immediateError(message, errorType) {
// eslint-disable-next-line no-param-reassign
message = coalesce(message, default_error)
// eslint-disable-next-line no-param-reassign
errorType = coalesce(errorType, ErrorType.BaseError)
// eslint-disable-next-line init-declarations
let error
attempt(() => {
// eslint-disable-next-line new-cap, unicorn/throw-new-error
error = CreateError(exports.getError(errorType), message)
})
.rescue(() => {
attempt(() => {
// eslint-disable-next-line new-cap, unicorn/throw-new-error
error = CreateError(errorType, message)
})
.rescue(() => {
// eslint-disable-next-line new-cap, unicorn/throw-new-error
error = CreateError($BaseError, message)
})
.else(noop)
.ensure(noop)
.end()
})
.else(noop)
.ensure(noop)
.end()
if (isdash.is(captureStackTrace)) {
captureStackTrace(error, immediateError)
}
exports.throwWhatever(error)
// How did we get here?
require("is-not-integer")()
}
exports.delayedError = function delayedError(message, errorType, delay) {
// eslint-disable-next-line no-param-reassign
message = coalesce(message, default_error)
// eslint-disable-next-line no-param-reassign
errorType = coalesce(errorType, ErrorType.BaseError)
return objectGetMember(call, "then")(
// eslint-disable-next-line new-cap
objectGetMember(just, "call")(CreateSleepFunction(delay)),
() => exports.immediateError(message, errorType),
)
}
exports.getError = function getError(errorType) {
return construct({
target: TernaryCompare,
// eslint-disable-next-line sort-keys
args: [ErrorMap.get(errorType), ErrorMap.get(errorType), errorType],
}).compare()
}
// eslint-disable-next-line sonarjs/cognitive-complexity, max-statements
exports.throwWhatever = function throwWhatever(whateverToThrow) {
if (whateverToThrow) {
bail(whateverToThrow)
} else {
// eslint-disable-next-line no-lonely-if
if (isFalse(whateverToThrow)) {
just.throw(falseValue())
} else if (isPositiveZero(whateverToThrow)) {
just.throw(zero)
} else if (isNegativeZero(whateverToThrow)) {
just.throw(negativeZero)
} else if (isEqual(whateverToThrow, zeroBigint)) {
just.throw(zeroBigint)
} else if (isEqual(whateverToThrow, emptyString)) {
just.throw(emptyString)
} else if (isNullOrUndefined(whateverToThrow)) {
if (isNull(whateverToThrow)) {
just.throw(nullvalue)
} else if (isUndefined(whateverToThrow)) {
just.throw(undef)
} else {
// eslint-disable-next-line new-cap, unicorn/throw-new-error
just.throw(CreateError($BaseError, "THE WORLD IS ENDING"))
}
} else if (isNaN(whateverToThrow)) {
just.throw(nanvalue)
} else {
just.throw(whateverToThrow)
}
}
}
// eslint-disable-next-line init-declarations, one-var
let tooManyPortionsMessage
attempt(() => {
const guacamole = construct({
target: Guacamole,
// eslint-disable-next-line sort-keys
args: [multiply(oneHundred, oneHundred)],
})
use(guacamole)
})
.rescue((error) => {
tooManyPortionsMessage = error.message
})
.else(noop)
.ensure(noop)
.end()
exports.MESSAGES = {
DOMAIN: {
FRUIT_CONSUMPTION_ERROR: {
NO_FRUIT_LEFT: noFruitLeftMessage,
},
VEGETABLES_DO_NOT_TALK_ERROR: {
VEGETABLES_CAN_NOT_TALK: vegetablesCanNotTalkMessage,
},
// eslint-disable-next-line sort-keys
PERSON_NOT_HUNGRY_ERROR: {
IS_NOT_HUNGRY_AND_CANNOT_BE_FED: personIsNotHungryAndCannotBeFedMessage,
},
PORTIONS_ERROR: {
PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER:
portionSizeExpectedToBeAPositiveIntegerMessage,
TOO_MANY_PORTIONS: tooManyPortionsMessage,
},
// eslint-disable-next-line sort-keys
FALSEJS_VALIDATION_FAILED_TO_PASS_ERROR: {
// eslint-disable-next-line no-inline-comments
VALIDATION_FAILED_TO_PASS: "Validation failed to pass", // Use the error message directly as we don't want to install FalseJS because it is really heavy.
},
},
}
exports.attempt = attempt
exports.ErrorType = ErrorType