Skip to content

Commit 9d196f1

Browse files
committed
Refactor to improve bundle size
1 parent 0d9be38 commit 9d196f1

File tree

2 files changed

+74
-101
lines changed

2 files changed

+74
-101
lines changed

index.js

Lines changed: 60 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,31 @@ var color = require('./color')
44

55
module.exports = color ? inspect : /* istanbul ignore next */ noColor
66

7-
inspect.color = inspect
8-
noColor.color = inspect
9-
inspect.noColor = noColor
10-
noColor.noColor = noColor
7+
inspect.color = noColor.color = inspect
8+
inspect.noColor = noColor.noColor = noColor
119

1210
var bold = ansiColor(1, 22)
1311
var dim = ansiColor(2, 22)
1412
var yellow = ansiColor(33, 39)
1513
var green = ansiColor(32, 39)
1614

17-
// Define ANSII color removal functionality.
15+
// ANSI color regex.
1816
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
1917

20-
// Standard keys defined by unist (<https://github.com/syntax-tree/unist>) that
21-
// we format differently.
22-
// We don’t ignore `data` though.
23-
// Also includes `name` (from xast) and `tagName` (from `hast`).
24-
var ignore = ['type', 'value', 'children', 'position']
25-
var ignoreString = ['name', 'tagName']
26-
27-
var dataOnly = ['data', 'attributes', 'properties']
28-
2918
// Inspects a node, without using color.
3019
function noColor(node) {
31-
return stripColor(inspect(node))
20+
return inspect(node).replace(colorExpression, '')
3221
}
3322

3423
// Inspects a node.
35-
function inspect(node, options) {
36-
var settings = options || {}
37-
var showPositions = settings.showPositions
38-
39-
if (showPositions === null || showPositions === undefined) {
40-
showPositions = true
41-
}
24+
function inspect(tree, options) {
25+
var positions =
26+
!options || options.showPositions == null ? true : options.showPositions
4227

43-
return inspectValue(node)
28+
return inspectValue(tree)
4429

4530
function inspectValue(node) {
46-
if (node && Boolean(node.length) && typeof node === 'object') {
31+
if (node && typeof node === 'object' && 'length' in node) {
4732
return inspectNodes(node)
4833
}
4934

@@ -59,30 +44,25 @@ function inspect(node, options) {
5944
}
6045

6146
function inspectNodes(nodes) {
62-
var length = nodes.length
63-
var index = -1
6447
var result = []
65-
var node
66-
var tail
67-
var value
68-
69-
while (++index < length) {
70-
node = nodes[index]
71-
tail = index === length - 1
72-
73-
value =
74-
dim((tail ? '└' : '├') + '─' + index) +
75-
' ' +
76-
indent(inspectValue(node), (tail ? ' ' : dim('│')) + ' ', true)
48+
var index = -1
7749

78-
result.push(value)
50+
while (++index < nodes.length) {
51+
result.push(
52+
dim((index < nodes.length - 1 ? '├' : '└') + '─' + index) +
53+
' ' +
54+
indent(
55+
inspectValue(nodes[index]),
56+
(index < nodes.length - 1 ? dim('│') : ' ') + ' ',
57+
true
58+
)
59+
)
7960
}
8061

8162
return result.join('\n')
8263
}
8364

8465
function inspectFields(object) {
85-
var nonEmpty = object.children && object.children.length
8666
var result = []
8767
var key
8868
var value
@@ -93,24 +73,36 @@ function inspect(node, options) {
9373

9474
if (
9575
value === undefined ||
96-
ignore.indexOf(key) !== -1 ||
97-
(ignoreString.indexOf(key) !== -1 && typeof value === 'string')
76+
// Standard keys defined by unist that we format differently.
77+
// <https://github.com/syntax-tree/unist>
78+
key === 'type' ||
79+
key === 'value' ||
80+
key === 'children' ||
81+
key === 'position' ||
82+
// Ignore `name` (from xast) and `tagName` (from `hast`) when string.
83+
(typeof value === 'string' && (key === 'name' || key === 'tagName'))
9884
) {
9985
continue
10086
}
10187

88+
// A single node.
10289
if (
10390
value &&
10491
typeof value === 'object' &&
105-
typeof value.type === 'string' &&
106-
dataOnly.indexOf(key) === -1
92+
value.type &&
93+
key !== 'data' &&
94+
key !== 'attributes' &&
95+
key !== 'properties'
10796
) {
10897
formatted = inspectTree(value)
109-
} else if (
110-
Array.isArray(value) &&
98+
}
99+
// A list of nodes.
100+
else if (
101+
value &&
102+
typeof value === 'object' &&
103+
'length' in value &&
111104
value[0] &&
112-
typeof value[0] === 'object' &&
113-
typeof value[0].type === 'string'
105+
value[0].type
114106
) {
115107
formatted = '\n' + inspectNodes(value)
116108
} else {
@@ -122,7 +114,10 @@ function inspect(node, options) {
122114
)
123115
}
124116

125-
return indent(result.join('\n'), (nonEmpty ? dim('│') : ' ') + ' ')
117+
return indent(
118+
result.join('\n'),
119+
(object.children && object.children.length ? dim('│') : ' ') + ' '
120+
)
126121
}
127122

128123
function inspectTree(node, pad) {
@@ -138,10 +133,7 @@ function inspect(node, options) {
138133
function formatNode(node) {
139134
var result = [bold(node.type)]
140135
var kind = node.tagName || node.name
141-
var position = node.position || {}
142-
var location = showPositions
143-
? stringifyPosition(position.start, position.end)
144-
: ''
136+
var position = positions ? stringifyPosition(node.position) : ''
145137

146138
if (typeof kind === 'string') {
147139
result.push('<', kind, '>')
@@ -153,8 +145,8 @@ function inspect(node, options) {
153145
result.push(' ', green(inspectNonTree(node.value, '')))
154146
}
155147

156-
if (location) {
157-
result.push(' ', dim('('), location, dim(')'))
148+
if (position) {
149+
result.push(' ', dim('('), position, dim(')'))
158150
}
159151

160152
return result.join('')
@@ -164,72 +156,42 @@ function inspect(node, options) {
164156
function indent(value, indentation, ignoreFirst) {
165157
var lines = value.split('\n')
166158
var index = ignoreFirst ? 0 : -1
167-
var length = lines.length
168159

169-
if (value === '') return ''
160+
if (!value) return value
170161

171-
while (++index < length) {
162+
while (++index < lines.length) {
172163
lines[index] = indentation + lines[index]
173164
}
174165

175166
return lines.join('\n')
176167
}
177168

178169
// Compile a position.
179-
function stringifyPosition(start, end) {
170+
function stringifyPosition(value) {
171+
var position = value || {}
180172
var result = []
181173
var positions = []
182174
var offsets = []
183175

184-
add(start)
185-
add(end)
176+
point(position.start)
177+
point(position.end)
186178

187-
if (positions.length > 0) {
188-
result.push(positions.join('-'))
189-
}
190-
191-
if (offsets.length > 0) {
192-
result.push(offsets.join('-'))
193-
}
179+
if (positions.length) result.push(positions.join('-'))
180+
if (offsets.length) result.push(offsets.join('-'))
194181

195182
return result.join(', ')
196183

197-
// Add a position.
198-
function add(position) {
199-
var tuple = stringifyPoint(position)
184+
function point(value) {
185+
if (value) {
186+
positions.push((value.line || 1) + ':' + (value.column || 1))
200187

201-
if (tuple) {
202-
positions.push(tuple[0])
203-
204-
if (tuple[1]) {
205-
offsets.push(tuple[1])
188+
if ('offset' in value) {
189+
offsets.push(String(value.offset || 0))
206190
}
207191
}
208192
}
209193
}
210194

211-
// Compile a point.
212-
function stringifyPoint(value) {
213-
var result = []
214-
215-
if (!value) {
216-
return null
217-
}
218-
219-
result = [[value.line || 1, value.column || 1].join(':')]
220-
221-
if ('offset' in value) {
222-
result.push(String(value.offset || 0))
223-
}
224-
225-
return result
226-
}
227-
228-
// Remove ANSI color from `value`.
229-
function stripColor(value) {
230-
return value.replace(colorExpression, '')
231-
}
232-
233195
// Factory to wrap values in ANSI colours.
234196
function ansiColor(open, close) {
235197
return color

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,21 @@
7878
"prettier": true,
7979
"esnext": false,
8080
"rules": {
81-
"unicorn/prefer-number-properties": "off",
82-
"unicorn/prefer-includes": "off",
81+
"complexity": "off",
82+
"eqeqeq": [
83+
"error",
84+
"always",
85+
{
86+
"null": "ignore"
87+
}
88+
],
8389
"guard-for-in": "off",
84-
"no-control-regex": "off"
90+
"no-control-regex": "off",
91+
"no-eq-null": "off",
92+
"no-multi-assign": "off",
93+
"unicorn/explicit-length-check": "off",
94+
"unicorn/prefer-number-properties": "off",
95+
"unicorn/prefer-includes": "off"
8596
},
8697
"ignore": [
8798
"types",

0 commit comments

Comments
 (0)