-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigkahuna.js
159 lines (133 loc) · 4.8 KB
/
bigkahuna.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
// TODO: integrate dotenv?
class BigKahuna {
constructor(args) {
this.weight = this.length = this.size = 0;
this.args = {};
this.nodashArgs = {};
this.programCall = process.argv.slice(0, 2);
args = args || process.argv.slice(2);
this.processArgs(args);
this.careForDashes = false;
// We don't want to inf-loop when we make Strict Kahunas
if (this.constructor.name === 'BigKahuna') this.must = new StrictKahuna(args, this);
}
processArgs(args) {
this.args._ = (this.args._ || []).concat(args);
this.nodashArgs._ = (this.nodashArgs._ || []).concat(args.map(a => a.replace(/^-+/gm, '')));
for (let i = 0; i < args.length; i++) {
let a = args[i];
let p = null;
if (a.startsWith('-') && i + 1 < args.length) {
p = args[i + 1];
if (p.startsWith('-')) p = null;
else i++;
}
this.args[a] = p;
this.nodashArgs[a.replace(/^-+/gm, '')] = p;
}
this.weight = this.length = this.size = args.length;
}
dashCare(care) {
if (typeof care === 'undefined') care = !this.careForDashes;
this.careForDashes = care;
return this;
}
arg(item) {
return this.cabinet(item);
}
cabinet(item) {
let args = this.nodashArgs._;
if (this.careForDashes) args = this.args._;
if (typeof item === 'undefined') return args;
if (item >= args.length || item < 0) return undefined;
else return args[item];
}
folders() {
if (this.careForDashes) return this.args;
return this.nodashArgs;
}
has(...argsIn) {
argsIn = argsIn || [];
if (!argsIn.length) return false;
if (!this.careForDashes) argsIn = argsIn.map(a => a.replace(/^-+/gm, ''));
let cabinet = this.cabinet();
for (const a of argsIn) {
if (cabinet.includes(a)) return true;
}
return false;
}
answer(...argsIn) {
argsIn = argsIn || [];
if (!argsIn.length) return false;
if (!this.careForDashes) argsIn = argsIn.map(a => a.replace(/^-+/gm, ''));
let folders = this.folders();
for (const a of argsIn) {
if (folders.hasOwnProperty(a)) return folders[a];
}
return undefined;
}
answerAmount(amount, ...argsIn) {
argsIn = argsIn || [];
if (!argsIn.length) return false;
if (!this.careForDashes) argsIn = argsIn.map(a => a.replace(/^-+/gm, ''));
let cabinet = this.cabinet();
for (let c = 0; c < cabinet.length; c++) {
if (argsIn.includes(cabinet[c])) {
c++; // increment so we are looking at the first arg
if (typeof amount === "number") {
return cabinet.slice(c, c + amount);
} else if (Array.isArray(amount)) {
let collect = cabinet.slice(c, c + amount[1]);
for (let q = amount[0]; q < collect.length; q++) {
if (collect[q].startsWith("-")) return collect.slice(0, q);
}
return collect;
}
}
}
return undefined;
}
}
class StrictKahuna extends BigKahuna {
arg(item) {
return this.cabinet(item);
}
cabinet(item) {
let c = super.cabinet(item);
if (typeof c === 'undefined') throw new MissingRequiredArgumentException(item);
return c;
}
has(...argsIn) {
let doesHave = super.has.apply(this, [...argsIn]);
if (!doesHave) throw new MissingRequiredArgumentException([...argsIn]);
return doesHave;
}
answer(...argsIn) {
let a = super.answer.apply(this, argsIn);
if (typeof a === 'undefined') throw new MissingRequiredArgumentException([...argsIn]);
return a;
}
answerAmount(amount, ...argsIn) {
let a = super.answerAmount.apply(this, [amount, ...argsIn]);
if (typeof a === 'undefined') throw new MissingRequiredArgumentException([...argsIn]);
return a;
}
}
class MissingRequiredArgumentException extends Error {
constructor(args) {
super();
this.name = "MissingRequiredArgumentException";
this.message = this.genMessage(args);
}
genMessage(args) {
let randLol = [
"The Kahuna needed that paperwork yesterday!",
"This'll be chonked from your pay!",
"The whole world is stopping because of this!",
"Your lunch break already finished, so you can't slack off now!"
];
randLol = randLol[Math.floor(Math.random() * randLol.length)];
return `Required argument [${args}] was not provided! ` + randLol;
}
}
module.exports = new BigKahuna();