Last active
June 11, 2017 02:30
-
-
Save afaur/968e1a6b5c3581fef89ef8819fdfb264 to your computer and use it in GitHub Desktop.
Make a replacement log function that doesn't require the parentheses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get stdout off process as outPipe | |
const { stdout: outPipe } = process | |
// Import log and trace off of console | |
const { log, trace } = console | |
// Make a logRaw function | |
const logRaw = (...args) => outPipe.write(...args) | |
// Easy log fn for node | |
function echo() { | |
const maybeArray = Array.prototype.slice.call(arguments)[0] | |
const logString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0] | |
const templateVarToValue = (match, string, offset) => eval(Array.from(match).splice(1).toString()) | |
log(logString.replace(/(\$[^\W]+)/g, templateVarToValue)) | |
} | |
// Give back an array of the stacktrace items | |
const traceIt = () => { | |
var orig = Error.prepareStackTrace | |
Error.prepareStackTrace = function(_, stack) { | |
return stack | |
} | |
var err = new Error | |
Error.captureStackTrace(err, arguments.callee) | |
var stack = err.stack | |
Error.prepareStackTrace = orig | |
return stack | |
} | |
// Takes a string of variable names comma seperated | |
// In: `foo, bar` => Out: `foo: fooVal, bar: barVal` | |
function varDump(/*str, stackTrace=false*/) { | |
// Parse the arguments | |
const maybeArray = Array.prototype.slice.call(arguments)[0] | |
// Parse first argument | |
const varString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0] | |
// Take all arguments, variable names | |
var varArray = varString.split(',') | |
// Take the last argument | |
const stackTrace = (varArray.pop().trim().toLowerCase() === 'true') | |
// If we need to create a stack trace | |
stackTrace && traceIt().forEach((site) => { | |
log(' \033[36m%s\033[90m in %s:%d\033[0m', | |
site.getFunctionName() || 'anonymous', | |
site.getFileName(), site.getLineNumber() | |
) | |
}) | |
// For each to variable names log out its current value | |
varArray | |
.map((__) => __.trim()) | |
.forEach((__) => logRaw(`${__}: ${eval(__)} `)) | |
// Add a newline at the end | |
logRaw("\n") | |
} | |
const a = "foo" | |
const b = "bar" | |
const c = "baz" | |
//echo`The value of: a=$a, b=$b, and c=$c` | |
//varDump`a, b, c, false` | |
module.exports = { varDump, echo } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First argument will be an `array` when called as: log`something` | |
// First argument will be a `string` when called as: log(`something`) | |
function log(/* arguments */) { | |
var maybeArray = Array.prototype.slice.call(arguments)[0] | |
var resultString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0] | |
console.log(resultString) | |
} | |
// Can call function without `()` | |
log`something` | |
// Can call function with `()` | |
log(`something`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Refactor approach to use something similar to this after refactoring to include some observed limitations listed below..
Use
return debug.fn
at the beginning of any function you would like to debug inside.Wherever you use
echo
to inspect inside that function should work now because we replace it.This works by using
arguments.callee.caller
to get all the code of the function, that the current function was called inside of.This program alters the original functions code and runs the altered version of the function respecting return values.
Some problems I can see are:
$this.something
for matching.this
over to the newly executing function.;
at end of code lines.arguments.callee.caller
is not available so I have written an alternative...