Comparing version 1.1.0 to 1.1.1
191
main.js
let Col = require('colors'); | ||
let util = require('util') | ||
let util = require('util'); | ||
const v8 = require('v8'); | ||
/** | ||
* @returns {any[]} | ||
* @param {any[]} el | ||
*/ | ||
const clone = el => { | ||
return v8.deserialize(v8.serialize(el)); | ||
} | ||
/** | ||
* @description Sorts by string length. | ||
* @param {string} f First param | ||
* @param {string} s Second param | ||
*/ | ||
const lSort = (f, s) => { | ||
if (f.length < s.length) { | ||
return -1; | ||
} | ||
if (f.length > s.length) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
Col.setTheme({ | ||
@@ -23,3 +48,3 @@ e: ['red'], | ||
REAL_INDENT: 0, | ||
STEPS_TO_INDENT: 1, | ||
indentationSteps: 1, | ||
GROUPS_IN: [], | ||
@@ -29,31 +54,33 @@ NEAR_GROUP_START: false, | ||
CATCHED: [], | ||
MAX_INDENT: 10, | ||
MESSAGES: { | ||
maxIndent: 10, | ||
msg: { | ||
info: " INFO ", | ||
warn: " WARN ", | ||
error: " ERR ", | ||
success: " SUCC ", | ||
fatal: "FATAL ", | ||
catch: "CATCH ", | ||
catched: "CATCH ", | ||
groupIn: "> ", | ||
groupOut: "< ", | ||
}, | ||
/** | ||
* @description Changes the maximum indentaion possible. Indenting when max-indent is already reached does nothing. | ||
* @param {Number} m Maximum indentation. -1 for unlimited. | ||
*/ | ||
maximumIndentation(m) { | ||
if (m === -1) { | ||
this.MAX_INDENT = Number.MAX_SAFE_INTEGER; | ||
} else { | ||
this.MAX_INDENT = m; | ||
} | ||
BOX: { | ||
h: "─", | ||
v: "│", | ||
se: "┌", | ||
sw: "┐", | ||
ne: "└", | ||
nw: "┘", | ||
ve: "├", | ||
vw: "┤", | ||
hs: "┬", | ||
hn: "┴", | ||
hv: "┼", | ||
dh: "╌", | ||
dv: "╎", | ||
cse: "╭", | ||
csw: "╮", | ||
cne: "╰", | ||
cnw: "╯" | ||
}, | ||
/** | ||
* @description Changes the steps used to indent each time. | ||
* @param {Number} l How many spaces | ||
*/ | ||
indentationLength(l) { | ||
this.STEPS_TO_INDENT = l; | ||
}, | ||
/** | ||
* @description Sets the theme of the logging. The object's elements are 'e' for error, 'he' for the error head, 'f' for fatal error, 'hf' for the fatal head, 'w' for warn, 'hw' for the warn head, 's' for success, 'hs' for the success head, 'i' for info, 'hi' for the info head, 'c' for catched error, 'hc' for catched head, 'g' for group headers. For how to define colors, see the color package guide. | ||
@@ -73,8 +100,8 @@ * @param {Object} theme | ||
this.NEAR_GROUP_END = false; | ||
process.stdout.write(" ".repeat(this.INDENTATION * this.STEPS_TO_INDENT) + util.format.apply(this, arguments) + '\n'); | ||
process.stdout.write(" ".repeat(this.INDENTATION * this.indentationSteps) + util.format.apply(this, arguments) + '\n'); | ||
}, | ||
/** | ||
* @description Logs an info message. NOTICE: to use the default head but also formatting, the second argument must be falsy. | ||
* @param {String} msg What to log. | ||
* @param {String} [head] The head message. | ||
* @param {string} msg What to log. | ||
* @param {string} [head] The head message. | ||
* @param {...any} [optionalParams] Formats. | ||
@@ -86,3 +113,3 @@ */ | ||
} else { | ||
this.log(this.MESSAGES.info.hi + " " + msg.i, ...optionalParams); | ||
this.log(this.msg.info.hi + " " + msg.i, ...optionalParams); | ||
} | ||
@@ -92,4 +119,4 @@ }, | ||
* @description Logs a warning message. NOTICE: to use the default head but also formatting, the second argument must be falsy. | ||
* @param {String} msg What to log. | ||
* @param {String} [head] The head message. | ||
* @param {string} msg What to log. | ||
* @param {string} [head] The head message. | ||
* @param {...any} [optionalParams] Formats. | ||
@@ -101,3 +128,3 @@ */ | ||
} else { | ||
this.log(this.MESSAGES.warn.hw + " " + msg.w, ...optionalParams); | ||
this.log(this.msg.warn.hw + " " + msg.w, ...optionalParams); | ||
} | ||
@@ -107,4 +134,4 @@ }, | ||
* @description Logs a success message. NOTICE: to use the default head but also formatting, the second argument must be falsy. | ||
* @param {String} msg What to log. | ||
* @param {String} [head] The head message. | ||
* @param {string} msg What to log. | ||
* @param {string} [head] The head message. | ||
* @param {...any} [optionalParams] Formats. | ||
@@ -116,3 +143,3 @@ */ | ||
} else { | ||
this.log(this.MESSAGES.warn.hs + " " + msg.s, ...optionalParams); | ||
this.log(this.msg.success.hs + " " + msg.s, ...optionalParams); | ||
} | ||
@@ -122,4 +149,4 @@ }, | ||
* @description Logs a non-fatal error message (doesn't throw). NOTICE: to use the default head but also formatting, the second argument must be falsy. | ||
* @param {String} e The error. | ||
* @param {String} [head] The head message. | ||
* @param {string} e The error. | ||
* @param {string} [head] The head message. | ||
* @param {...any} [optionalParams] Formats. | ||
@@ -137,3 +164,3 @@ */ | ||
} else { | ||
this.log(this.MESSAGES.error.he + " " + (function () { | ||
this.log(this.msg.error.he + " " + (function () { | ||
if (e instanceof Error) { | ||
@@ -149,4 +176,4 @@ return e.message.e; | ||
* @description Logs a fatal error message (throws). NOTICE: to use the default head but also formatting, the second argument must be falsy. | ||
* @param {String} e The error. | ||
* @param {String} [head] The head message. | ||
* @param {string} e The error. | ||
* @param {string} [head] The head message. | ||
* @param {...any} [optionalParams] Formats. | ||
@@ -165,3 +192,3 @@ */ | ||
} else { | ||
this.log(this.MESSAGES.fatal.hf + " " + (function () { | ||
this.log(this.msg.fatal.hf + " " + (function () { | ||
if (e instanceof Error) { | ||
@@ -173,3 +200,3 @@ return e.message.f; | ||
})(), ...optionalParams); | ||
throw new Error(this.MESSAGES.fatal.hf + ' ' + 'Fatal error occurred. See above for details.'.f); | ||
throw new Error(this.msg.fatal.hf + ' ' + 'Fatal error occurred. See above for details.'.f); | ||
}; | ||
@@ -179,3 +206,3 @@ }, | ||
* @description Catches an error for future logging. | ||
* @param {Error|string} e | ||
* @param {Error|string} e The error to catch. | ||
*/ | ||
@@ -189,3 +216,3 @@ catch(e) { | ||
} else { | ||
this.log(this.MESSAGES.catch.hc + ' ' + e.c); | ||
this.log(this.msg.catched.hc + ' ' + e.c); | ||
} | ||
@@ -195,3 +222,3 @@ }, | ||
* @description Logs all catched messages. | ||
* @param {String} [head] The head message. | ||
* @param {string} [head] The head message. | ||
*/ | ||
@@ -218,8 +245,8 @@ uncatch(head) { | ||
SHIFT() { | ||
this.REAL_INDENT += this.STEPS_TO_INDENT; | ||
if (this.REAL_INDENT <= this.MAX_INDENT) this.INDENTATION = this.REAL_INDENT; | ||
this.REAL_INDENT += this.indentationSteps; | ||
if (this.maxIndent === -1 || this.REAL_INDENT <= this.maxIndent) this.INDENTATION = this.REAL_INDENT; | ||
}, | ||
UNSHIFT() { | ||
if (this.REAL_INDENT - this.STEPS_TO_INDENT >= 0) { | ||
this.REAL_INDENT -= this.STEPS_TO_INDENT; | ||
if (this.REAL_INDENT - this.indentationSteps >= 0) { | ||
this.REAL_INDENT -= this.indentationSteps; | ||
this.INDENTATION = this.REAL_INDENT; | ||
@@ -230,3 +257,3 @@ } | ||
* @description Starts a group. | ||
* @param {String} name The group's name. | ||
* @param {string} name The group's name. | ||
*/ | ||
@@ -236,7 +263,7 @@ group(name) { | ||
if (name) { | ||
this.log((this.MESSAGES.groupIn + name).g); | ||
this.log((this.msg.groupIn + name).g); | ||
this.SHIFT(); | ||
this.GROUPS_IN.push(name) | ||
} else { | ||
this.log((this.MESSAGES.groupIn + 'Unnamed group').g); | ||
this.log((this.msg.groupIn + 'Unnamed group').g); | ||
this.SHIFT(); | ||
@@ -253,8 +280,70 @@ this.GROUPS_IN.push('Unnamed group'); | ||
this.UNSHIFT(); | ||
this.log((this.MESSAGES.groupOut + " " + this.GROUPS_IN[this.GROUPS_IN.length - 1]).g); | ||
this.log((this.msg.groupOut + " " + this.GROUPS_IN[this.GROUPS_IN.length - 1]).g); | ||
this.GROUPS_IN.pop(); | ||
this.NEAR_GROUP_END = true; | ||
}, | ||
groupEnd() { | ||
this.ungroup() | ||
}, | ||
LOGTABLE(t) { | ||
e = ""; | ||
t.forEach(el => e += el + '\n'); | ||
e = e.slice(0, -1); | ||
return e | ||
}, | ||
//TODO table | ||
table(arr) { | ||
if (arr instanceof Array && arr[0] instanceof Array) { | ||
// 2d array table. | ||
} else if (arr instanceof Array && arr[0] !== undefined) { | ||
// 1d array table. | ||
/* | ||
["a", "bb", "ccc"] => | ||
┌───┐ | ||
│ a │ | ||
├───┤ | ||
│bb │ | ||
├───┤ | ||
│ccc│ | ||
└───┘ | ||
*/ | ||
let lines = []; | ||
arr.forEach((el, id) => {arr[id] = util.format(el)}); | ||
let sortedByLength = clone(arr).sort(lSort); | ||
let reversedlySBL = clone(sortedByLength).reverse(); | ||
lines[0] = `${this.BOX.se}${this.BOX.h.repeat(reversedlySBL[0].length)}${this.BOX.sw}` | ||
this.log(this.LOGTABLE(lines)); | ||
arr.forEach(el => { | ||
lines.push(`${this.BOX.v}${el}${this.BOX.v}`); | ||
lines.push(`${this.BOX.ve}${this.BOX.h.repeat(reversedlySBL[0].length)}${this.BOX.vw}`); | ||
}); | ||
this.log(this.LOGTABLE(lines)); | ||
lines.pop(); | ||
lines.push(`${this.BOX.ne}${this.BOX.h.repeat(reversedlySBL[0].length)}${this.BOX.nw}`); | ||
this.log(this.LOGTABLE(lines)); | ||
} else if (arr instanceof Array) { | ||
// 0d array table. | ||
this.log(`${this.BOX.se}${this.BOX.sw}\n${this.BOX.ne}${this.BOX.nw}`) | ||
} else if (arr.__proto__ !== undefined && arr instanceof Object && ([...Object.keys(arr)][0] instanceof Object)) { | ||
// 2d object table. | ||
} else if (arr.__proto__ !== undefined && arr instanceof Object && ([...Object.keys(arr)][0] !== undefined)) { | ||
// 1d object table. | ||
} else if (arr instanceof Object) { | ||
// 0d object table. | ||
this.log(`${this.BOX.cse}${this.BOX.csw}\n${this.BOX.cne}${this.BOX.cnw}`) | ||
} else { | ||
this.fatal(new Error(`${arr} is a primitive or a null object.`)) | ||
} | ||
} | ||
} | ||
module.exports = exp; | ||
module.exports = exp; | ||
exp.table([1, 2, 12]) |
{ | ||
"name": "conslog", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Console Logging, done the right way.", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
118
readme.md
@@ -43,5 +43,119 @@ # ConsLog | ||
```typescript | ||
console.log(msg: any, ...optionalParams?: any[]); | ||
function log(msg: any, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout`. Works exactly like `oldC.log` except for the indentation. | ||
Logs to `stdout`. Works exactly like `oldC.log` except for the indentation. | ||
## `warn` | ||
```typescript | ||
function warn(msg: any, head?: string, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout` as a warning. Prefixed with 'warn' or `head`. | ||
## `info` | ||
```typescript | ||
function info(msg: any, head?: string, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout` as an information. Prefixed with 'info' or `head`. | ||
## `error` | ||
```typescript | ||
function error(e: Error, head?: string, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout` as an error. Doesn't throw. Prefixed with 'err' or `head`. | ||
## `success` | ||
```typescript | ||
function success(msg: any, head?: string, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout` as a success message. Prefixed with 'succ' or `head`. | ||
## `fatal` | ||
```typescript | ||
function fatal(e: Error, head?: string, ...optionalParams?: any[]); | ||
``` | ||
Logs to `stdout` as a fatal error. Throws. Prefixed with 'fatal' or `head`. | ||
## `changeTheme` | ||
```typescript | ||
interface Theme { | ||
i: string[], // Info color, defaults to ["blue"] | ||
hi: string[],// Info prefix color, defaults to ["bgBlue", "white"] | ||
w: string[], // Warn color, defaults to ["yellow"] | ||
hw: string[],// Warn prefix color, defaults to ["bgYellow", "white"] | ||
e: string[], // Error color, defaults to ["red"] | ||
he: string[],// Error prefix color, defaults to ["bgRed", "white"] | ||
f: string[], // Fatal color, defaults to ["magenta"] | ||
hf: string[],// Fatal prefix color, defaults to ["bgMagenta", "white"] | ||
s: string[], // Success color, defaults to ["green"] | ||
hs: string[],// Success prefix color, defaults to ["bgGreen", "white"] | ||
c: string[], // Catched error color, defaults to ["red"] | ||
hc: string[],// Catched error prefix color, defaults to ["bgGreen", "white"] | ||
g: string[] // Group header color, defaults to ["inverse"] | ||
} | ||
function changeTheme(theme: Theme); | ||
``` | ||
Sets the color theme. See [the `colors` doc](https://npmjs.org/package/colors) for information on how to write a theme. | ||
# Variables | ||
## `indentationSteps` | ||
```typescript | ||
let indentationSteps: number = 1; | ||
``` | ||
How many spaces to indent each time. | ||
## `maxIndent` | ||
```typescript | ||
let maxIndent: number = 10; | ||
``` | ||
The maximum indentation you can reach. Set to -1 for no limit. | ||
## `msg` | ||
```typescript | ||
interface Messages { | ||
info: string, | ||
warn: string, | ||
error: string, | ||
fatal: string, | ||
catched: string, | ||
success: string, | ||
groupIn: string, | ||
groupOut: string | ||
} | ||
let msg: Messages = { | ||
info: " INFO ", | ||
warn: " WARN ", | ||
error: " ERR ", | ||
fatal: "FATAL ", | ||
catched: "CATCH ", | ||
success: " SUCC ", | ||
groupIn: "> ", | ||
groupOut: "< " | ||
} | ||
``` | ||
The prefixes applied to the specific logging functions. | ||
# License | ||
MIT |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
14754
332
160
1