Skip to content

Commit

Permalink
Added last two functions
Browse files Browse the repository at this point in the history
It's now possible to create a link from an error message to stackoverflow and to create links to search for any sentence
  • Loading branch information
martin-headspace committed Oct 20, 2019
1 parent 728584f commit 242fb52
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
71 changes: 58 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -296,5 +339,7 @@ module.exports = {
rename,
xray,
random,
fake
fake,
stackoverflow,
searchable
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
20 changes: 15 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 242fb52

Please sign in to comment.