diff --git a/.gitignore b/.gitignore index fdefc8c..735f4af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ .DS_Store *.log -.nyc_output/ coverage/ node_modules/ yarn.lock diff --git a/.prettierignore b/.prettierignore index e7939c4..cebe81f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,2 @@ coverage/ -*.json *.md diff --git a/index.js b/index.js index 23b3f50..7c6ec16 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,11 @@ -// LICENSE : MIT -'use strict' - -var assign = require('object-assign') - -module.exports = map - -function map(tree, iteratee) { +export function map(tree, iteratee) { return preorder(tree, null, null) function preorder(node, index, parent) { - var children = node.children - var newNode = assign({}, iteratee(node, index, parent)) + var newNode = Object.assign({}, iteratee(node, index, parent)) - if (children) { - newNode.children = children.map(function (child, index) { + if (node.children) { + newNode.children = node.children.map(function (child, index) { return preorder(child, index, node) }) } diff --git a/package.json b/package.json index 9fb5782..a66223f 100644 --- a/package.json +++ b/package.json @@ -26,18 +26,18 @@ "Titus Wormer (https://wooorm.com)", "Christian Murphy " ], + "sideEffects": false, + "type": "module", + "main": "index.js", + "types": "types/index.d.ts", "files": [ "index.js", "types/index.d.ts" ], - "types": "types/index.d.ts", - "dependencies": { - "@types/mdast": "^3.0.0", - "object-assign": "^4.0.0" - }, + "dependencies": {}, "devDependencies": { + "c8": "^7.0.0", "dtslint": "^4.0.0", - "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^9.0.0", "remark-preset-wooorm": "^8.0.0", @@ -49,17 +49,11 @@ }, "scripts": { "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", - "test-api": "node test", - "test-coverage": "nyc --reporter lcov tape test.js", + "test-api": "node test.js", + "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", "test-types": "dtslint types", "test": "npm run format && npm run test-coverage && npm run test-types" }, - "nyc": { - "check-coverage": true, - "lines": 100, - "functions": 100, - "branches": 100 - }, "prettier": { "tabWidth": 2, "useTabs": false, @@ -70,13 +64,10 @@ }, "xo": { "prettier": true, - "esnext": false, "rules": { - "unicorn/no-fn-reference-in-iterator": "off" - }, - "ignore": [ - "types/" - ] + "no-var": "off", + "prefer-arrow-callback": "off" + } }, "remarkConfig": { "plugins": [ diff --git a/readme.md b/readme.md index 44f4f5b..1af0b69 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,9 @@ with the given function. ## Install +This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): +Node 12+ is needed to use it and it must be `import`ed instead of `require`d. + [npm][]: ```sh @@ -22,8 +25,8 @@ npm install unist-util-map ## Use ```js -var u = require('unist-builder') -var map = require('unist-util-map') +import u from 'unist-builder' +import {map} from 'unist-util-map' var tree = u('tree', [ u('leaf', 'leaf 1'), @@ -59,6 +62,9 @@ Yields: ## API +This package exports the following identifiers: `map`. +There is no default export. + ### `map(tree, mapFn)` Create a new [tree][] by mapping all [node][]s with the given function. diff --git a/test.js b/test.js index eb38807..0ffc59f 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,6 @@ -'use strict' - -var test = require('tape') -var assign = require('object-assign') -var u = require('unist-builder') -var map = require('.') +import test from 'tape' +import u from 'unist-builder' +import {map} from './index.js' test('unist-util-map', function (t) { t.deepEqual( @@ -27,7 +24,9 @@ test('unist-util-map', function (t) { t.end() function changeLeaf(node) { - return node.type === 'leaf' ? assign({}, node, {value: 'CHANGED'}) : node + return node.type === 'leaf' + ? Object.assign({}, node, {value: 'CHANGED'}) + : node } function nullLeaf(node) {