Skip to content

Commit b2e8291

Browse files
committed
Use ESM
1 parent cee53f3 commit b2e8291

File tree

6 files changed

+37
-62
lines changed

6 files changed

+37
-62
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-
nlcst-is-literal.js
7-
nlcst-is-literal.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-
nlcst-is-literal.js
3-
nlcst-is-literal.min.js
4-
*.json
52
*.md

index.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
'use strict'
2-
3-
var toString = require('nlcst-to-string')
4-
5-
module.exports = isLiteral
1+
import {toString} from 'nlcst-to-string'
62

73
var single = [
84
'-', // Hyphen-minus
@@ -39,16 +35,11 @@ var pairs = {
3935
'「': ['」']
4036
}
4137

42-
var open = []
43-
var key
44-
45-
for (key in pairs) {
46-
open.push(key)
47-
}
38+
var open = Object.keys(pairs)
4839

4940
// Check if the node in `parent` at `position` is enclosed by matching
5041
// delimiters.
51-
function isLiteral(parent, index) {
42+
export function isLiteral(parent, index) {
5243
if (!(parent && parent.children)) {
5344
throw new Error('Parent must be a node')
5445
}
@@ -61,8 +52,8 @@ function isLiteral(parent, index) {
6152
}
6253
}
6354

64-
if (isNaN(index)) {
65-
throw new Error('Index must be a number')
55+
if (typeof index !== 'number' || Number.isNaN(index)) {
56+
throw new TypeError('Index must be a number')
6657
}
6758

6859
return Boolean(
@@ -98,7 +89,7 @@ function siblingDelimiter(parent, position, step, delimiters) {
9889
}
9990

10091
if (sibling.type !== 'WhiteSpaceNode') {
101-
return delimiters.indexOf(toString(sibling)) > -1 && sibling
92+
return delimiters.includes(toString(sibling)) && sibling
10293
}
10394

10495
index += step

package.json

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,30 @@
2222
"contributors": [
2323
"Titus Wormer <[email protected]> (https://wooorm.com)"
2424
],
25+
"sideEffects": false,
26+
"type": "module",
27+
"main": "index.js",
2528
"files": [
2629
"index.js"
2730
],
2831
"dependencies": {
29-
"nlcst-to-string": "^2.0.0"
32+
"nlcst-to-string": "^3.0.0"
3033
},
3134
"devDependencies": {
32-
"browserify": "^17.0.0",
33-
"nyc": "^15.0.0",
35+
"c8": "^7.0.0",
3436
"prettier": "^2.0.0",
3537
"remark-cli": "^9.0.0",
3638
"remark-preset-wooorm": "^8.0.0",
3739
"retext": "^7.0.0",
3840
"tape": "^5.0.0",
39-
"tinyify": "^3.0.0",
40-
"unist-util-visit": "^2.0.0",
41-
"xo": "^0.38.0"
41+
"unist-util-visit": "^3.0.0",
42+
"xo": "^0.39.0"
4243
},
4344
"scripts": {
4445
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
45-
"build-bundle": "browserify . -s nlcstIsLiteral -o nlcst-is-literal.js",
46-
"build-mangle": "browserify . -s nlcstIsLiteral -o nlcst-is-literal.min.js -p tinyify",
47-
"build": "npm run build-bundle && npm run build-mangle",
48-
"test-api": "node test",
49-
"test-coverage": "nyc --reporter lcov tape test.js",
50-
"test": "npm run format && npm run build && npm run test-coverage"
51-
},
52-
"nyc": {
53-
"check-coverage": true,
54-
"lines": 100,
55-
"functions": 100,
56-
"branches": 100
46+
"test-api": "node test.js",
47+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
48+
"test": "npm run format && npm run test-coverage"
5749
},
5850
"prettier": {
5951
"tabWidth": 2,
@@ -65,16 +57,10 @@
6557
},
6658
"xo": {
6759
"prettier": true,
68-
"esnext": false,
6960
"rules": {
70-
"guard-for-in": "off",
71-
"unicorn/prefer-includes": "off",
72-
"unicorn/prefer-number-properties": "off",
73-
"unicorn/prefer-type-error": "off"
74-
},
75-
"ignore": [
76-
"nlcst-is-literal.js"
77-
]
61+
"no-var": "off",
62+
"prefer-arrow-callback": "off"
63+
}
7864
},
7965
"remarkConfig": {
8066
"plugins": [

readme.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ about “monsieur”.
1616

1717
## Install
1818

19+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
20+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
21+
1922
[npm][]:
2023

2124
```sh
@@ -41,12 +44,12 @@ The word — quux — is meant as a literal.
4144
And our script, `example.js`, looks as follows:
4245

4346
```js
44-
var vfile = require('to-vfile')
45-
var unified = require('unified')
46-
var english = require('retext-english')
47-
var visit = require('unist-util-visit')
48-
var toString = require('nlcst-to-string')
49-
var literal = require('nlcst-is-literal')
47+
import vfile from 'to-vfile'
48+
import unified from 'unified'
49+
import english from 'retext-english'
50+
import {visit} from 'unist-util-visit'
51+
import {toString} from 'nlcst-to-string'
52+
import {isLiteral} from 'nlcst-is-literal'
5053

5154
var file = vfile.readSync('example.txt')
5255

@@ -57,7 +60,7 @@ var tree = unified()
5760
visit(tree, 'WordNode', visitor)
5861

5962
function visitor(node, index, parent) {
60-
if (literal(parent, index)) {
63+
if (isLiteral(parent, index)) {
6164
console.log(toString(node))
6265
}
6366
}
@@ -75,6 +78,9 @@ quux
7578

7679
## API
7780

81+
This package exports the following identifiers: `isLiteral`.
82+
There is no default export.
83+
7884
### `isLiteral(parent, index|child)`
7985

8086
Check if the `child` in `parent` is enclosed by matching delimiters.

test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict'
2-
3-
var assert = require('assert')
4-
var test = require('tape')
5-
var visit = require('unist-util-visit')
6-
var retext = require('retext')
7-
var isLiteral = require('.')
1+
import assert from 'assert'
2+
import test from 'tape'
3+
import retext from 'retext'
4+
import {visit} from 'unist-util-visit'
5+
import {isLiteral} from './index.js'
86

97
test('isLiteral()', function (t) {
108
t.throws(

0 commit comments

Comments
 (0)