Skip to content

Commit aeed3ab

Browse files
committed
Refactor code-style
1 parent 086a91a commit aeed3ab

File tree

5 files changed

+145
-219
lines changed

5 files changed

+145
-219
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,remarkrc,eslintrc,sh}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 53 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,65 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2016 Titus Wormer
4-
* @license MIT
5-
* @module hast:util:parse-selector
6-
* @fileoverview Parse a simple CSS selector to a HAST node.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env commonjs */
12-
13-
/*
14-
* Characters
15-
*/
3+
/* Expose. */
4+
module.exports = parse;
165

17-
var CC_DOT = '.'.charCodeAt(0);
18-
var CC_HASH = '#'.charCodeAt(0);
6+
/* Characters */
7+
var dot = '.'.charCodeAt(0);
8+
var hash = '#'.charCodeAt(0);
199

20-
/**
21-
* Parse a simple CSS selector into a HAST node.
22-
*
23-
* @example
24-
* parse('foo#bar.baz.qux');
25-
* // {
26-
* // 'type': 'element',
27-
* // 'tagName': 'foo',
28-
* // 'properties': {
29-
* // 'id': 'bar',
30-
* // 'className': ['baz', 'qux']
31-
* // },
32-
* // 'children': []
33-
* // }
34-
*
35-
* @param {string?} selector - Simple CSS selector.
36-
* @return {Node} - HAST node.
37-
*/
10+
/* Parse a simple CSS selector into a HAST node. */
3811
function parse(selector) {
39-
var id = null;
40-
var className = [];
41-
var value = selector || '';
42-
var name = 'div';
43-
var node;
44-
var type = null;
45-
var index = -1;
46-
var code;
47-
var length = value.length;
48-
var subvalue;
49-
var lastIndex;
50-
51-
node = {
52-
'type': 'element',
53-
'tagName': null,
54-
'properties': {},
55-
'children': []
56-
};
57-
58-
type = null;
59-
60-
while (++index <= length) {
61-
code = value.charCodeAt(index);
62-
63-
if (!code || code === CC_DOT || code === CC_HASH) {
64-
subvalue = value.slice(lastIndex, index);
65-
66-
if (subvalue) {
67-
if (type === CC_DOT) {
68-
className.push(subvalue);
69-
} else if (type === CC_HASH) {
70-
id = subvalue;
71-
} else {
72-
name = subvalue;
73-
}
74-
}
75-
76-
lastIndex = index + 1;
77-
type = code;
12+
var id = null;
13+
var className = [];
14+
var value = selector || '';
15+
var name = 'div';
16+
var node;
17+
var type = null;
18+
var index = -1;
19+
var code;
20+
var length = value.length;
21+
var subvalue;
22+
var lastIndex;
23+
24+
node = {
25+
type: 'element',
26+
tagName: null,
27+
properties: {},
28+
children: []
29+
};
30+
31+
type = null;
32+
33+
while (++index <= length) {
34+
code = value.charCodeAt(index);
35+
36+
if (!code || code === dot || code === hash) {
37+
subvalue = value.slice(lastIndex, index);
38+
39+
if (subvalue) {
40+
if (type === dot) {
41+
className.push(subvalue);
42+
} else if (type === hash) {
43+
id = subvalue;
44+
} else {
45+
name = subvalue;
7846
}
79-
}
47+
}
8048

81-
node.tagName = name;
82-
83-
if (id) {
84-
node.properties.id = id;
49+
lastIndex = index + 1;
50+
type = code;
8551
}
52+
}
8653

87-
if (className.length) {
88-
node.properties.className = className;
89-
}
54+
node.tagName = name;
9055

91-
return node;
92-
}
56+
if (id) {
57+
node.properties.id = id;
58+
}
9359

94-
/*
95-
* Expose.
96-
*/
60+
if (className.length !== 0) {
61+
node.properties.className = className;
62+
}
9763

98-
module.exports = parse;
64+
return node;
65+
}

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626
"dependencies": {},
2727
"devDependencies": {
2828
"browserify": "^13.0.0",
29-
"eslint": "^2.0.0",
3029
"esmangle": "^1.0.0",
3130
"hast": "0.0.2",
3231
"istanbul": "^0.4.0",
33-
"jscs": "^2.0.0",
34-
"jscs-jsdoc": "^1.0.0",
3532
"remark": "^4.0.0",
3633
"remark-comment-config": "^3.0.0",
3734
"remark-github": "^4.0.1",
@@ -40,18 +37,23 @@
4037
"remark-usage": "^3.0.0",
4138
"remark-validate-links": "^3.0.0",
4239
"remark-yaml-config": "^3.0.0",
43-
"tape": "^4.0.0"
40+
"tape": "^4.0.0",
41+
"xo": "^0.17.0"
4442
},
4543
"scripts": {
4644
"build-md": "remark . --quiet --frail",
4745
"build-bundle": "browserify index.js --bare -s hastUtilParseSelector > hast-util-parse-selector.js",
4846
"build-mangle": "esmangle hast-util-parse-selector.js > hast-util-parse-selector.min.js",
4947
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
50-
"lint-api": "eslint .",
51-
"lint-style": "jscs --reporter inline .",
52-
"lint": "npm run lint-api && npm run lint-style",
48+
"lint": "xo",
5349
"test-api": "node test.js",
5450
"test-coverage": "istanbul cover test.js",
5551
"test": "npm run build && npm run lint && npm run test-coverage"
52+
},
53+
"xo": {
54+
"space": true,
55+
"ignores": [
56+
"hast-util-parse-selector.js"
57+
]
5658
}
5759
}

readme.md

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,19 @@ module, [uncompressed and compressed][releases].
1515

1616
## Usage
1717

18-
Dependencies:
19-
2018
```javascript
2119
var parseSelector = require('hast-util-parse-selector');
22-
```
2320

24-
Parse:
25-
26-
```javascript
27-
var node = parseSelector('.quux#bar.baz.qux');
21+
console.log(parseSelector('.quux#bar.baz.qux'));
2822
```
2923

3024
Yields:
3125

32-
```json
33-
{
34-
"type": "element",
35-
"tagName": "div",
36-
"properties": {
37-
"id": "bar",
38-
"className": [
39-
"quux",
40-
"baz",
41-
"qux"
42-
]
43-
},
44-
"children": []
45-
}
26+
```js
27+
{ type: 'element',
28+
tagName: 'div',
29+
properties: { id: 'bar', className: [ 'quux', 'baz', 'qux' ] },
30+
children: [] }
4631
```
4732

4833
## API

0 commit comments

Comments
 (0)