Skip to content

Commit a8d04b6

Browse files
committed
Use ESM
1 parent c0f366e commit a8d04b6

File tree

8 files changed

+65
-94
lines changed

8 files changed

+65
-94
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-
unist-util-inspect.js
7-
unist-util-inspect.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-
unist-util-inspect.js
3-
unist-util-inspect.min.js
4-
*.json
52
*.md

color-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = false
1+
export var color = false

color.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = true
1+
export var color = true

index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
'use strict'
1+
import {color} from './color.js'
22

3-
var color = require('./color')
3+
export var inspect = color
4+
? inspectColor
5+
: /* istanbul ignore next */ inspectNoColor
46

5-
module.exports = color ? inspect : /* istanbul ignore next */ noColor
6-
7-
inspect.color = noColor.color = inspect
8-
inspect.noColor = noColor.noColor = noColor
7+
var own = {}.hasOwnProperty
98

109
var bold = ansiColor(1, 22)
1110
var dim = ansiColor(2, 22)
1211
var yellow = ansiColor(33, 39)
1312
var green = ansiColor(32, 39)
1413

1514
// ANSI color regex.
15+
/* eslint-disable-next-line no-control-regex */
1616
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
1717

1818
// Inspects a node, without using color.
19-
function noColor(node) {
20-
return inspect(node).replace(colorExpression, '')
19+
export function inspectNoColor(node) {
20+
return inspectColor(node).replace(colorExpression, '')
2121
}
2222

2323
// Inspects a node.
24-
function inspect(tree, options) {
24+
export function inspectColor(tree, options) {
2525
var positions =
26-
!options || options.showPositions == null ? true : options.showPositions
26+
!options ||
27+
options.showPositions === null ||
28+
options.showPositions === undefined
29+
? true
30+
: options.showPositions
2731

2832
return inspectValue(tree)
2933

@@ -69,6 +73,8 @@ function inspect(tree, options) {
6973
var formatted
7074

7175
for (key in object) {
76+
if (!own.call(object, key)) continue
77+
7278
value = object[key]
7379

7480
if (
@@ -116,7 +122,7 @@ function inspect(tree, options) {
116122

117123
return indent(
118124
result.join('\n'),
119-
(object.children && object.children.length ? dim('│') : ' ') + ' '
125+
(object.children && object.children.length > 0 ? dim('│') : ' ') + ' '
120126
)
121127
}
122128

@@ -176,8 +182,8 @@ function stringifyPosition(value) {
176182
point(position.start)
177183
point(position.end)
178184

179-
if (positions.length) result.push(positions.join('-'))
180-
if (offsets.length) result.push(offsets.join('-'))
185+
if (positions.length > 0) result.push(positions.join('-'))
186+
if (offsets.length > 0) result.push(offsets.join('-'))
181187

182188
return result.join(', ')
183189

package.json

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,45 @@
2323
"contributors": [
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
26-
"files": [
27-
"index.js",
28-
"color.js",
29-
"color-browser.js",
30-
"types/index.d.ts"
31-
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
29+
"types": "types/index.d.ts",
3230
"browser": {
3331
"./color.js": "./color-browser.js"
3432
},
3533
"react-native": {
3634
"./color.js": "./color-browser.js"
3735
},
38-
"types": "types/index.d.ts",
36+
"files": [
37+
"types/index.d.ts",
38+
"color.js",
39+
"color-browser.js",
40+
"index.js"
41+
],
3942
"devDependencies": {
4043
"@types/unist": "^2.0.0",
41-
"browserify": "^17.0.0",
44+
"c8": "^7.0.0",
4245
"chalk": "^4.0.0",
4346
"dtslint": "^4.0.0",
4447
"hastscript": "^6.0.0",
45-
"nyc": "^15.0.0",
4648
"prettier": "^2.0.0",
4749
"remark-cli": "^9.0.0",
4850
"remark-preset-wooorm": "^8.0.0",
4951
"retext": "^7.0.0",
5052
"strip-ansi": "^6.0.0",
5153
"tape": "^5.0.0",
52-
"tinyify": "^3.0.0",
5354
"unist-builder": "^2.0.0",
5455
"xast-util-from-xml": "^1.0.0",
5556
"xastscript": "^2.0.0",
5657
"xo": "^0.38.0"
5758
},
5859
"scripts": {
5960
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
60-
"build-bundle": "browserify . -s unistUtilInspect -o unist-util-inspect.js",
61-
"build-mangle": "browserify . -s unistUtilInspect -o unist-util-inspect.min.js -p tinyify",
62-
"build": "npm run build-bundle && npm run build-mangle",
63-
"test-api": "node test",
64-
"test-coverage": "nyc --reporter lcov tape test.js",
61+
"test-api": "node test.js",
62+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
6563
"test-types": "dtslint types",
66-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
64+
"test": "npm run format && npm run test-coverage && npm run test-types"
6765
},
6866
"prettier": {
6967
"tabWidth": 2,
@@ -75,35 +73,12 @@
7573
},
7674
"xo": {
7775
"prettier": true,
78-
"esnext": false,
7976
"rules": {
8077
"complexity": "off",
81-
"eqeqeq": [
82-
"error",
83-
"always",
84-
{
85-
"null": "ignore"
86-
}
87-
],
88-
"guard-for-in": "off",
89-
"no-control-regex": "off",
90-
"no-eq-null": "off",
91-
"no-multi-assign": "off",
92-
"import/no-extraneous-dependencies": "off",
93-
"unicorn/explicit-length-check": "off",
94-
"unicorn/prefer-number-properties": "off",
95-
"unicorn/prefer-includes": "off"
96-
},
97-
"ignore": [
98-
"types",
99-
"unist-util-inspect.js"
100-
]
101-
},
102-
"nyc": {
103-
"check-coverage": true,
104-
"lines": 100,
105-
"functions": 100,
106-
"branches": 100
78+
"import/no-mutable-exports": "off",
79+
"no-var": "off",
80+
"prefer-arrow-callback": "off"
81+
}
10782
},
10883
"remarkConfig": {
10984
"plugins": [

readme.md

Lines changed: 12 additions & 4 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
@@ -51,6 +54,10 @@ root[2]
5154

5255
## API
5356

57+
This package exports the following identifiers: `inspect`, `inspectColor`, and
58+
`inspectNoColor`.
59+
There is no default export.
60+
5461
### `inspect(node[, options])`
5562

5663
Inspect the given [`node`][node].
@@ -65,12 +72,13 @@ Whether to include positional information (`boolean`, default: `true`).
6572

6673
`string` — String representing `node`.
6774

68-
### `inspect.<style>[.<style>…](node[, options])`
75+
### `inspectColor(node[, options])`
76+
77+
Inspect, with ANSI color sequences (default in Node).
6978

70-
Where `<style>` is either `color` or `noColor`.
79+
### `inspectNoColor(node[, options])`
7180

72-
To explicitly add or remove ANSI sequences, use `inspect.color(node)` or
73-
`inspect.noColor(node)`.
81+
Inspect, but without ANSI color sequences (default in browser).
7482

7583
## Contribute
7684

test.js

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var chalk = require('chalk')
5-
var strip = require('strip-ansi')
6-
var u = require('unist-builder')
7-
var h = require('hastscript')
8-
var x = require('xastscript')
9-
var retext = require('retext')
10-
var fromXml = require('xast-util-from-xml')
11-
var inspect = require('.')
1+
import test from 'tape'
2+
import chalk from 'chalk'
3+
import strip from 'strip-ansi'
4+
import u from 'unist-builder'
5+
import h from 'hastscript'
6+
import x from 'xastscript'
7+
import retext from 'retext'
8+
import fromXml from 'xast-util-from-xml'
9+
import {inspect, inspectColor, inspectNoColor} from './index.js'
1210

1311
var chalkEnabled = new chalk.Instance({level: 1})
1412

@@ -17,16 +15,6 @@ var paragraph = 'Some simple text. Other “sentence”.'
1715
test('inspect', function (t) {
1816
t.equal(typeof inspect, 'function', 'should be a `function`')
1917

20-
t.test('should have `color` and `noColor` properties', function (st) {
21-
st.equal(typeof inspect.color, 'function')
22-
st.equal(typeof inspect.noColor, 'function')
23-
24-
st.equal(typeof inspect.color.noColor, 'function')
25-
st.equal(typeof inspect.noColor.color, 'function')
26-
27-
st.end()
28-
})
29-
3018
t.end()
3119
})
3220

@@ -69,7 +57,7 @@ test('inspect()', function (t) {
6957
t.test('should work on non-nodes', function (st) {
7058
st.equal(strip(inspect('foo')), '"foo"')
7159
st.equal(strip(inspect(null)), 'null')
72-
st.equal(strip(inspect(NaN)), 'null')
60+
st.equal(strip(inspect(Number.NaN)), 'null')
7361
st.equal(strip(inspect(3)), '3')
7462

7563
st.end()
@@ -340,9 +328,9 @@ test('inspect()', function (t) {
340328
t.end()
341329
})
342330

343-
test('inspect.noColor()', function (t) {
331+
test('inspectNoColor()', function (t) {
344332
t.equal(
345-
inspect.noColor(retext().parse(paragraph).children[0].children[0]),
333+
inspectNoColor(retext().parse(paragraph).children[0].children[0]),
346334
[
347335
'SentenceNode[6] (1:1-1:18, 0-17)',
348336
'├─0 WordNode[1] (1:1-1:5, 0-4)',
@@ -361,9 +349,9 @@ test('inspect.noColor()', function (t) {
361349
t.end()
362350
})
363351

364-
test('inspect.color()', function (t) {
352+
test('inspectColor()', function (t) {
365353
t.equal(
366-
inspect.color(retext().parse(paragraph).children[0].children[0]),
354+
inspectColor(retext().parse(paragraph).children[0].children[0]),
367355
[
368356
chalkEnabled.bold('SentenceNode') +
369357
chalkEnabled.dim('[') +

0 commit comments

Comments
 (0)