Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 14, 2021
1 parent 0fe3709 commit fc6f506
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 43 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
16 changes: 4 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
Expand Down
31 changes: 11 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Christian Murphy <christian.murphy.42@gmail.com>"
],
"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",
Expand All @@ -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,
Expand All @@ -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": [
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'),
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit fc6f506

Please sign in to comment.