-
Notifications
You must be signed in to change notification settings - Fork 0
/
jr.js
52 lines (39 loc) · 1.45 KB
/
jr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const JR = (function() {
const prettyjson = require('prettyjson');
const JR = {
utilities: {}
}; // Custom utilities package
JR.utilities.isUndefined = obj => typeof obj === 'undefined';
JR.utilities.isNull = obj => obj === null;
JR.utilities.isString = str => typeof str === 'string';
JR.utilities.isObject = obj => ((null !== obj) && Object.prototype.toString.call(obj) === '[object Object]');
JR.utilities.isArray = arr => ((null !== arr) && Object.prototype.toString.call(arr) === '[object Array]');
JR.utilities.isNumber = num => typeof num === 'number';
JR.utilities.isBoolean = bool => typeof bool === 'boolean';
const { isNull, isUndefined } = JR.utilities;
const jsonOptions = {
keysColor: 'cyan',
dashColor: 'cyan',
stringColor: 'yellow',
// noColor: true
};
function markStrings(obj) {
if (JR.utilities.isString(obj)) {
return `"${obj}"`;
} else if (JR.utilities.isArray(obj)) {
return Array.prototype.map.call(obj, markStrings);
} else if (JR.utilities.isObject(obj)) {
for (let prop in obj) {
obj[prop] = markStrings(obj[prop]);
}
}
return obj;
}
JR.log = (...args) => {
console.log.apply(null, args.map(arg => prettyjson.render(isUndefined(arg) ? 'undefined' : isNull(arg) ? 'null' : markStrings(arg), jsonOptions)));
};
JR.sectionLog = name => console.log(`\n${prettyjson.render(`========== ${name} ==========`, { stringColor: 'magenta' })}`);
return JR;
})();
module.exports = JR;