Skip to content

Commit 2c43757

Browse files
committed
Refactor code-style
1 parent 4412cf6 commit 2c43757

File tree

5 files changed

+194
-182
lines changed

5 files changed

+194
-182
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
hastscript.js
3+
hastscript.min.js

index.js

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,180 @@
1-
'use strict';
1+
'use strict'
22

3-
var parseSelector = require('hast-util-parse-selector');
4-
var camelcase = require('camelcase');
5-
var propertyInformation = require('property-information');
6-
var spaces = require('space-separated-tokens').parse;
7-
var commas = require('comma-separated-tokens').parse;
3+
var parseSelector = require('hast-util-parse-selector')
4+
var camelcase = require('camelcase')
5+
var propertyInformation = require('property-information')
6+
var spaces = require('space-separated-tokens').parse
7+
var commas = require('comma-separated-tokens').parse
88

9-
module.exports = h;
9+
module.exports = h
1010

1111
/* Hyperscript compatible DSL for creating virtual HAST
1212
* trees. */
1313
function h(selector, properties, children) {
14-
var node = parseSelector(selector);
15-
var property;
14+
var node = parseSelector(selector)
15+
var property
1616

1717
if (
1818
properties &&
1919
!children &&
20-
(
21-
typeof properties === 'string' ||
20+
(typeof properties === 'string' ||
2221
'length' in properties ||
23-
isNode(node.tagName, properties)
24-
)
22+
isNode(node.tagName, properties))
2523
) {
26-
children = properties;
27-
properties = null;
24+
children = properties
25+
properties = null
2826
}
2927

3028
if (properties) {
3129
for (property in properties) {
32-
addProperty(node.properties, property, properties[property]);
30+
addProperty(node.properties, property, properties[property])
3331
}
3432
}
3533

36-
addChild(node.children, children);
34+
addChild(node.children, children)
3735

3836
if (node.tagName === 'template') {
39-
node.content = {type: 'root', children: node.children};
40-
node.children = [];
37+
node.content = {type: 'root', children: node.children}
38+
node.children = []
4139
}
4240

43-
return node;
41+
return node
4442
}
4543

4644
/* Check if `value` is a valid child node of `tagName`. */
4745
function isNode(tagName, value) {
48-
var type = value.type;
46+
var type = value.type
4947

5048
if (typeof type === 'string') {
51-
type = type.toLowerCase();
49+
type = type.toLowerCase()
5250
}
5351

5452
if (tagName === 'input' || !type || typeof type !== 'string') {
55-
return false;
53+
return false
5654
}
5755

5856
if (typeof value.children === 'object' && 'length' in value.children) {
59-
return true;
57+
return true
6058
}
6159

6260
if (tagName === 'button') {
63-
return type !== 'menu' &&
61+
return (
62+
type !== 'menu' &&
6463
type !== 'submit' &&
6564
type !== 'reset' &&
66-
type !== 'button';
65+
type !== 'button'
66+
)
6767
}
6868

69-
return 'value' in value;
69+
return 'value' in value
7070
}
7171

7272
/* Add `value` as a child to `nodes`. */
7373
function addChild(nodes, value) {
74-
var index;
75-
var length;
74+
var index
75+
var length
7676

7777
if (value === null || value === undefined) {
78-
return;
78+
return
7979
}
8080

8181
if (typeof value === 'string' || typeof value === 'number') {
82-
value = {type: 'text', value: String(value)};
82+
value = {type: 'text', value: String(value)}
8383
}
8484

8585
if (typeof value === 'object' && 'length' in value) {
86-
index = -1;
87-
length = value.length;
86+
index = -1
87+
length = value.length
8888

8989
while (++index < length) {
90-
addChild(nodes, value[index]);
90+
addChild(nodes, value[index])
9191
}
9292

93-
return;
93+
return
9494
}
9595

9696
if (typeof value !== 'object' || !('type' in value)) {
97-
throw new Error('Expected node, nodes, or string, got `' + value + '`');
97+
throw new Error('Expected node, nodes, or string, got `' + value + '`')
9898
}
9999

100-
nodes.push(value);
100+
nodes.push(value)
101101
}
102102

103103
/* Add `name` and its `value` to `properties`. `properties` can
104104
* be prefilled by `parseSelector`: it can have `id` and `className`
105105
* properties. */
106106
function addProperty(properties, name, value) {
107-
var info = propertyInformation(name) || {};
108-
var result = value;
109-
var key;
107+
var info = propertyInformation(name) || {}
108+
var result = value
109+
var key
110110

111111
/* Ignore nully and NaN values. */
112112
if (value === null || value === undefined || value !== value) {
113-
return;
113+
return
114114
}
115115

116116
/* Handle values. */
117117
if (name === 'style') {
118118
/* Accept `object`. */
119119
if (typeof value !== 'string') {
120-
result = [];
120+
result = []
121121

122122
for (key in value) {
123-
result.push([key, value[key]].join(': '));
123+
result.push([key, value[key]].join(': '))
124124
}
125125

126-
result = result.join('; ');
126+
result = result.join('; ')
127127
}
128128
} else if (info.spaceSeparated) {
129129
/* Accept both `string` and `Array`. */
130-
result = typeof value === 'string' ? spaces(result) : result;
130+
result = typeof value === 'string' ? spaces(result) : result
131131

132132
/* Class-names (which can be added both on
133133
* the `selector` and here). */
134134
if (name === 'class' && properties.className) {
135-
result = properties.className.concat(result);
135+
result = properties.className.concat(result)
136136
}
137137
} else if (info.commaSeparated) {
138138
/* Accept both `string` and `Array`. */
139-
result = typeof value === 'string' ? commas(result) : result;
139+
result = typeof value === 'string' ? commas(result) : result
140140
}
141141

142-
result = parsePrimitive(info, name, result);
142+
result = parsePrimitive(info, name, result)
143143

144-
properties[info.propertyName || camelcase(name)] = result;
144+
properties[info.propertyName || camelcase(name)] = result
145145
}
146146

147147
/* Parse a (list of) primitives. */
148148
function parsePrimitive(info, name, value) {
149-
var result = value;
150-
var index;
151-
var length;
149+
var result = value
150+
var index
151+
var length
152152

153153
if (typeof value === 'object' && 'length' in value) {
154-
length = value.length;
155-
index = -1;
156-
result = [];
154+
length = value.length
155+
index = -1
156+
result = []
157157

158158
while (++index < length) {
159-
result[index] = parsePrimitive(info, name, value[index]);
159+
result[index] = parsePrimitive(info, name, value[index])
160160
}
161161

162-
return result;
162+
return result
163163
}
164164

165165
if (info.numeric || info.positiveNumeric) {
166166
if (!isNaN(result) && result !== '') {
167-
result = Number(result);
167+
result = Number(result)
168168
}
169169
} else if (info.boolean || info.overloadedBoolean) {
170170
/* Accept `boolean` and `string`. */
171171
if (
172172
typeof result === 'string' &&
173173
(result === '' || value.toLowerCase() === name)
174174
) {
175-
result = true;
175+
result = true
176176
}
177177
}
178178

179-
return result;
179+
return result
180180
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,37 @@
3333
"browserify": "^16.0.0",
3434
"esmangle": "^1.0.0",
3535
"nyc": "^12.0.0",
36+
"prettier": "^1.13.5",
3637
"remark-cli": "^5.0.0",
3738
"remark-preset-wooorm": "^4.0.0",
3839
"tape": "^4.0.0",
3940
"xo": "^0.21.0"
4041
},
4142
"scripts": {
42-
"build-md": "remark . -qfo",
43+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
4344
"build-bundle": "browserify index.js --bare -s hastscript > hastscript.js",
4445
"build-mangle": "esmangle hastscript.js > hastscript.min.js",
45-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
46-
"lint": "xo",
46+
"build": "npm run build-bundle && npm run build-mangle",
4747
"test-api": "node test",
4848
"test-coverage": "nyc --reporter lcov tape test.js",
49-
"test": "npm run build && npm run lint && npm run test-coverage"
49+
"test": "npm run format && npm run build && npm run test-coverage"
5050
},
5151
"nyc": {
5252
"check-coverage": true,
5353
"lines": 100,
5454
"functions": 100,
5555
"branches": 100
5656
},
57+
"prettier": {
58+
"tabWidth": 2,
59+
"useTabs": false,
60+
"singleQuote": true,
61+
"bracketSpacing": false,
62+
"semi": false,
63+
"trailingComma": "none"
64+
},
5765
"xo": {
58-
"space": true,
66+
"prettier": true,
5967
"esnext": false,
6068
"rules": {
6169
"no-self-compare": "off",

readme.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ npm install hastscript
1414
## Usage
1515

1616
```javascript
17-
var h = require('hastscript');
17+
var h = require('hastscript')
1818

1919
var tree = h('.foo#some-id', [
2020
h('span', 'some text'),
2121
h('input', {type: 'text', value: 'foo'}),
22-
h('a.alpha', {
23-
class: 'bravo charlie',
24-
download: 'download'
25-
}, ['delta', 'echo'])
26-
]);
22+
h(
23+
'a.alpha',
24+
{
25+
class: 'bravo charlie',
26+
download: 'download'
27+
},
28+
['delta', 'echo']
29+
)
30+
])
31+
32+
console.log(tree)
2733
```
2834

2935
Yields:

0 commit comments

Comments
 (0)