Created
June 23, 2020 09:36
-
-
Save PetraSp/c68c34b0a036988d4c988fc968d9fb0b to your computer and use it in GitHub Desktop.
Tagged template literals
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
| const logger = (strings, ...values) => { | |
| var str = ''; | |
| for (let i = 0; i < strings.length; i++) { | |
| if (i > 0) { | |
| if (values[i-1] && typeof values[i-1] == "object") { | |
| if (values[i-1] instanceof Error) { | |
| if (values[i-1].stack) { | |
| str += values[i-1].stack; | |
| continue; | |
| } | |
| } | |
| else{ | |
| try { | |
| str += JSON.stringify(values[i-1]); | |
| continue; | |
| } | |
| catch (err) {} | |
| } | |
| } | |
| str += values[i-1]; | |
| } | |
| str += strings[i]; | |
| } | |
| console.log(str); | |
| return str; | |
| } | |
| var v = 42; | |
| var o = { | |
| a: 1, | |
| b: [2,3,4] | |
| } | |
| logger `this is my value: ${v} and another ${o}` | |
| try { | |
| nothing(); | |
| } | |
| catch (err) { | |
| logger `caught: ${err}`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment