Skip to content

Commit af39518

Browse files
committed
Refactor readme.md
1 parent 13a7f74 commit af39518

File tree

1 file changed

+101
-64
lines changed

1 file changed

+101
-64
lines changed

readme.md

Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,141 @@
1-
# unist-util-map [![Build Status][build-badge]][build-page]
1+
# unist-util-map
22

3-
Create a new Unist tree with all nodes that mapped by the provided function.
3+
[![Build][build-badge]][build]
4+
[![Coverage][coverage-badge]][coverage]
5+
[![Downloads][downloads-badge]][downloads]
6+
[![Size][size-badge]][size]
47

5-
Helper for creating [unist: Universal Syntax Tree][unist].
8+
[**unist**][unist] utility to create a new [tree][] by mapping all [node][]s
9+
with the given function.
610

7-
* [retext][], [remark][], [rehype][], [textlint][]
11+
## Install
812

9-
## Installation
13+
[npm][]:
1014

1115
```sh
1216
npm install unist-util-map
1317
```
1418

1519
## Usage
1620

17-
### `map(AST, function(node, index, parent){ /* return */ }): AST`
21+
```js
22+
var u = require('unist-builder')
23+
var map = require('unist-util-map')
24+
25+
var tree = u('tree', [
26+
u('leaf', 'leaf 1'),
27+
u('node', [u('leaf', 'leaf 2')]),
28+
u('void'),
29+
u('leaf', 'leaf 3')
30+
])
31+
32+
var next = map(tree, function(node) {
33+
return node.type === 'leaf'
34+
? Object.assign({}, node, {value: 'CHANGED'})
35+
: node
36+
})
1837

19-
map function return new AST object.
38+
console.dir(next, {depth: null})
39+
```
2040

21-
```js
22-
const assert = require('assert')
23-
const assign = require('object-assign')
24-
const map = require('unist-util-map')
41+
Yields:
2542

26-
// Input
27-
const tree = {
28-
type: 'root',
43+
```js
44+
{
45+
type: 'tree',
2946
children: [
30-
{
31-
type: 'node',
32-
children: [{type: 'leaf', value: '1'}]
33-
},
34-
{type: 'leaf', value: '2'}
47+
{ type: 'leaf', value: 'CHANGED' },
48+
{ type: 'node', children: [ { type: 'leaf', value: 'CHANGED' } ] },
49+
{ type: 'void' },
50+
{ type: 'leaf', value: 'CHANGED' }
3551
]
3652
}
53+
```
3754

38-
// Transform:
39-
const actual = map(tree, function(node) {
40-
if (node.type === 'leaf') {
41-
return assign({}, node, {value: 'CHANGED'})
42-
}
43-
// No change
44-
return node
45-
})
55+
…note that `tree` is not mutated.
4656

47-
// Expected output:
48-
const expected = {
49-
type: 'root',
50-
children: [
51-
{
52-
type: 'node',
53-
children: [{type: 'leaf', value: 'CHANGED'}]
54-
},
55-
{type: 'leaf', value: 'CHANGED'}
56-
]
57-
}
57+
## API
5858

59-
assert.deepEqual(actual, expected)
60-
```
59+
### `map(tree, mapFn)`
6160

62-
## Tests
61+
Create a new [tree][] by mapping all [node][]s with the given function.
6362

64-
```sh
65-
npm test
66-
```
63+
###### Parameters
64+
65+
* `tree` ([`Node`][node]) — [Tree][] to map
66+
* `callback` ([`Function`][callback]) — Function that returns a new node
67+
68+
###### Returns
69+
70+
[`Node`][node] — New mapped [tree][].
6771

68-
## Contributing
72+
#### `function mapFn(node[, index, parent])`
6973

70-
1. Fork it!
71-
2. Create your feature branch: `git checkout -b my-new-feature`
72-
3. Commit your changes: `git commit -am 'Add some feature'`
73-
4. Push to the branch: `git push origin my-new-feature`
74-
5. Submit a pull request :D
74+
Function called with a [node][] to produce a new node.
7575

76-
See [`contribute.md` in `syntax-tree/unist`][contributing] for ways to get
76+
###### Parameters
77+
78+
* `node` ([`Node`][node]) — Current [node][] being processed
79+
* `index` (`number?`) — [Index][] of `node`, or `null`
80+
* `parent` (`Node?`) — [Parent][] of `node`, or `null`
81+
82+
###### Returns
83+
84+
[`Node`][node] — Node to be used in the new [tree][].
85+
Its children are not used: if the original node has children, those are mapped.
86+
87+
## Contribute
88+
89+
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
7790
started.
91+
See [`support.md`][support] for ways to get help.
7892

79-
This organisation has a [Code of Conduct][coc]. By interacting with this
80-
repository, organisation, or community you agree to abide by its terms.
93+
This project has a [Code of Conduct][coc].
94+
By interacting with this repository, organisation, or community you agree to
95+
abide by its terms.
8196

8297
## License
8398

84-
[MIT][]
99+
[MIT][license] © [azu][author]
100+
101+
<!-- Definitions -->
102+
103+
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-find-all-after.svg
104+
105+
[build]: https://travis-ci.org/syntax-tree/unist-util-find-all-after
106+
107+
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-find-all-after.svg
108+
109+
[coverage]: https://codecov.io/github/syntax-tree/unist-util-find-all-after
110+
111+
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-find-all-after.svg
112+
113+
[downloads]: https://www.npmjs.com/package/unist-util-find-all-after
114+
115+
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-find-all-after.svg
116+
117+
[size]: https://bundlephobia.com/result?p=unist-util-find-all-after
118+
119+
[npm]: https://docs.npmjs.com/cli/install
120+
121+
[license]: license
85122

86-
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-map.svg
123+
[author]: https://efcl.info
87124

88-
[build-page]: https://travis-ci.org/syntax-tree/unist-util-map
125+
[unist]: https://github.com/syntax-tree/unist
89126

90-
[unist]: https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree"
127+
[node]: https://github.com/syntax-tree/unist#node
91128

92-
[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
129+
[tree]: https://github.com/syntax-tree/unist#tree
93130

94-
[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md
131+
[parent]: https://github.com/syntax-tree/unist#parent-1
95132

96-
[remark]: https://github.com/remarkjs/remark
133+
[index]: https://github.com/syntax-tree/unist#index
97134

98-
[retext]: https://github.com/retextjs/retext
135+
[callback]: #function-mapfnnode-index-parent
99136

100-
[rehype]: https://github.com/rehypejs/rehype
137+
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
101138

102-
[textlint]: https://github.com/textlint/textlint
139+
[support]: https://github.com/syntax-tree/.github/blob/master/support.md
103140

104-
[mit]: license
141+
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md

0 commit comments

Comments
 (0)