Skip to content

Commit 9eb35fa

Browse files
committed
Use ESM
1 parent 42638e2 commit 9eb35fa

27 files changed

+192
-244
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
'use strict'
2-
module.exports = require('./lib')
1+
export {toXml} from './lib/index.js'

lib/all.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict'
2-
3-
var one = require('./one')
4-
5-
module.exports = all
1+
import {one} from './one.js'
62

73
// Serialize all children of `parent`.
8-
function all(parent, config) {
4+
export function all(parent, config) {
95
var children = (parent && parent.children) || []
106
var index = -1
117
var results = []

lib/cdata.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
'use strict'
2-
3-
var escape = require('./util-escape')
4-
5-
module.exports = serializeCdataSection
1+
import {escape} from './util-escape.js'
62

73
var unsafe = /]]>/g
84
var subset = ['>']
95

106
// Serialize a CDATA section.
11-
function serializeCdataSection(node) {
7+
export function cdata(node) {
128
return '<![CDATA[' + escape(node.value, subset, unsafe) + ']]>'
139
}

lib/comment.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
'use strict'
2-
3-
var escape = require('./util-escape')
4-
5-
module.exports = serializeComment
1+
import {escape} from './util-escape.js'
62

73
// Serialize a comment.
8-
function serializeComment(node) {
4+
export function comment(node) {
95
return '<!--' + escape(node.value, ['-']) + '-->'
106
}

lib/doctype.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
'use strict'
2-
3-
var name = require('./name')
4-
var value = require('./value')
5-
6-
module.exports = serializeDocumentType
1+
import {name} from './name.js'
2+
import {value} from './value.js'
73

84
// Serialize a document type.
9-
function serializeDocumentType(node, config) {
5+
export function doctype(node, config) {
106
var nodeName = name(node.name)
117
var pub = node.public
128
var sys = node.system

lib/element.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict'
1+
import {all} from './all.js'
2+
import {name} from './name.js'
3+
import {value} from './value.js'
24

3-
var all = require('./all')
4-
var name = require('./name')
5-
var value = require('./value')
6-
7-
module.exports = serializeElement
5+
var own = {}.hasOwnProperty
86

97
// Serialize an element.
10-
function serializeElement(node, config) {
8+
export function element(node, config) {
119
var nodeName = name(node.name)
1210
var content = all(node, config)
1311
var attributes = node.attributes || {}
@@ -17,10 +15,12 @@ function serializeElement(node, config) {
1715
var result
1816

1917
for (key in attributes) {
20-
result = attributes[key]
18+
if (own.call(attributes, key)) {
19+
result = attributes[key]
2120

22-
if (result !== null && result !== undefined) {
23-
attrs.push(name(key) + '=' + value(result, config))
21+
if (result !== null && result !== undefined) {
22+
attrs.push(name(key) + '=' + value(result, config))
23+
}
2424
}
2525
}
2626

lib/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
'use strict'
2-
3-
var one = require('./one')
4-
5-
module.exports = toXml
1+
import {one} from './one.js'
62

73
var quotationMark = '"'
84
var apostrophe = "'"
95

106
// Serialize the given node.
11-
function toXml(node, options) {
7+
export function toXml(node, options) {
128
var settings = options || {}
139
var quote = settings.quote || quotationMark
1410
var alternative = quote === quotationMark ? apostrophe : quotationMark
@@ -34,7 +30,7 @@ function toXml(node, options) {
3430
dangerous: settings.allowDangerousXml,
3531
close: settings.closeEmptyElements,
3632
tight: settings.tightClose,
37-
quote: quote,
33+
quote,
3834
alternative: smart ? alternative : null
3935
})
4036
}

lib/instruction.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
'use strict'
2-
3-
var name = require('./name')
4-
var escape = require('./util-escape')
5-
6-
module.exports = serializeProcessingInstruction
1+
import {escape} from './util-escape.js'
2+
import {name} from './name.js'
73

84
var unsafe = /\?>/g
95
var subset = ['>']
106

117
// Serialize a processing instruction.
12-
function serializeProcessingInstruction(node) {
8+
export function instruction(node) {
139
var nodeName = name(node.name) || 'x'
1410
var result = escape(node.value, subset, unsafe)
1511
return '<?' + nodeName + (result ? ' ' + result : '') + '?>'

lib/name.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict'
2-
3-
var escape = require('./util-escape')
4-
5-
module.exports = serializeName
1+
import {escape} from './util-escape.js'
62

73
var subset = ['\t', '\n', ' ', '"', '&', "'", '/', '<', '=', '>']
84

9-
function serializeName(value) {
5+
export function name(value) {
106
return escape(value, subset)
117
}

lib/one.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
'use strict'
2-
3-
module.exports = one
1+
import {all} from './all.js'
2+
import {element} from './element.js'
3+
import {text} from './text.js'
4+
import {comment} from './comment.js'
5+
import {doctype} from './doctype.js'
6+
import {instruction} from './instruction.js'
7+
import {cdata} from './cdata.js'
8+
import {raw} from './raw.js'
49

510
var own = {}.hasOwnProperty
611

712
var handlers = {}
813

9-
handlers.root = require('./all')
10-
handlers.element = require('./element')
11-
handlers.text = require('./text')
12-
handlers.comment = require('./comment')
13-
handlers.doctype = require('./doctype')
14-
handlers.instruction = require('./instruction')
15-
handlers.cdata = require('./cdata')
16-
handlers.raw = require('./raw')
14+
handlers.root = all
15+
handlers.element = element
16+
handlers.text = text
17+
handlers.comment = comment
18+
handlers.doctype = doctype
19+
handlers.instruction = instruction
20+
handlers.cdata = cdata
21+
handlers.raw = raw
1722

1823
// Serialize one node.
19-
function one(node, config) {
24+
export function one(node, config) {
2025
var type = node && node.type
2126

2227
if (!type) {

lib/raw.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
'use strict'
2-
3-
var text = require('./text')
4-
5-
module.exports = serializeRaw
1+
import {text} from './text.js'
62

73
// Serialize raw.
8-
function serializeRaw(node, config) {
4+
export function raw(node, config) {
95
return config.dangerous ? node.value : text(node, config)
106
}

lib/text.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
'use strict'
2-
3-
var escape = require('./util-escape')
4-
5-
module.exports = serializeText
1+
import {escape} from './util-escape.js'
62

73
var subset = ['&', '<']
84

95
// Serialize a text.
10-
function serializeText(node) {
6+
export function text(node) {
117
return escape(node.value, subset)
128
}

lib/util-escape.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
'use strict'
2-
3-
var entities = require('stringify-entities/light')
4-
5-
module.exports = escape
1+
import {stringifyEntitiesLight} from 'stringify-entities'
62

73
var noncharacter = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g
84

9-
function escape(value, subset, unsafe) {
5+
export function escape(value, subset, unsafe) {
106
var result = clean(value)
117

128
return unsafe ? result.replace(unsafe, encode) : encode(result)
139

1410
function encode($0) {
15-
return entities($0, {subset: subset})
11+
return stringifyEntitiesLight($0, {subset})
1612
}
1713
}
1814

lib/value.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict'
1+
import {ccount} from 'ccount'
2+
import {escape} from './util-escape.js'
23

3-
var ccount = require('ccount')
4-
var escape = require('./util-escape')
5-
6-
module.exports = serializeValue
7-
8-
function serializeValue(value, config) {
4+
export function value(value, config) {
95
var primary = config.quote
106
var secondary = config.alternative
117
var result = String(value)

package.json

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,35 @@
2121
"contributors": [
2222
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2323
],
24+
"sideEffects": false,
25+
"type": "module",
26+
"main": "index.js",
27+
"types": "types/",
2428
"files": [
25-
"lib",
26-
"index.js",
27-
"types/index.d.ts"
29+
"lib/",
30+
"types/index.d.ts",
31+
"index.js"
2832
],
29-
"types": "types",
3033
"dependencies": {
3134
"@types/xast": "^1.0.0",
32-
"ccount": "^1.0.0",
33-
"stringify-entities": "^3.1.0"
35+
"ccount": "^2.0.0",
36+
"stringify-entities": "^4.0.0"
3437
},
3538
"devDependencies": {
36-
"dtslint": "^4.0.0",
37-
"nyc": "^15.0.0",
39+
"c8": "^7.0.0",
3840
"prettier": "^2.0.0",
3941
"remark-cli": "^9.0.0",
4042
"remark-preset-wooorm": "^8.0.0",
4143
"tape": "^5.0.0",
42-
"unist-builder": "^2.0.0",
44+
"unist-builder": "^3.0.0",
4345
"xastscript": "^2.0.0",
44-
"xo": "^0.38.0"
46+
"xo": "^0.39.0"
4547
},
4648
"scripts": {
4749
"format": "remark . -qfo && prettier . --write --loglevel warn && xo --fix",
48-
"test-api": "node test",
49-
"test-coverage": "nyc --reporter lcov tape test/index.js",
50-
"test-types": "dtslint types",
51-
"test": "npm run format && npm run test-coverage && npm run test-types"
50+
"test-api": "node test/index.js",
51+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
52+
"test": "npm run format && npm run test-coverage"
5253
},
5354
"prettier": {
5455
"tabWidth": 2,
@@ -60,19 +61,14 @@
6061
},
6162
"xo": {
6263
"prettier": true,
63-
"esnext": false,
6464
"rules": {
65-
"unicorn/prefer-includes": "off",
66-
"unicorn/prefer-number-properties": "off",
67-
"guard-for-in": "off",
68-
"no-control-regex": "off"
69-
}
70-
},
71-
"nyc": {
72-
"check-coverage": true,
73-
"lines": 100,
74-
"functions": 100,
75-
"branches": 100
65+
"no-control-regex": "off",
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
68+
},
69+
"ignore": [
70+
"types/"
71+
]
7672
},
7773
"remarkConfig": {
7874
"plugins": [

readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

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

1720
```sh
@@ -21,9 +24,9 @@ npm install xast-util-to-xml
2124
## Use
2225

2326
```js
24-
var u = require('unist-builder')
25-
var x = require('xastscript')
26-
var toXml = require('xast-util-to-xml')
27+
import {u} from 'unist-builder'
28+
import {x} from 'xastscript'
29+
import {toXml} from 'xast-util-to-xml'
2730

2831
var tree = u('root', [
2932
u('instruction', {name: 'xml'}, 'version="1.0" encoding="utf-8"'),
@@ -61,6 +64,9 @@ Yields:
6164

6265
## API
6366

67+
This package exports the following identifiers: `toXml`.
68+
There is no default export.
69+
6470
### `toXml(tree[, options])`
6571

6672
Serialize the given **[xast][]** *[tree][]* (or list of nodes).

0 commit comments

Comments
 (0)