Skip to content

Commit 0369e03

Browse files
committed
Refactor to improve size
Inspired by GH-3.
1 parent e53bbc7 commit 0369e03

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

index.js

+16-27
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,17 @@ var hash = '#'.charCodeAt(0);
99

1010
/* Parse a simple CSS selector into a HAST node. */
1111
function parse(selector) {
12-
var id = null;
13-
var className = [];
1412
var value = selector || '';
1513
var name = 'div';
16-
var node;
17-
var type = null;
14+
var props = {};
1815
var index = -1;
19-
var code;
2016
var length = value.length;
17+
var className;
18+
var type;
19+
var code;
2120
var subvalue;
2221
var lastIndex;
2322

24-
node = {
25-
type: 'element',
26-
tagName: null,
27-
properties: {},
28-
children: []
29-
};
30-
31-
type = null;
32-
3323
while (++index <= length) {
3424
code = value.charCodeAt(index);
3525

@@ -38,9 +28,13 @@ function parse(selector) {
3828

3929
if (subvalue) {
4030
if (type === dot) {
41-
className.push(subvalue);
31+
if (className) {
32+
className.push(subvalue);
33+
} else {
34+
className = props.className = [subvalue];
35+
}
4236
} else if (type === hash) {
43-
id = subvalue;
37+
props.id = subvalue;
4438
} else {
4539
name = subvalue;
4640
}
@@ -51,15 +45,10 @@ function parse(selector) {
5145
}
5246
}
5347

54-
node.tagName = name;
55-
56-
if (id) {
57-
node.properties.id = id;
58-
}
59-
60-
if (className.length !== 0) {
61-
node.properties.className = className;
62-
}
63-
64-
return node;
48+
return {
49+
type: 'element',
50+
tagName: name,
51+
properties: props,
52+
children: []
53+
};
6554
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
},
4343
"xo": {
4444
"space": true,
45+
"rules": {
46+
"max-depth": "off"
47+
},
4548
"ignores": [
4649
"hast-util-parse-selector.js"
4750
]

0 commit comments

Comments
 (0)