diff --git a/index.js b/index.js index b62d5d4..b49d8db 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,7 @@ const StackTrace = require('stacktrace-js') const clone = require('clone') const faker = require('faker') +const ErrorParser = require('error-stack-parser') const chalk = require('chalk') /** @@ -37,8 +38,7 @@ const QUACK = '🦆 {Quack}' * @param {boolean} detailed Prints the complete stack trace */ function quack(word = '', debug = false, level = 2, detailed = false) { - var stack = StackTrace.getSync() - console.log(QUACK+' > '+(!word ? quack.caller.name : word)) + console.log(QUACK+' > '+chalk.yellow(!word ? quack.caller.name : word)) if (debug) { printStack(level, detailed) } @@ -70,28 +70,33 @@ function printStack(level, detailed) { * @param {any} value Value tested for being truthy */ function check(value) { - console.log(QUACK) let type = '' - console.log('This value is of type %s', typeof value) + console.log('This value is of type '+chalk.blue(typeof value)) console.group() switch (typeof value) { - case 'number': - console.log('-> Its value is %f', value) + case 'number': case 'string' : + console.log('-> Its value is '+chalk.green(value)) break case 'object': if (isEmpty(value)) { - console.log('-> Its value is empty') + console.log('-> Its value is '+chalk.red('empty')) } else { if (value[0] == undefined) { console.table(xray(value)) } else { - console.log('-> Array type. First value structure:') - console.table(xray(value[0])) + 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 %s', value == null ? 'null' : value.toString()) + console.log('-> Its value is '+ chalk.blue(value == null ? 'null' : value.toString())) + break + } console.groupEnd() } @@ -213,8 +218,8 @@ function xray(obj) { for (var key in obj) { cats.push({ Key: key, - Type: obj[key], - Value: typeof obj[key] + Value: obj[key], + Type: typeof obj[key] }) } return cats @@ -287,6 +292,44 @@ function fake(schema) { 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, @@ -296,5 +339,7 @@ module.exports = { rename, xray, random, - fake + fake, + stackoverflow, + searchable } \ No newline at end of file diff --git a/package.json b/package.json index b0c7cbb..b33a676 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "chalk": "^2.4.2", "clone": "^2.1.2", + "error-stack-parser": "^2.0.4", "faker": "^4.1.0", "stacktrace-js": "^2.0.1" } diff --git a/test.js b/test.js index e058bd6..8ddf1c9 100644 --- a/test.js +++ b/test.js @@ -1,17 +1,27 @@ const ducker = require('./index') function sum (a,b) { - ducker.quack() return a + b } async function mult (a,b) { - ducker.quack('',true) - return await a * b + return await a / 0 } -mult(5,4) -sum(1,2) +// try { +// JSON.parse({"foo": 1,}) +// } catch(e){ +// console.log(e) +// ducker.stackoverflow(e) +// } + +console.log(ducker.searchable('Amazing thing you got there baby','bing')) +// sum(1,2) +// ducker.check([1,2,['wow',true,[{'a' : 2}]]]) +// ducker.stackoverflow(new RangeError()) + + +// ducker.test(mult,[2,2],4,true) // ducker.quack('',true,-1) // console.log(ducker.random(false,5,6)) // console.log(ducker.fake({