|
1 |
| -# unist-util-map [![Build Status][build-badge]][build-page] |
| 1 | +# unist-util-map |
2 | 2 |
|
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] |
4 | 7 |
|
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. |
6 | 10 |
|
7 |
| -* [retext][], [remark][], [rehype][], [textlint][] |
| 11 | +## Install |
8 | 12 |
|
9 |
| -## Installation |
| 13 | +[npm][]: |
10 | 14 |
|
11 | 15 | ```sh
|
12 | 16 | npm install unist-util-map
|
13 | 17 | ```
|
14 | 18 |
|
15 | 19 | ## Usage
|
16 | 20 |
|
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 | +}) |
18 | 37 |
|
19 |
| -map function return new AST object. |
| 38 | +console.dir(next, {depth: null}) |
| 39 | +``` |
20 | 40 |
|
21 |
| -```js |
22 |
| -const assert = require('assert') |
23 |
| -const assign = require('object-assign') |
24 |
| -const map = require('unist-util-map') |
| 41 | +Yields: |
25 | 42 |
|
26 |
| -// Input |
27 |
| -const tree = { |
28 |
| - type: 'root', |
| 43 | +```js |
| 44 | +{ |
| 45 | + type: 'tree', |
29 | 46 | 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' } |
35 | 51 | ]
|
36 | 52 | }
|
| 53 | +``` |
37 | 54 |
|
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. |
46 | 56 |
|
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 |
58 | 58 |
|
59 |
| -assert.deepEqual(actual, expected) |
60 |
| -``` |
| 59 | +### `map(tree, mapFn)` |
61 | 60 |
|
62 |
| -## Tests |
| 61 | +Create a new [tree][] by mapping all [node][]s with the given function. |
63 | 62 |
|
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][]. |
67 | 71 |
|
68 |
| -## Contributing |
| 72 | +#### `function mapFn(node[, index, parent])` |
69 | 73 |
|
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. |
75 | 75 |
|
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 |
77 | 90 | started.
|
| 91 | +See [`support.md`][support] for ways to get help. |
78 | 92 |
|
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. |
81 | 96 |
|
82 | 97 | ## License
|
83 | 98 |
|
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 |
85 | 122 |
|
86 |
| -[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-map.svg |
| 123 | +[author]: https://efcl.info |
87 | 124 |
|
88 |
| -[build-page]: https://travis-ci.org/syntax-tree/unist-util-map |
| 125 | +[unist]: https://github.com/syntax-tree/unist |
89 | 126 |
|
90 |
| -[unist]: https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree" |
| 127 | +[node]: https://github.com/syntax-tree/unist#node |
91 | 128 |
|
92 |
| -[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md |
| 129 | +[tree]: https://github.com/syntax-tree/unist#tree |
93 | 130 |
|
94 |
| -[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md |
| 131 | +[parent]: https://github.com/syntax-tree/unist#parent-1 |
95 | 132 |
|
96 |
| -[remark]: https://github.com/remarkjs/remark |
| 133 | +[index]: https://github.com/syntax-tree/unist#index |
97 | 134 |
|
98 |
| -[retext]: https://github.com/retextjs/retext |
| 135 | +[callback]: #function-mapfnnode-index-parent |
99 | 136 |
|
100 |
| -[rehype]: https://github.com/rehypejs/rehype |
| 137 | +[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md |
101 | 138 |
|
102 |
| -[textlint]: https://github.com/textlint/textlint |
| 139 | +[support]: https://github.com/syntax-tree/.github/blob/master/support.md |
103 | 140 |
|
104 |
| -[mit]: license |
| 141 | +[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md |
0 commit comments