Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print JSON in any method #3

Merged
merged 5 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ c.w() = console.warn()
c.j() = console.log() for logging JavaScript JSON objects
```

Printing JSON works for all console methods
```javascript
c.l({hello: 'world'});
```
Or
```javascript
c.l('object is %s', {hello: 'world'});
```

For log() messages you can use 3 different ways to insert a variable:
```javascript
let str = "Awesome";
Expand Down
116 changes: 61 additions & 55 deletions smart-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,77 @@

'use strict';

function textToJSON (text) {
var newText = text;
if (typeof text === 'object') newText = JSON.stringify(text, null, 4); //Only stringify if the variable is JSON
return newText;
}

var c = {
a: (...extraParam) => console.assert(...extraParam), // console.assert()
dir: (param) => console.dir(param), // console.dir() - non-standard method
e: (text, ...extraParam) => console.error(text, ...extraParam), // console.error()
i: (text, ...extraParam) => console.info(text, ...extraParam), // console.info()
l: (text, ...extraParam) => console.log(text, ...extraParam), // console.log()
p: (param) => param ? console.profile(param) : console.profile(), // console.profile()
a: (...extraParam) => console.assert(textToJSON(...extraParam)), // console.assert()
dir: (param) => console.dir(textToJSON(param)), // console.dir() - non-standard method
e: (text, ...extraParam) => console.error(textToJSON(text), textToJSON(...extraParam)), // console.error()
i: (text, ...extraParam) => console.info(textToJSON(text), textToJSON(...extraParam)), // console.info()
l: (text, ...extraParam) => console.log(textToJSON(text), textToJSON(...extraParam)), // console.log()
p: (param) => param ? console.profile(textToJSON(param)) : console.profile(), // console.profile()
pe: () => console.profileEnd(), // console.profileEnd()
t: (param) => param ? console.time(param) : console.time(), // console.time()
te: (param) => param ? console.timeEnd(param) : console.timeEnd(), // console.timeEnd()
w: (text, ...extraParam) => console.warn(text, ...extraParam), // console.warn()
t: (param) => param ? console.time(textToJSON(param)) : console.time(), // console.time()
te: (param) => param ? console.timeEnd(textToJSON(param)) : console.timeEnd(), // console.timeEnd()
w: (text, ...extraParam) => console.warn(textToJSON(text), textToJSON(...extraParam)), // console.warn()
j: (object, space) => console.log(JSON.stringify(object, null, space || 4)), // console.log() - for logging JSON objects

// log() style formatting
lb: (text, ...extraParam) => console.log("\x1b[1m" + text + "\x1b[0m", ...extraParam), // bold
lf: (text, ...extraParam) => console.log("\x1b[2m" + text + "\x1b[0m", ...extraParam), // faint
lu: (text, ...extraParam) => console.log("\x1b[4m" + text + "\x1b[0m", ...extraParam), // underline
ll: (text, ...extraParam) => console.log("\x1b[5m" + text + "\x1b[0m", ...extraParam), // blink
ln: (text, ...extraParam) => console.log("\x1b[7m" + text + "\x1b[0m", ...extraParam), // negative
lh: (text, ...extraParam) => console.log("\x1b[8m" + text + "\x1b[0m", ...extraParam), // hidden
lb: (text, ...extraParam) => console.log("\x1b[1m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // bold
lf: (text, ...extraParam) => console.log("\x1b[2m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // faint
lu: (text, ...extraParam) => console.log("\x1b[4m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // underline
ll: (text, ...extraParam) => console.log("\x1b[5m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blink
ln: (text, ...extraParam) => console.log("\x1b[7m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // negative
lh: (text, ...extraParam) => console.log("\x1b[8m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // hidden
// log() colors
lK: (text, ...extraParam) => console.log("\x1b[30m" + text + "\x1b[0m", ...extraParam), // black
lR: (text, ...extraParam) => console.log("\x1b[31m" + text + "\x1b[0m", ...extraParam), // red
lG: (text, ...extraParam) => console.log("\x1b[32m" + text + "\x1b[0m", ...extraParam), // green
lY: (text, ...extraParam) => console.log("\x1b[33m" + text + "\x1b[0m", ...extraParam), // yellow
lB: (text, ...extraParam) => console.log("\x1b[34m" + text + "\x1b[0m", ...extraParam), // blue
lM: (text, ...extraParam) => console.log("\x1b[35m" + text + "\x1b[0m", ...extraParam), // magenta
lC: (text, ...extraParam) => console.log("\x1b[36m" + text + "\x1b[0m", ...extraParam), // cyan
lW: (text, ...extraParam) => console.log("\x1b[37m" + text + "\x1b[0m", ...extraParam), // white
lK: (text, ...extraParam) => console.log("\x1b[30m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // black
lR: (text, ...extraParam) => console.log("\x1b[31m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // red
lG: (text, ...extraParam) => console.log("\x1b[32m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // green
lY: (text, ...extraParam) => console.log("\x1b[33m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // yellow
lB: (text, ...extraParam) => console.log("\x1b[34m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blue
lM: (text, ...extraParam) => console.log("\x1b[35m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // magenta
lC: (text, ...extraParam) => console.log("\x1b[36m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // cyan
lW: (text, ...extraParam) => console.log("\x1b[37m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // white
// log() bold + colors
lbK: (text, ...extraParam) => console.log("\x1b[1m\x1b[30m" + text + "\x1b[0m", ...extraParam), // black
lbR: (text, ...extraParam) => console.log("\x1b[1m\x1b[31m" + text + "\x1b[0m", ...extraParam), // red
lbG: (text, ...extraParam) => console.log("\x1b[1m\x1b[32m" + text + "\x1b[0m", ...extraParam), // green
lbY: (text, ...extraParam) => console.log("\x1b[1m\x1b[33m" + text + "\x1b[0m", ...extraParam), // yellow
lbB: (text, ...extraParam) => console.log("\x1b[1m\x1b[34m" + text + "\x1b[0m", ...extraParam), // blue
lbM: (text, ...extraParam) => console.log("\x1b[1m\x1b[35m" + text + "\x1b[0m", ...extraParam), // magenta
lbC: (text, ...extraParam) => console.log("\x1b[1m\x1b[36m" + text + "\x1b[0m", ...extraParam), // cyan
lbW: (text, ...extraParam) => console.log("\x1b[1m\x1b[37m" + text + "\x1b[0m", ...extraParam), // white
lbK: (text, ...extraParam) => console.log("\x1b[1m\x1b[30m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // black
lbR: (text, ...extraParam) => console.log("\x1b[1m\x1b[31m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // red
lbG: (text, ...extraParam) => console.log("\x1b[1m\x1b[32m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // green
lbY: (text, ...extraParam) => console.log("\x1b[1m\x1b[33m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // yellow
lbB: (text, ...extraParam) => console.log("\x1b[1m\x1b[34m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blue
lbM: (text, ...extraParam) => console.log("\x1b[1m\x1b[35m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // magenta
lbC: (text, ...extraParam) => console.log("\x1b[1m\x1b[36m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // cyan
lbW: (text, ...extraParam) => console.log("\x1b[1m\x1b[37m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // white
// log() underline + bold + colors
luK: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[30m" + text + "\x1b[0m", ...extraParam), // black
luR: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[31m" + text + "\x1b[0m", ...extraParam), // red
luG: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[32m" + text + "\x1b[0m", ...extraParam), // green
luY: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[33m" + text + "\x1b[0m", ...extraParam), // yellow
luB: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[34m" + text + "\x1b[0m", ...extraParam), // blue
luM: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[35m" + text + "\x1b[0m", ...extraParam), // magenta
luC: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[36m" + text + "\x1b[0m", ...extraParam), // cyan
luW: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[37m" + text + "\x1b[0m", ...extraParam), // white
luK: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[30m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // black
luR: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[31m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // red
luG: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[32m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // green
luY: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[33m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // yellow
luB: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[34m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blue
luM: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[35m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // magenta
luC: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[36m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // cyan
luW: (text, ...extraParam) => console.log("\x1b[4m\x1b[1m\x1b[37m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // white
// log() blink + bold + colors
llK: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[30m" + text + "\x1b[0m", ...extraParam), // black
llR: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[31m" + text + "\x1b[0m", ...extraParam), // red
llG: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[32m" + text + "\x1b[0m", ...extraParam), // green
llY: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[33m" + text + "\x1b[0m", ...extraParam), // yellow
llB: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[34m" + text + "\x1b[0m", ...extraParam), // blue
llM: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[35m" + text + "\x1b[0m", ...extraParam), // magenta
llC: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[36m" + text + "\x1b[0m", ...extraParam), // cyan
llW: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[37m" + text + "\x1b[0m", ...extraParam), // white
llK: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[30m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // black
llR: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[31m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // red
llG: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[32m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // green
llY: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[33m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // yellow
llB: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[34m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blue
llM: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[35m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // magenta
llC: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[36m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // cyan
llW: (text, ...extraParam) => console.log("\x1b[5m\x1b[1m\x1b[37m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // white
// log() bold + background colors
lKBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[40m" + text + "\x1b[0m", ...extraParam), // black
lRBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[41m" + text + "\x1b[0m", ...extraParam), // red
lGBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[42m" + text + "\x1b[0m", ...extraParam), // green
lYBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[43m" + text + "\x1b[0m", ...extraParam), // yellow
lBBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[44m" + text + "\x1b[0m", ...extraParam), // blue
lMBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[45m" + text + "\x1b[0m", ...extraParam), // magenta
lCBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[46m" + text + "\x1b[0m", ...extraParam), // cyan
lWBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[47m" + text + "\x1b[0m", ...extraParam), // white
lKBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[40m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // black
lRBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[41m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // red
lGBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[42m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // green
lYBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[43m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // yellow
lBBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[44m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // blue
lMBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[45m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // magenta
lCBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[46m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // cyan
lWBG: (text, ...extraParam) => console.log("\x1b[1m\x1b[47m" + textToJSON(text) + "\x1b[0m", textToJSON(...extraParam)), // white
}

module.exports = {c: c}