Skip to content

Commit 53246c3

Browse files
committed
Use ESM
1 parent 0deb85b commit 53246c3

File tree

6 files changed

+62
-77
lines changed

6 files changed

+62
-77
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
'use strict'
1+
import {stringify as commas} from 'comma-separated-tokens'
2+
import {stringify as spaces} from 'space-separated-tokens'
3+
import {html, svg, find} from 'property-information'
4+
import {position} from 'unist-util-position'
5+
import {webNamespaces} from 'web-namespaces'
6+
import {zwitch} from 'zwitch'
27

3-
module.exports = toXast
4-
5-
var comma = require('comma-separated-tokens')
6-
var html = require('property-information/html')
7-
var svg = require('property-information/svg')
8-
var find = require('property-information/find')
9-
var space = require('space-separated-tokens')
10-
var position = require('unist-util-position')
11-
var namespaces = require('web-namespaces')
12-
var xtend = require('xtend')
13-
var zwitch = require('zwitch')
8+
var own = {}.hasOwnProperty
149

1510
var one = zwitch('type', {
16-
handlers: {
17-
root: root,
18-
element: element,
19-
text: text,
20-
comment: comment,
21-
doctype: doctype
22-
},
23-
invalid: invalid,
24-
unknown: unknown
11+
handlers: {root, element, text, comment, doctype},
12+
invalid,
13+
unknown
2514
})
2615

2716
function invalid(value) {
@@ -32,7 +21,7 @@ function unknown(value) {
3221
throw new Error('Cannot transform node of type `' + value.type + '`')
3322
}
3423

35-
function toXast(tree, options) {
24+
export function toXast(tree, options) {
3625
var space = typeof options === 'string' ? options : (options || {}).space
3726
return one(tree, {schema: space === 'svg' ? svg : html, ns: null})
3827
}
@@ -62,6 +51,7 @@ function doctype(node, config) {
6251
)
6352
}
6453

54+
// eslint-disable-next-line complexity
6555
function element(node, parentConfig) {
6656
var props = node.properties || {}
6757
var schema = parentConfig.schema
@@ -71,31 +61,39 @@ function element(node, parentConfig) {
7161
var key
7262
var info
7363

74-
if (props.xmlns === namespaces.html) {
64+
if (props.xmlns === webNamespaces.html) {
7565
schema = html
76-
} else if (props.xmlns === namespaces.svg) {
66+
} else if (props.xmlns === webNamespaces.svg) {
7767
schema = svg
7868
} else if (props.xmlns) {
7969
// We don’t support non-HTML, non-SVG namespaces, so stay in the same.
8070
} else if (schema === html && node.tagName === 'svg') {
8171
schema = svg
8272
}
8373

84-
config = xtend(parentConfig, {schema: schema, ns: namespaces[schema.space]})
74+
config = Object.assign({}, parentConfig, {
75+
schema,
76+
ns: webNamespaces[schema.space]
77+
})
8578

8679
if (parentConfig.ns !== config.ns) {
8780
attributes.xmlns = config.ns
8881
}
8982

9083
for (key in props) {
84+
if (!own.call(props, key)) {
85+
continue
86+
}
87+
9188
info = find(schema, key)
9289
value = props[key]
9390

9491
// Ignore nullish, false, and `NaN` values, and falsey known booleans.
9592
if (
96-
value == null ||
93+
value === undefined ||
94+
value === null ||
9795
value === false ||
98-
value !== value ||
96+
(typeof value === 'number' && Number.isNaN(value)) ||
9997
(!value && info.boolean)
10098
) {
10199
continue
@@ -108,9 +106,7 @@ function element(node, parentConfig) {
108106
// Accept `array`.
109107
// Most props are space-separated.
110108
else if (typeof value === 'object' && 'length' in value) {
111-
value = info.commaSeparated
112-
? comma.stringify(value)
113-
: space.stringify(value)
109+
value = info.commaSeparated ? commas(value) : spaces(value)
114110
}
115111
// Cast everything else to string.
116112
else if (typeof value !== 'string') {
@@ -120,11 +116,7 @@ function element(node, parentConfig) {
120116
attributes[info.attribute] = value
121117
}
122118

123-
return patch(
124-
node,
125-
{type: 'element', name: node.tagName, attributes: attributes},
126-
config
127-
)
119+
return patch(node, {type: 'element', name: node.tagName, attributes}, config)
128120
}
129121

130122
function patch(origin, node, config) {

package.json

+16-25
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,37 @@
2525
"contributors": [
2626
"Titus Wormer <[email protected]> (https://wooorm.com)"
2727
],
28+
"sideEffects": false,
29+
"type": "module",
30+
"main": "index.js",
2831
"files": [
2932
"index.js"
3033
],
3134
"dependencies": {
32-
"comma-separated-tokens": "^1.0.0",
33-
"property-information": "^5.0.0",
34-
"space-separated-tokens": "^1.0.0",
35-
"unist-util-position": "^3.1.0",
36-
"web-namespaces": "^1.0.0",
37-
"xtend": "^4.0.0",
38-
"zwitch": "^1.0.0"
35+
"comma-separated-tokens": "^2.0.0",
36+
"property-information": "^6.0.0",
37+
"space-separated-tokens": "^2.0.0",
38+
"unist-util-position": "^4.0.0",
39+
"web-namespaces": "^2.0.0",
40+
"zwitch": "^2.0.0"
3941
},
4042
"devDependencies": {
43+
"c8": "^7.0.0",
4144
"hastscript": "^6.0.0",
42-
"nyc": "^15.0.0",
4345
"prettier": "^2.0.0",
4446
"remark-cli": "^9.0.0",
4547
"remark-preset-wooorm": "^8.0.0",
4648
"tape": "^5.0.0",
47-
"unist-builder": "^2.0.0",
49+
"unist-builder": "^3.0.0",
4850
"xastscript": "^2.0.0",
49-
"xo": "^0.38.0"
51+
"xo": "^0.39.0"
5052
},
5153
"scripts": {
5254
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
53-
"test-api": "node test",
54-
"test-coverage": "nyc --reporter lcov tape test.js",
55+
"test-api": "node test.js",
56+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
5557
"test": "npm run format && npm run test-coverage"
5658
},
57-
"nyc": {
58-
"check-coverage": true,
59-
"lines": 100,
60-
"functions": 100,
61-
"branches": 100
62-
},
6359
"prettier": {
6460
"tabWidth": 2,
6561
"useTabs": false,
@@ -70,14 +66,9 @@
7066
},
7167
"xo": {
7268
"prettier": true,
73-
"esnext": false,
7469
"rules": {
75-
"eqeqeq": "off",
76-
"guard-for-in": "off",
77-
"no-eq-null": "off",
78-
"no-self-compare": "off",
79-
"unicorn/prefer-number-properties": "off",
80-
"unicorn/prefer-includes": "off"
70+
"no-var": "off",
71+
"prefer-arrow-callback": "off"
8172
}
8273
},
8374
"remarkConfig": {

readme.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
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
@@ -32,11 +35,11 @@ Say we have an `example.html` file, that looks as follows:
3235
…and our script, `example.js`, looks as follows:
3336

3437
```js
35-
var fs = require('fs')
36-
var unified = require('unified')
37-
var parse = require('rehype-parse')
38-
var toXast = require('hast-util-to-xast')
39-
var toXml = require('xast-util-to-xml')
38+
import fs from 'fs'
39+
import unified from 'unified'
40+
import parse from 'rehype-parse'
41+
import {toXast} from 'hast-util-to-xast'
42+
import {toXml} from 'xast-util-to-xml'
4043

4144
// Get the HTML syntax tree:
4245
var hast = unified()
@@ -60,6 +63,9 @@ Yields:
6063

6164
## API
6265

66+
This package exports the following identifiers: `toXast`.
67+
There is no default export.
68+
6369
### `toXast(node[, space|options])`
6470

6571
Transform the given **[hast][]** *[tree][]* to **[xast][]**.

test.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var ns = require('web-namespaces')
5-
var u = require('unist-builder')
6-
var h = require('hastscript')
7-
var s = require('hastscript/svg')
8-
var x = require('xastscript')
9-
var toXast = require('.')
1+
import test from 'tape'
2+
import {webNamespaces as ns} from 'web-namespaces'
3+
import {u} from 'unist-builder'
4+
import h from 'hastscript'
5+
import s from 'hastscript/svg.js'
6+
import x from 'xastscript'
7+
import {toXast} from './index.js'
108

119
test('toXast', function (t) {
1210
t.test('main', function (t) {
@@ -190,7 +188,7 @@ test('toXast', function (t) {
190188
)
191189

192190
t.deepEqual(
193-
toXast(u('element', {tagName: 'br', properties: {prop: NaN}}, [])),
191+
toXast(u('element', {tagName: 'br', properties: {prop: Number.NaN}}, [])),
194192
x('br', {xmlns: ns.html}),
195193
'should support attribute values: `NaN`'
196194
)

0 commit comments

Comments
 (0)