Last active
October 29, 2018 18:30
-
-
Save Alpvax/39fcf584c44092e88c10c0ba6dec643f to your computer and use it in GitHub Desktop.
A template literal tag for formatting templates in console outputs
This file contains 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
let objconsole = new Proxy(function(strs, ...args) { | |
let ret = strs.reduce((a, c, i) => { | |
if(c) a.concat(c); | |
if(i < args.length) a.concat(args[i]); | |
return a; | |
}, []); | |
return ret; | |
}, new Proxy({}, { | |
get(handler, trapkey) { | |
if(trapkey === "apply") { | |
return undefined; | |
} else if(trapkey === "get") { | |
return function(target, name, ...args) { | |
if(["debug", "info", "log", "warn", "error"].includes(name)) { | |
return new Proxy(Reflect.get(console, name, ...args), { | |
apply(target, thisArg, argumentsList) { | |
args = ...argumentsList; | |
console.debug("ARGS:", argumentsList, "-->", args);//XXX | |
Reflect.apply(target, thisArg, args); | |
} | |
}); | |
} | |
return Reflect.get(console, name, ...args); | |
} | |
} | |
return function(target, ...args) { | |
return Reflect[trapkey](console, ...args); | |
} | |
} | |
})); | |
let objconsole = new Proxy(function(strs, ...args) { | |
return strs.reduce((a, c, i) => { | |
if(c) { | |
let str = c; | |
if(i > 0) str = str.replace(/^ /, ""); //Strip initial space if not first | |
if(i < args.length) str = str.replace(/ $/, ""); //Strip trailing space if not last | |
a = a.concat(str); | |
} | |
if(i < args.length) { | |
a = a.concat([args[i]]); // Don't over-flatten | |
} | |
return a; | |
}, []); | |
}, new Proxy({}, { | |
get(handler, trapkey) { | |
if(trapkey === "apply") { | |
return undefined; // Pass through to the wrapped function | |
} else if(trapkey === "get") { | |
return function(target, name, ...args) { // return custom log functions | |
if(["debug", "info", "log", "warn", "error"].includes(name)) { | |
return new Proxy(Reflect.get(console, name, ...args), { | |
// Wrap and expand all arguments to create a single 1d array | |
apply(target, thisArg, argumentsList) { | |
args = [].concat(...argumentsList); | |
Reflect.apply(target, thisArg, args); | |
} | |
}); | |
} | |
return Reflect.get(console, name, ...args); // Pass every other property request through to console | |
} | |
} | |
return function(target, ...args) { // Pass everything else through to console | |
return Reflect[trapkey](console, ...args); | |
} | |
} | |
})); | |
console.log(...objconsole`Hello ${{age:"old"}} world`); | |
function func() {console.log("Dummy Function"); } | |
console.log(...objconsole`What${"Happens"} when ${"there"}${" are"} many different types such as: | |
numbers: ${1} | |
objects: ${{aKey: "value", numKey: 2, funKey: func}} | |
functions: ${func} | |
strings: ${"alphabet"} | |
templates: ${`${1} + 2 = ${1 + 2}`}?`) | |
objconsole.warn(objconsole`Hello ${{age:"old"}} world`); | |
objconsole.log(objconsole`Hello ${{age:"old"}} world`, objconsole`Another ${1} object: ${{key: 14}}`); | |
objconsole.log(objconsole`Testing an array of ${[1,2,3,4,5]}`); | |
objconsole.log(objconsole`Testing an array of ${[1,2,3,4,5]} and another of ${"abcd".split("")}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment