-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
103 lines (93 loc) · 2.14 KB
/
logger.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const differ = require("deep-diff")
// https://github.com/flitbit/diff#differences
const dictionary = {
E: {
text: "CHANGED",
},
N: {
text: "ADDED",
},
D: {
text: "DELETED",
},
A: {
text: "ARRAY",
},
}
function withColor(color) {
return `color: ${color}; font-weight: bold`
}
function titleStyle(namespace) {
return [`%c ${namespace}`, withColor("#08b"), "Model"]
}
function render(diff) {
const { kind, path, lhs, rhs, index, item } = diff
const str1 = path && path.join(".")
switch (kind) {
case "E":
return [str1, lhs, "→", rhs]
case "N":
return [str1, rhs]
case "D":
return [str1]
case "A":
return [`${str1}[${index}]`, item]
default:
return []
}
}
function excludeFn(obj) {
if (!obj) return obj
return Object.keys(obj).reduce((result, key) => {
if (typeof obj[key] !== "function") {
result[key] = obj[key]
}
return result
}, {})
}
module.exports = function logger(
originalPrevState,
originalNewState,
namespace,
logger = console,
isCollapsed = false,
) {
const prevState = excludeFn(originalPrevState)
const newState = excludeFn(originalNewState)
const diff = differ(prevState, newState)
try {
if (isCollapsed) {
logger.groupCollapsed(...titleStyle(namespace))
} else {
logger.group(...titleStyle(namespace))
}
} catch (e) {
logger.log(...titleStyle(namespace))
}
logger.log("%c prevState", withColor("#9E9E9E"), prevState)
if (diff) {
const diffObj = {}
diff.forEach(elem => {
const { kind } = elem
const output = render(elem)
diffObj[dictionary[kind].text] = output
.filter(m => m !== null && m !== undefined)
.map(m => {
if (typeof m === "object") {
return JSON.stringify(m)
}
return m
})
.join(" ")
})
logger.log("%c diff", withColor("#ca4000"), diffObj)
} else {
logger.log("%c diff", withColor("#ca4000"), "—— no diff ——")
}
logger.log("%c nextState", withColor("#4CAF50"), newState)
try {
logger.groupEnd()
} catch (e) {
logger.log("—— end —— ")
}
}