Skip to content

Commit 24fd84c

Browse files
committed
Split files
1 parent 0599187 commit 24fd84c

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

src/message.js

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
'use strict'
22

3-
const { inspect } = require('util')
4-
5-
const { bold, dim, inverse } = require('chalk')
6-
7-
const { LEVELS } = require('./level')
3+
const { printMultiline, print, prettify } = require('./serialize')
84

95
// Retrieve `message` which sums up all information that can be gathered about
106
// the event.
@@ -31,23 +27,23 @@ const getMessage = function({
3127

3228
const uncaughtException = function({ error }) {
3329
return `An exception was thrown but not caught
34-
${printValue(error)}`
30+
${print(error)}`
3531
}
3632

3733
const warning = function({ error, error: { code, detail = '' } }) {
3834
const codeMessage = code === undefined ? '' : `(${code}) `
3935
return `${codeMessage}${detail}
40-
${printValue(error)}`
36+
${print(error)}`
4137
}
4238

4339
const unhandledRejection = function({ promiseValue }) {
4440
return `A promise was rejected but not handled
45-
Promise was rejected with: ${serialize(promiseValue)}`
41+
Promise was rejected with: ${printMultiline(promiseValue)}`
4642
}
4743

4844
const rejectionHandled = function({ promiseValue }) {
4945
return `A promise was handled after being already rejected
50-
Promise was rejected with: ${serialize(promiseValue)}`
46+
Promise was rejected with: ${printMultiline(promiseValue)}`
5147
}
5248

5349
const multipleResolves = function({
@@ -58,8 +54,8 @@ const multipleResolves = function({
5854
}) {
5955
const again = promiseState === secondPromiseState ? ' again' : ''
6056
return `A promise was resolved/rejected multiple times
61-
Promise was initially ${promiseState} with: ${serialize(promiseValue)}
62-
Promise was then ${secondPromiseState}${again} with: ${serialize(
57+
Promise was initially ${promiseState} with: ${printMultiline(promiseValue)}
58+
Promise was then ${secondPromiseState}${again} with: ${printMultiline(
6359
secondPromiseValue,
6460
)}`
6561
}
@@ -72,36 +68,6 @@ const MESSAGES = {
7268
multipleResolves,
7369
}
7470

75-
const serialize = function(value) {
76-
const valueA = printValue(value)
77-
// Print multiline values on the next line
78-
const valueB = valueA.includes('\n') ? `\n${valueA}` : valueA
79-
return valueB
80-
}
81-
82-
const printValue = function(value) {
83-
if (value instanceof Error) {
84-
return value.stack
85-
}
86-
87-
return inspect(value)
88-
}
89-
90-
const prettify = function({ message, eventName, level }) {
91-
const [header, ...lines] = message.split('\n')
92-
93-
// Add color, icon and `eventName` to first message line.
94-
const { COLOR, SIGN } = LEVELS[level]
95-
const headerA = COLOR(`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`)
96-
// Add gray color and indentation to other lines.
97-
const linesA = lines.map(line => dim(`\t${VERTICAL_BAR} ${line}`))
98-
99-
const messageA = [headerA, ...linesA].join('\n')
100-
return messageA
101-
}
102-
103-
const VERTICAL_BAR = '\u2016'
104-
10571
module.exports = {
10672
getMessage,
10773
}

src/serialize.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const { inspect } = require('util')
4+
5+
const { bold, dim, inverse } = require('chalk')
6+
7+
const { LEVELS } = require('./level')
8+
9+
const printMultiline = function(value) {
10+
const valueA = print(value)
11+
// Print multiline values on the next line
12+
const valueB = valueA.includes('\n') ? `\n${valueA}` : valueA
13+
return valueB
14+
}
15+
16+
const print = function(value) {
17+
if (value instanceof Error) {
18+
return value.stack
19+
}
20+
21+
return inspect(value)
22+
}
23+
24+
const prettify = function({ message, eventName, level }) {
25+
const [header, ...lines] = message.split('\n')
26+
27+
// Add color, icon and `eventName` to first message line.
28+
const { COLOR, SIGN } = LEVELS[level]
29+
const headerA = COLOR(`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`)
30+
// Add gray color and indentation to other lines.
31+
const linesA = lines.map(line => dim(`\t${VERTICAL_BAR} ${line}`))
32+
33+
const messageA = [headerA, ...linesA].join('\n')
34+
return messageA
35+
}
36+
37+
const VERTICAL_BAR = '\u2016'
38+
39+
module.exports = {
40+
printMultiline,
41+
print,
42+
prettify,
43+
}

0 commit comments

Comments
 (0)