-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
345 lines (322 loc) · 10.9 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
// ,---, ,-.
// .' .' `\ ,--/ /|
// ,---.' \ ,--, ,--. :/ | __ ,-.
// | | .`\ | ,'_ /| : : ' / ,' ,'/ /|
// : : | ' | .--. | | : ,---. | ' / ,---. ' | |' |
// | ' ' ; :,'_ /| : . | / \ ' | : / \ | | ,'
// ' | ; . || ' | | . . / / ' | | \ / / |' : /
// | | : | '| | ' | | | . ' / ' : |. \ . ' / || | '
// ' : | / ; : | : ; ; | ' ; :__ | | ' \ \' ; /|; : |
// | | '` ,/ ' : `--' \' | '.'|' : |--' ' | / || , ;
// ; : .' : , .-./| : :; |,' | : | ---'
// | ,.' `--`----' \ \ / '--' \ \ /
// '---' `----' `----'
// Built by: Fernando Martin Garcia Del Angel
// Built because I was bored
/**
* Libraries to use
*/
const StackTrace = require('stacktrace-js')
const clone = require('clone')
const faker = require('faker')
const ErrorParser = require('error-stack-parser')
const chalk = require('chalk')
/**
* Quack
*/
const QUACK = '🦆 {Quack}'
/**
* Quacks!
* @param {string} word Word or Group to be Quacked
* @param {boolean} debug Prints the stack or not
* @param {number} level Number of leves of stack to print
* @param {boolean} detailed Prints the complete stack trace
*/
function quack(word = '', debug = false, level = 2, detailed = false) {
console.log(QUACK+' > '+chalk.yellow(!word ? quack.caller.name : word))
if (debug) {
printStack(level, detailed)
}
}
/**
* Prints the stack into a simple, readable table
* @param {number} level level of depth to print on the stack
* @param {boolean} detailed prints the source or not
*/
function printStack(level, detailed) {
if (level) {
var stack = StackTrace.getSync().slice(0, level)
for (var i = 0; i < stack.length; i++) {
stack[i] = rename(stack[i], {
columnNumber: 'column',
lineNumber: 'line',
})
if (!detailed) {
delete stack[i].source
}
}
console.table(stack)
}
}
/**
* Verifies wether value is truthy or not
* @param {any} value Value tested for being truthy
*/
function check(value) {
let type = ''
console.log('This value is of type '+chalk.blue(typeof value))
console.group()
switch (typeof value) {
case 'number': case 'string' :
console.log('-> Its value is '+chalk.green(value))
break
case 'object':
if (isEmpty(value)) {
console.log('-> Its value is '+chalk.red('empty'))
} else {
if (value[0] == undefined) {
console.table(xray(value))
} else {
console.log('->'+chalk.yellow(' Array type '))
for(val in value){
console.group(chalk.magenta('Arr['+val+']'))
check(value[val])
console.groupEnd(chalk.magenta('Arr['+val+']'))
}
}
}
break
case 'boolean':
console.log('-> Its value is '+ chalk.blue(value == null ? 'null' : value.toString()))
break
}
console.groupEnd()
}
/**
* Starts a timer between two segments of code
* @param {string} group custom grouping variable
*/
function go(group = QUACK) {
console.time(group)
}
/**
* Stops a previously started timer
* @param {string} group custom grouping variable
*/
function stop(group = QUACK) {
console.timeEnd(group)
}
/**
* Tests a certain function (Async or not) and checks if the desired result is gotten
* @param {function} testable Function to be tested
* @param {array} args Arguments to pass to the function as an Array (ex. [1,2])
* @param {any} expectedResult Expected result
* @param {boolean} debug Flag to determine if the stack should be printed
* @param {number} level Level of detail of the stack
* @param {flag} detailed Flag to determine if all data from the stack should be printed
*/
async function test(testable, args, expectedResult, debug = false, level = 3, detailed = false) {
console.log(QUACK)
console.log('Function name: ' + chalk.cyan(testable.name))
console.group('--Result')
let result
switch (testable.constructor.name) {
case 'Function':
result = testable(...args)
console.log('Function Result: ' + chalk.yellow(result))
console.log('Expected Result: ' + chalk.yellow(expectedResult))
console.log(result === expectedResult ? chalk.green('Test successful!') : chalk.red('Test failed!'))
break
case 'AsyncFunction':
result = await testable(...args)
console.log('Function Result: ' + chalk.yellow(result))
console.log('Expected Result: ' + chalk.yellow(expectedResult))
console.log(result === expectedResult ? chalk.green('Test successful!') : chalk.red('Test failed!'))
break
default:
console.log('dedault')
break
}
if (debug) {
printStack(level, detailed)
}
console.groupEnd('--Result')
}
/**
* Renames the keys of a given object
* @param {Object} object Object whose keys will be changed (ex. {'a': 2})
* @param {Object} changes Object that maps the required changes (ex. {a: 'b'}) => {'b':2
* @returns {Object} Modified object with the new keys
*/
function rename(object, changes) {
if (!changes || typeof changes !== 'object') {
return object
}
if (Array.isArray(object)) {
const newArray = []
for (var i = 0; i < object.length; i++) {
newArray.push(rename(object[i], changes))
}
return newArray
} else {
if (typeof object !== 'object') {
return object
}
var copy = clone(object)
for (var key in changes) {
if (typeof changes[key] === 'object') {
if (copy.hasOwnProperty(key)) {
copy[key] = rename(copy[key], changes[key])
continue
}
}
if (copy.hasOwnProperty(key)) {
var temp = copy[key]
copy[changes[key]] = temp
delete copy[key]
}
}
return copy
}
}
/**
* Checks if an Object is Empty
* @param {Object} obj Object to check for emptyness
* @returns Flag to indicate if it's empty or not
*/
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
/**
* Categorizes JSON Elements into types and values
* @param {Object} obj Object to check
* @returns {Array} Array to be printed or checked
*/
function xray(obj) {
var cats = []
for (var key in obj) {
cats.push({
Key: key,
Value: obj[key],
Type: typeof obj[key]
})
}
return cats
}
/**
* Generates a random number
* @param {boolean} floating Flag to represent if desired value should be floating
* @param {number} min Minimum value to generate
* @param {number} max Maximum value to generate
* @returns Random number
*/
function random(floating = false, min = 0, max = Number.MAX_SAFE_INTEGER) {
let number = (Math.random() * (max - min)) + min
return !floating ? Math.floor(number) : number
}
/**
* Generates an Object to use on tests based on a given schema
* @param {Object} schema Schema and Data Type to use on the fake object (ex. {'name':name, 'age':number, 'engaged':boolean})
* @param {Number} amount Amount of fake Objects to create
* @returns Object with the required fake data
*/
function fake(schema) {
let fak = {}
let card = faker.helpers.contextualCard()
for (var key in schema) {
if (typeof schema[key] == 'object') {
fak[key] = fake(schema[key])
} else {
switch (schema[key]) {
case 'age': case 'number' :
fak[key] = random(false, 18, 100)
break
case 'job':
fak[key] = faker.name.jobTitle()
break
case 'blood':
var bloodT = ['A', 'B', 'AB', 'O']
var RH = ['+', '-']
fak[key] = { 'type': bloodT[random(false, 0, bloodT.length)], 'rh': RH[random(false, 0, RH.length)] }
break
case 'color':
fak[key] = faker.commerce.color()
break
case 'ip' :
fak[key] = faker.internet.ip()
break
case 'password':
fak[key] = faker.internet.password()
break
case 'title' :
fak[key] = faker.name.title
break
case 'word' :
fak[key] = faker.lorem.word()
break
case 'sentence' :
fak[key] = faker.lorem.sentence()
break
case 'paragraphs' :
fak[key] = faker.lorem.paragraphs(3)
break
default :
fak[key] = card[schema[key]]
break
}
}
}
return fak
}
/**
* Creates a link to stackoverflow based on an error
* @param {Error} error Error to convert
*/
function stackoverflow(error){
console.log(QUACK)
if(error.message) {
var xcb = searchable(error.message)
console.log(chalk.yellow('Check on SO for Error > '+error.message))
console.log(chalk.blue(xcb))
} else {
console.log(chalk.red('Error message not present'))
}
}
/**
* Modifies a string to be searchable on search motors
* @param {String} query Natural language sentence to be converted
* @param {String} website Website to search
* @returns {String} Link to the desired website
*/
function searchable(query,website='stackoverflow') {
const websites = {
stackoverflow : "http://stackoverflow.com/search?q=[js]+",
google: "http://www.google.com/search?q=",
youtube: 'https://www.youtube.com/results?search_query=',
duckduckgo: 'https://duckduckgo.com/?q=',
bing : 'https://www.bing.com/search?q=',
yahoo: 'https://mx.search.yahoo.com/search?p='
}
if(query) {
return websites[website]+query.split(" ").map(word => word.toLowerCase()).join('+')
} else {
return query
}
}
module.exports = {
quack,
check,
go,
stop,
test,
rename,
xray,
random,
fake,
stackoverflow,
searchable
}