Skip to content

Commit

Permalink
Added newer functions
Browse files Browse the repository at this point in the history
Added most initial functions for the package to work
  • Loading branch information
martin-headspace committed Oct 19, 2019
1 parent 9709f2c commit 35a968b
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 0 deletions.
162 changes: 162 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@


// ,---, ,-.
// .' .' `\ ,--/ /|
// ,---.' \ ,--, ,--. :/ | __ ,-.
// | | .`\ | ,'_ /| : : ' / ,' ,'/ /|
// : : | ' | .--. | | : ,---. | ' / ,---. ' | |' |
// | ' ' ; :,'_ /| : . | / \ ' | : / \ | | ,'
// ' | ; . || ' | | . . / / ' | | \ / / |' : /
// | | : | '| | ' | | | . ' / ' : |. \ . ' / || | '
// ' : | / ; : | : ; ; | ' ; :__ | | ' \ \' ; /|; : |
// | | '` ,/ ' : `--' \' | '.'|' : |--' ' | / || , ;
// ; : .' : , .-./| : :; |,' | : | ---'
// | ,.' `--`----' \ \ / '--' \ \ /
// '---' `----' `----'
// Built by: Fernando Martin Garcia Del Angel
// Built because I was bored

const StackTrace = require('stacktrace-js')
const utils = require('./utilities/utils')
const QUACK = '🦆 {Quack}'

/**
* Quacks!
* @param {string} group Counts from a custom group
* @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(group = QUACK, debug = false, level = 2, detailed = false) {
console.count(group)
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) {
var stack = StackTrace.getSync().slice(0,level)
for(var i = 0; i < stack.length; i++) {
stack[i] = utils.renameKeys(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) {
console.log(QUACK)
let type = ''
console.log('This value is of type %s',typeof value)
console.group()
switch(typeof value) {
case 'number' :
console.log('-> Its value is %f',value)
break
case 'object' :
if(utils.isEmpty(value)) {
console.log('-> Its value is empty')
} else {
if (value[0] == undefined) {
categorizer(value)
} else {
console.log('-> Array type. First value structure:')
categorizer(value[0])
}
}
break
case 'boolean' :
console.log('-> Its value is %s',value == null ? 'null' : value.toString())
}
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: '+testable.name)
console.group('--Result')
let result
switch(testable.constructor.name) {
case 'Function' :
result = testable(...args)
console.log('Function Result: '+result)
console.log('Expected Result: '+expectedResult)
console.log(result === expectedResult ? 'Test successful!' : 'Test failed!')
break
case 'AsyncFunction' :
result = await testable(...args)
console.log('Function Result: '+result)
console.log('Expected Result: '+expectedResult)
console.log(result === expectedResult ? 'Test successful!' : 'Test failed!')
break
default :
console.log('dedault')
break
}
if(debug) {
printStack(level,detailed)
}
console.groupEnd('--Result')
}

/**
* Categorizes JSON elements
* @param {Object} obj Object element
*/
function categorizer(obj) {
var cats = []
for (var key in obj) {
cats.push({
Key : key,
Type : obj[key],
Value : typeof obj[key]
})
}
console.table(cats)
}

module.exports = {
quack,
check,
go,
stop,
test
}
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "duckerdbg",
"version": "0.0.1",
"description": "Simple console debugger for simple applications",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/a01334390/ducker.git"
},
"keywords": [
"debug",
"testing",
"duck"
],
"author": "Fernando Martin Garcia Del Angel",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/a01334390/ducker/issues"
},
"homepage": "https://github.com/a01334390/ducker#readme",
"dependencies": {
"clone": "^2.1.2",
"stacktrace-js": "^2.0.1"
}
}
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const ducker = require('./index')

function sum (a,b) {
return a + b
}

async function mult (a,b) {
return await a * b
}

ducker.go()
ducker.quack([{ 'a': 1 }, { 'a': 2 }])
ducker.quack('wow', true)
ducker.check(2)
ducker.check({ 'a': 2, 'b': 4 })
ducker.check([{ 'a': 2, 'b': 4 }, { 'a': 2, 'b': 4 }])
ducker.check([])
ducker.check(false)
ducker.check(null)
ducker.quack()
ducker.quack()
ducker.quack()
ducker.quack()
ducker.test(sum,[1,2],3)
ducker.test(mult,[2,5],3,true)
ducker.stop()

50 changes: 50 additions & 0 deletions utilities/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var clone = require('clone')

function renameKeys(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(renameKeys(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] = renameKeys(copy[key], changes[key])
continue
}
}

if (copy.hasOwnProperty(key)) {
var temp = copy[key]

copy[changes[key]] = temp
delete copy[key]
}
}
return copy
}
}

function isEmpty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}

module.exports = {
renameKeys,
isEmpty
}

0 comments on commit 35a968b

Please sign in to comment.