Skip to content

Commit 7478199

Browse files
committed
Refactor to improve bundle size
1 parent 3b8253e commit 7478199

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

index.js

+31-40
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,56 @@
22

33
module.exports = x
44

5-
var slice = [].slice
6-
75
// Creating xast elements.
86
function x(name, attributes) {
9-
var attrs = {}
10-
var childrenIndex = 2
11-
var attribute
12-
var value
13-
var node
7+
var node = {type: 'element', name: name, attributes: {}, children: []}
8+
var index = 1
9+
var key
1410

15-
if (typeof name !== 'string' || name === '') {
11+
if (typeof name !== 'string' || !name) {
1612
throw new Error('Expected element name, got `' + name + '`')
1713
}
1814

19-
node = {type: 'element', name: name, attributes: attrs, children: []}
20-
21-
// Note that we do not accept a node instead of attributes.
22-
if (
23-
typeof attributes === 'number' ||
24-
typeof attributes === 'string' ||
25-
(attributes && 'length' in attributes)
26-
) {
27-
childrenIndex = 1
28-
attributes = null
29-
}
30-
31-
if (attributes !== null && attributes !== undefined) {
32-
for (attribute in attributes) {
33-
value = attributes[attribute]
34-
35-
// Ignore nullish and NaN values.
36-
if (value !== null && value !== undefined && value === value) {
37-
attrs[attribute] = String(value)
15+
// Handle props.
16+
if (attributes) {
17+
if (
18+
typeof attributes === 'string' ||
19+
typeof attributes === 'number' ||
20+
'length' in attributes
21+
) {
22+
// Nope, it’s something for `children`.
23+
index--
24+
} else {
25+
for (key in attributes) {
26+
// Ignore nullish and NaN values.
27+
if (attributes[key] != null && attributes[key] === attributes[key]) {
28+
node.attributes[key] = String(attributes[key])
29+
}
3830
}
3931
}
4032
}
4133

42-
add(node.children, slice.call(arguments, childrenIndex))
34+
// Handle children.
35+
while (++index < arguments.length) {
36+
addChild(node.children, arguments[index])
37+
}
4338

4439
return node
4540
}
4641

47-
function add(siblings, value) {
48-
var index
49-
var length
42+
function addChild(nodes, value) {
43+
var index = -1
5044

51-
if (value === null || value === undefined) {
45+
if (value == null) {
5246
// Empty.
5347
} else if (typeof value === 'string' || typeof value === 'number') {
54-
siblings.push({type: 'text', value: String(value)})
48+
nodes.push({type: 'text', value: String(value)})
5549
} else if (typeof value === 'object' && 'length' in value) {
56-
index = -1
57-
length = value.length
58-
59-
while (++index < length) {
60-
add(siblings, value[index])
50+
while (++index < value.length) {
51+
addChild(nodes, value[index])
6152
}
62-
} else if (typeof value === 'object' && typeof value.type === 'string') {
63-
siblings.push(value)
53+
} else if (typeof value === 'object' && value.type) {
54+
nodes.push(value)
6455
} else {
6556
throw new TypeError('Expected node, nodes, string, got `' + value + '`')
6657
}

package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@
6767
"prettier": true,
6868
"esnext": false,
6969
"rules": {
70-
"unicorn/prefer-number-properties": "off",
70+
"eqeqeq": [
71+
"error",
72+
"always",
73+
{
74+
"null": "ignore"
75+
}
76+
],
77+
"guard-for-in": "off",
78+
"no-eq-null": "off",
7179
"no-self-compare": "off",
72-
"guard-for-in": "off"
80+
"unicorn/prefer-number-properties": "off"
7381
}
7482
},
7583
"remarkConfig": {

0 commit comments

Comments
 (0)