Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 5a19ac1

Browse files
committed
Refactor code-style
1 parent 5cdfc05 commit 5a19ac1

File tree

5 files changed

+445
-346
lines changed

5 files changed

+445
-346
lines changed

.prettierignore

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

index.js

Lines changed: 98 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
'use strict';
1+
'use strict'
22

3-
var trim = require('trim');
4-
var paramCase = require('kebab-case');
5-
var information = require('property-information');
6-
var spaces = require('space-separated-tokens');
7-
var commas = require('comma-separated-tokens');
8-
var nan = require('is-nan');
9-
var is = require('unist-util-is');
3+
var trim = require('trim')
4+
var paramCase = require('kebab-case')
5+
var information = require('property-information')
6+
var spaces = require('space-separated-tokens')
7+
var commas = require('comma-separated-tokens')
8+
var nan = require('is-nan')
9+
var is = require('unist-util-is')
1010

11-
module.exports = wrapper;
11+
module.exports = wrapper
1212

1313
function wrapper(h, node, prefix) {
14-
var r;
15-
var v;
14+
var r
15+
var v
1616

1717
if (typeof h !== 'function') {
18-
throw new Error('h is not a function');
18+
throw new Error('h is not a function')
1919
}
2020

21-
r = react(h);
22-
v = vdom(h);
21+
r = react(h)
22+
v = vdom(h)
2323

2424
if (prefix === null || prefix === undefined) {
25-
prefix = r === true || v === true ? 'h-' : false;
25+
prefix = r === true || v === true ? 'h-' : false
2626
}
2727

2828
if (is('root', node)) {
2929
if (node.children.length === 1 && is('element', node.children[0])) {
30-
node = node.children[0];
30+
node = node.children[0]
3131
} else {
3232
node = {
3333
type: 'element',
3434
tagName: 'div',
3535
properties: {},
3636
children: node.children
37-
};
37+
}
3838
}
3939
} else if (!is('element', node)) {
40-
throw new Error('Expected root or element, not `' + ((node && node.type) || node) + '`');
40+
throw new Error(
41+
'Expected root or element, not `' + ((node && node.type) || node) + '`'
42+
)
4143
}
4244

4345
return toH(h, node, {
@@ -46,41 +48,41 @@ function wrapper(h, node, prefix) {
4648
react: r,
4749
vdom: v,
4850
hyperscript: hyperscript(h)
49-
});
51+
})
5052
}
5153

5254
/* Transform a HAST node through a hyperscript interface
5355
* to *anything*! */
5456
function toH(h, node, ctx) {
55-
var selector = node.tagName;
56-
var properties;
57-
var attributes;
58-
var children;
59-
var property;
60-
var elements;
61-
var length;
62-
var index;
63-
var value;
64-
65-
properties = node.properties;
66-
attributes = {};
57+
var selector = node.tagName
58+
var properties
59+
var attributes
60+
var children
61+
var property
62+
var elements
63+
var length
64+
var index
65+
var value
66+
67+
properties = node.properties
68+
attributes = {}
6769

6870
for (property in properties) {
69-
addAttribute(attributes, property, properties[property], ctx);
71+
addAttribute(attributes, property, properties[property], ctx)
7072
}
7173

7274
if (ctx.vdom === true) {
73-
selector = selector.toUpperCase();
75+
selector = selector.toUpperCase()
7476
}
7577

7678
if (ctx.hyperscript === true && attributes.id) {
77-
selector += '#' + attributes.id;
78-
delete attributes.id;
79+
selector += '#' + attributes.id
80+
delete attributes.id
7981
}
8082

8183
if ((ctx.hyperscript === true || ctx.vdom === true) && attributes.className) {
82-
selector += '.' + spaces.parse(attributes.className).join('.');
83-
delete attributes.className;
84+
selector += '.' + spaces.parse(attributes.className).join('.')
85+
delete attributes.className
8486
}
8587

8688
if (typeof attributes.style === 'string') {
@@ -89,46 +91,48 @@ function toH(h, node, ctx) {
8991
* docs/vnode.md#propertiesstyle-vs-propertiesattributesstyle */
9092
if (ctx.vdom === true) {
9193
if (!attributes.attributes) {
92-
attributes.attributes = {};
94+
attributes.attributes = {}
9395
}
9496

95-
attributes.attributes.style = attributes.style;
96-
delete attributes.style;
97-
/* React only accepts `style` as object. */
97+
attributes.attributes.style = attributes.style
98+
delete attributes.style
99+
/* React only accepts `style` as object. */
98100
} else if (ctx.react === true) {
99-
attributes.style = parseStyle(attributes.style);
101+
attributes.style = parseStyle(attributes.style)
100102
}
101103
}
102104

103105
if (ctx.prefix) {
104-
ctx.key++;
105-
attributes.key = ctx.prefix + ctx.key;
106+
ctx.key++
107+
attributes.key = ctx.prefix + ctx.key
106108
}
107109

108-
elements = [];
109-
children = node.children || [];
110-
length = children.length;
111-
index = -1;
110+
elements = []
111+
children = node.children || []
112+
length = children.length
113+
index = -1
112114

113115
while (++index < length) {
114-
value = children[index];
116+
value = children[index]
115117

116118
if (is('element', value)) {
117-
elements.push(toH(h, value, ctx));
119+
elements.push(toH(h, value, ctx))
118120
} else if (is('text', value)) {
119-
elements.push(value.value);
121+
elements.push(value.value)
120122
}
121123
}
122124

123125
/* Ensure no React warnings are triggered for
124126
* void elements having children passed in. */
125-
return elements.length === 0 ? h(selector, attributes) : h(selector, attributes, elements);
127+
return elements.length === 0
128+
? h(selector, attributes)
129+
: h(selector, attributes, elements)
126130
}
127131

128132
/* Add `name` and its `value` to `props`. */
129133
function addAttribute(props, name, value, ctx) {
130-
var info = information(name) || {};
131-
var subprop;
134+
var info = information(name) || {}
135+
var subprop
132136

133137
/* Ignore nully, `false`, `NaN`, and falsey known
134138
* booleans. */
@@ -139,109 +143,112 @@ function addAttribute(props, name, value, ctx) {
139143
nan(value) ||
140144
(info.boolean && !value)
141145
) {
142-
return;
146+
return
143147
}
144148

145149
if (info.name) {
146-
name = info.name;
150+
name = info.name
147151
} else if (ctx.react && !paramCasedReactProp(name)) {
148-
name = camelCase(name);
152+
name = camelCase(name)
149153
} else {
150-
name = paramCase(name);
154+
name = paramCase(name)
151155
}
152156

153157
if (value !== null && typeof value === 'object' && 'length' in value) {
154158
/* Accept `array`. Most props are space-separater. */
155-
value = (info.commaSeparated ? commas : spaces).stringify(value);
159+
value = (info.commaSeparated ? commas : spaces).stringify(value)
156160
}
157161

158162
/* Treat `true` and truthy known booleans. */
159163
if (info.boolean && ctx.hyperscript === true) {
160-
value = '';
164+
value = ''
161165
}
162166

163167
if (info.name !== 'class' && (info.mustUseAttribute || !info.name)) {
164168
if (ctx.vdom === true) {
165-
subprop = 'attributes';
169+
subprop = 'attributes'
166170
} else if (ctx.hyperscript === true) {
167-
subprop = 'attrs';
171+
subprop = 'attrs'
168172
}
169173

170174
if (subprop) {
171175
if (props[subprop] === undefined) {
172-
props[subprop] = {};
176+
props[subprop] = {}
173177
}
174178

175-
props[subprop][name] = value;
179+
props[subprop][name] = value
176180

177-
return;
181+
return
178182
}
179183
}
180184

181-
props[info.propertyName || name] = value;
185+
props[info.propertyName || name] = value
182186
}
183187

184188
/* Check if `h` is `react.createElement`. It doesn’t accept
185189
* `class` as an attribute, it must be added through the
186190
* `selector`. */
187191
function react(h) {
188-
var node = h && h('div');
189-
return Boolean(node && ('_owner' in node || '_store' in node) && node.key === null);
192+
var node = h && h('div')
193+
return Boolean(
194+
node && ('_owner' in node || '_store' in node) && node.key === null
195+
)
190196
}
191197

192198
/* Check if `h` is `hyperscript`. It doesn’t accept
193199
* `class` as an attribute, it must be added through the
194200
* `selector`. */
195201
function hyperscript(h) {
196-
return Boolean(h && h.context && h.cleanup);
202+
return Boolean(h && h.context && h.cleanup)
197203
}
198204

199-
/**
200-
* Check if `h` is `virtual-dom/h`. It’s the only
205+
/* Check if `h` is `virtual-dom/h`. It’s the only
201206
* hyperscript “compatible” interface needing `attributes`. */
202207
function vdom(h) {
203208
try {
204-
return h('div').type === 'VirtualNode';
205-
} catch (err) { /* Empty */ }
209+
return h('div').type === 'VirtualNode'
210+
} catch (err) {
211+
/* Empty */
212+
}
206213

207214
/* istanbul ignore next */
208-
return false;
215+
return false
209216
}
210217

211218
function parseStyle(value) {
212-
var result = {};
213-
var declarations = value.split(';');
214-
var length = declarations.length;
215-
var index = -1;
216-
var declaration;
217-
var prop;
218-
var pos;
219+
var result = {}
220+
var declarations = value.split(';')
221+
var length = declarations.length
222+
var index = -1
223+
var declaration
224+
var prop
225+
var pos
219226

220227
while (++index < length) {
221-
declaration = declarations[index];
222-
pos = declaration.indexOf(':');
228+
declaration = declarations[index]
229+
pos = declaration.indexOf(':')
223230
if (pos !== -1) {
224-
prop = camelCase(trim(declaration.slice(0, pos)));
225-
result[prop] = trim(declaration.slice(pos + 1));
231+
prop = camelCase(trim(declaration.slice(0, pos)))
232+
result[prop] = trim(declaration.slice(pos + 1))
226233
}
227234
}
228235

229-
return result;
236+
return result
230237
}
231238

232239
function paramCasedReactProp(name) {
233-
var head = name.slice(0, 4);
234-
return (head === 'data' || head === 'aria') && name.length > 4;
240+
var head = name.slice(0, 4)
241+
return (head === 'data' || head === 'aria') && name.length > 4
235242
}
236243

237244
function camelCase(val) {
238245
if (val.slice(0, 4) === '-ms-') {
239-
val = 'ms-' + val.slice(4);
246+
val = 'ms-' + val.slice(4)
240247
}
241248

242-
return val.replace(/-([a-z])/g, replace);
249+
return val.replace(/-([a-z])/g, replace)
243250
}
244251

245252
function replace($0, $1) {
246-
return $1.toUpperCase();
253+
return $1.toUpperCase()
247254
}

package.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"esmangle": "^1.0.0",
3939
"hyperscript": "^2.0.2",
4040
"nyc": "^12.0.0",
41+
"prettier": "^1.13.5",
4142
"react": "^16.1.1",
4243
"react-dom": "^16.1.1",
4344
"rehype": "^5.0.0",
@@ -50,26 +51,34 @@
5051
"xo": "^0.21.0"
5152
},
5253
"scripts": {
53-
"build-md": "remark . -qfo",
54+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
5455
"build-bundle": "browserify index.js --bare -s hast-to-hyperscript > hast-to-hyperscript.js",
5556
"build-mangle": "esmangle hast-to-hyperscript.js > hast-to-hyperscript.min.js",
56-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
57-
"lint": "xo",
57+
"build": "npm run build-bundle && npm run build-mangle",
5858
"test-api": "node test",
5959
"test-coverage": "nyc --reporter lcov tape test.js",
60-
"test": "npm run build && npm run lint && npm run test-coverage"
60+
"test": "npm run format && npm run build && npm run test-coverage"
6161
},
6262
"nyc": {
6363
"check-coverage": true,
6464
"lines": 100,
6565
"functions": 100,
6666
"branches": 100
6767
},
68+
"prettier": {
69+
"tabWidth": 2,
70+
"useTabs": false,
71+
"singleQuote": true,
72+
"bracketSpacing": false,
73+
"semi": false,
74+
"trailingComma": "none"
75+
},
6876
"xo": {
69-
"space": true,
77+
"prettier": true,
7078
"esnext": false,
7179
"rules": {
7280
"unicorn/prefer-type-error": "off",
81+
"complexity": "off",
7382
"guard-for-in": "off"
7483
}
7584
},

0 commit comments

Comments
 (0)