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

Commit 442274c

Browse files
committed
Use ESM
1 parent d5e64f8 commit 442274c

File tree

6 files changed

+80
-97
lines changed

6 files changed

+80
-97
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-to-hyperscript.js
7-
hast-to-hyperscript.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
hast-to-hyperscript.js
3-
hast-to-hyperscript.min.js
4-
*.json
52
*.md

index.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
'use strict'
2-
3-
var html = require('property-information/html')
4-
var svg = require('property-information/svg')
5-
var find = require('property-information/find')
6-
var hastToReact = require('property-information/hast-to-react.json')
7-
var spaces = require('space-separated-tokens')
8-
var commas = require('comma-separated-tokens')
9-
var style = require('style-to-object')
10-
var ns = require('web-namespaces')
11-
var convert = require('unist-util-is/convert')
1+
import {html, svg, find, hastToReact} from 'property-information'
2+
import {stringify as spaces} from 'space-separated-tokens'
3+
import {stringify as commas} from 'comma-separated-tokens'
4+
import style from 'style-to-object'
5+
import {webNamespaces as ns} from 'web-namespaces'
6+
import {convert} from 'unist-util-is'
7+
8+
var own = {}.hasOwnProperty
129

1310
var root = convert('root')
1411
var element = convert('element')
1512
var text = convert('text')
1613

17-
module.exports = wrapper
18-
19-
function wrapper(h, node, options) {
14+
export function toH(h, node, options) {
2015
var settings = options || {}
2116
var r = react(h)
2217
var v = vue(h)
2318
var vd = vdom(h)
2419
var prefix
2520

2621
if (typeof h !== 'function') {
27-
throw new Error('h is not a function')
22+
throw new TypeError('h is not a function')
2823
}
2924

3025
if (typeof settings === 'string' || typeof settings === 'boolean') {
@@ -50,9 +45,14 @@ function wrapper(h, node, options) {
5045
)
5146
}
5247

53-
return toH(h, node, {
48+
return transform(h, node, {
5449
schema: settings.space === 'svg' ? svg : html,
55-
prefix: prefix == null ? (r || v || vd ? 'h-' : null) : prefix,
50+
prefix:
51+
prefix === undefined || prefix === null
52+
? r || v || vd
53+
? 'h-'
54+
: null
55+
: prefix,
5656
key: 0,
5757
react: r,
5858
vue: v,
@@ -62,7 +62,7 @@ function wrapper(h, node, options) {
6262
}
6363

6464
// Transform a hast node through a hyperscript interface to *anything*!
65-
function toH(h, node, ctx) {
65+
function transform(h, node, ctx) {
6666
var parentSchema = ctx.schema
6767
var schema = parentSchema
6868
var name = node.tagName
@@ -78,7 +78,9 @@ function toH(h, node, ctx) {
7878
}
7979

8080
for (key in node.properties) {
81-
addAttribute(attributes, key, node.properties[key], ctx, name)
81+
if (own.call(node.properties, key)) {
82+
addAttribute(attributes, key, node.properties[key], ctx, name)
83+
}
8284
}
8385

8486
if (ctx.vdom) {
@@ -99,7 +101,7 @@ function toH(h, node, ctx) {
99101
value = node.children[index]
100102

101103
if (element(value)) {
102-
nodes.push(toH(h, value, ctx))
104+
nodes.push(transform(h, value, ctx))
103105
} else if (text(value)) {
104106
nodes.push(value.value)
105107
}
@@ -111,20 +113,22 @@ function toH(h, node, ctx) {
111113

112114
// Ensure no React warnings are triggered for void elements having children
113115
// passed in.
114-
return nodes.length
116+
return nodes.length > 0
115117
? h.call(node, name, attributes, nodes)
116118
: h.call(node, name, attributes)
117119
}
118120

121+
// eslint-disable-next-line complexity, max-params
119122
function addAttribute(props, prop, value, ctx, name) {
120123
var info = find(ctx.schema, prop)
121124
var subprop
122125

123126
// Ignore nullish and `NaN` values.
124127
// Ignore `false` and falsey known booleans for hyperlike DSLs.
125128
if (
126-
value == null ||
127-
value !== value ||
129+
value === undefined ||
130+
value === null ||
131+
(typeof value === 'number' && Number.isNaN(value)) ||
128132
(value === false && (ctx.vue || ctx.vdom || ctx.hyperscript)) ||
129133
(!value && info.boolean && (ctx.vue || ctx.vdom || ctx.hyperscript))
130134
) {
@@ -134,7 +138,7 @@ function addAttribute(props, prop, value, ctx, name) {
134138
if (value && typeof value === 'object' && 'length' in value) {
135139
// Accept `array`.
136140
// Most props are space-separated.
137-
value = (info.commaSeparated ? commas : spaces).stringify(value)
141+
value = info.commaSeparated ? commas(value) : spaces(value)
138142
}
139143

140144
// Treat `true` and truthy known booleans.
@@ -175,7 +179,9 @@ function addAttribute(props, prop, value, ctx, name) {
175179
function react(h) {
176180
var node = h && h('div')
177181
return Boolean(
178-
node && ('_owner' in node || '_store' in node) && node.key == null
182+
node &&
183+
('_owner' in node || '_store' in node) &&
184+
(node.key === undefined || node.key === null)
179185
)
180186
}
181187

package.json

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,58 +32,48 @@
3232
"Koto Hajime <toxictoxer@gmail.com>",
3333
"Christian Murphy <christian.murphy.42@gmail.com>"
3434
],
35+
"sideEffects": false,
36+
"type": "module",
37+
"main": "index.js",
38+
"types": "types/index.d.ts",
3539
"files": [
36-
"index.js",
37-
"types/index.d.ts"
40+
"types/index.d.ts",
41+
"index.js"
3842
],
39-
"types": "types/index.d.ts",
4043
"dependencies": {
4144
"@types/unist": "^2.0.3",
42-
"comma-separated-tokens": "^1.0.0",
43-
"property-information": "^5.3.0",
44-
"space-separated-tokens": "^1.0.0",
45+
"comma-separated-tokens": "^2.0.0",
46+
"property-information": "^6.0.0",
47+
"space-separated-tokens": "^2.0.0",
4548
"style-to-object": "^0.3.0",
46-
"unist-util-is": "^4.0.0",
47-
"web-namespaces": "^1.0.0"
49+
"unist-util-is": "^5.0.0",
50+
"web-namespaces": "^2.0.0"
4851
},
4952
"devDependencies": {
5053
"@types/hyperscript": "0.0.4",
5154
"@types/react": "^17.0.0",
5255
"@types/virtual-dom": "^2.0.0",
53-
"browserify": "^17.0.0",
54-
"dtslint": "^4.0.0",
56+
"c8": "^7.0.0",
5557
"hyperscript": "^2.0.0",
56-
"nyc": "^15.0.0",
5758
"prettier": "^2.0.0",
5859
"react": "^17.0.0",
5960
"react-dom": "^17.0.0",
6061
"rehype": "^11.0.0",
6162
"remark-cli": "^9.0.0",
6263
"remark-preset-wooorm": "^8.0.0",
6364
"tape": "^5.0.0",
64-
"tinyify": "^3.0.0",
65-
"unist-builder": "^2.0.0",
65+
"unist-builder": "^3.0.0",
6666
"vdom-to-html": "^2.0.0",
6767
"virtual-dom": "^2.0.0",
6868
"vue": "^2.0.0",
6969
"vue-server-renderer": "^2.0.0",
70-
"xo": "^0.38.0"
70+
"xo": "^0.39.0"
7171
},
7272
"scripts": {
7373
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
74-
"build-bundle": "browserify index.js -s hastToHyperscript -o hast-to-hyperscript.js",
75-
"build-mangle": "browserify index.js -s hastToHyperscript -o hast-to-hyperscript.min.js -p tinyify",
76-
"build": "npm run build-bundle && npm run build-mangle",
77-
"test-api": "node test",
78-
"test-coverage": "nyc --reporter lcov tape test.js",
79-
"test-types": "dtslint types",
80-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
81-
},
82-
"nyc": {
83-
"check-coverage": true,
84-
"lines": 100,
85-
"functions": 100,
86-
"branches": 100
74+
"test-api": "node test.js",
75+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
76+
"test": "npm run format && npm run test-coverage"
8777
},
8878
"prettier": {
8979
"tabWidth": 2,
@@ -95,24 +85,13 @@
9585
},
9686
"xo": {
9787
"prettier": true,
98-
"esnext": false,
9988
"rules": {
100-
"complexity": "off",
101-
"eqeqeq": [
102-
"error",
103-
"always",
104-
{
105-
"null": "ignore"
106-
}
107-
],
108-
"guard-for-in": "off",
109-
"max-params": "off",
110-
"no-eq-null": "off",
111-
"no-self-compare": "off",
112-
"unicorn/explicit-length-check": "off",
113-
"unicorn/no-fn-reference-in-iterator": "off",
114-
"unicorn/prefer-type-error": "off"
115-
}
89+
"no-var": "off",
90+
"prefer-arrow-callback": "off"
91+
},
92+
"ignore": [
93+
"types/"
94+
]
11695
},
11796
"remarkConfig": {
11897
"plugins": [

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ a [hyperscript][] interface.
1313

1414
## Install
1515

16+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
18+
1619
[npm][]:
1720

1821
```sh
@@ -22,8 +25,8 @@ npm install hast-to-hyperscript
2225
## Use
2326

2427
```js
25-
var toH = require('hast-to-hyperscript')
26-
var h = require('hyperscript')
28+
import {toH} from 'hast-to-hyperscript'
29+
import h from 'hyperscript'
2730

2831
var tree = {
2932
type: 'element',
@@ -55,6 +58,9 @@ Yields:
5558

5659
## API
5760

61+
This package exports the following identifiers: `toH`.
62+
There is no default export.
63+
5864
### `toH(h, tree[, options|prefix])`
5965

6066
Transform a [**hast**][hast] [*tree*][tree] to something else through a

test.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var namespaces = require('web-namespaces')
5-
var u = require('unist-builder')
6-
var h = require('hyperscript')
7-
var v = require('virtual-dom/h')
8-
var vs = require('virtual-dom/virtual-hyperscript/svg')
9-
var r = require('react').createElement
10-
var rehype = require('rehype')
11-
var vToString = require('vdom-to-html')
12-
var rToString = require('react-dom/server').renderToStaticMarkup
13-
var Vue = require('vue')
14-
var VueSSR = require('vue-server-renderer')
15-
var toH = require('.')
1+
import test from 'tape'
2+
import {webNamespaces as ns} from 'web-namespaces'
3+
import {u} from 'unist-builder'
4+
import h from 'hyperscript'
5+
import v from 'virtual-dom/h.js'
6+
import vs from 'virtual-dom/virtual-hyperscript/svg.js'
7+
import rehype from 'rehype'
8+
import vToString from 'vdom-to-html'
9+
import {createElement as r} from 'react'
10+
import {renderToStaticMarkup as rToString} from 'react-dom/server.js'
11+
import Vue from 'vue'
12+
import VueSSR from 'vue-server-renderer'
13+
import {toH} from './index.js'
1614

1715
var processor = rehype().data('settings', {fragment: true, position: false})
1816

@@ -226,7 +224,7 @@ test('hast-to-hyperscript', function (t) {
226224
'svg',
227225
{
228226
key: 'h-5',
229-
namespace: namespaces.svg,
227+
namespace: ns.svg,
230228
attributes: {
231229
xmlns: 'http://www.w3.org/2000/svg',
232230
viewBox: '0 0 500 500'
@@ -235,7 +233,7 @@ test('hast-to-hyperscript', function (t) {
235233
[
236234
vs('circle', {
237235
key: 'h-6',
238-
namespace: namespaces.svg,
236+
namespace: ns.svg,
239237
attributes: {cx: 120, cy: 120, r: 100}
240238
})
241239
]
@@ -617,13 +615,13 @@ test('hast-to-hyperscript', function (t) {
617615

618616
t.equal(
619617
toH(v, u('element', {tagName: 'div'}), {space: 'svg'}).namespace,
620-
namespaces.svg,
618+
ns.svg,
621619
'should support `space: "svg"`'
622620
)
623621

624622
t.equal(
625623
toH(v, u('element', {tagName: 'svg'})).namespace,
626-
namespaces.svg,
624+
ns.svg,
627625
'should infer `space: "svg"`'
628626
)
629627

@@ -753,7 +751,7 @@ function json(value) {
753751

754752
function vueToString(render) {
755753
return VueSSR.createRenderer({template: identity}).renderToString(
756-
new Vue({render: render}).$mount()
754+
new Vue({render}).$mount()
757755
)
758756
}
759757

0 commit comments

Comments
 (0)