Skip to content

Commit 2b42f9e

Browse files
committed
Refactor and enforce code-style
1 parent ce5a522 commit 2b42f9e

File tree

5 files changed

+177
-124
lines changed

5 files changed

+177
-124
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# unist-util-map [![Build Status](https://travis-ci.org/syntax-tree/unist-util-map.svg?branch=master)](https://travis-ci.org/syntax-tree/unist-util-map)
1+
# unist-util-visit-parents [![Build Status][build-badge]][build-page]
22

33
Create a new Unist tree with all nodes that mapped by the provided function.
44

5-
Helper for creating [unist: Universal Syntax Tree](https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree").
5+
Helper for creating [unist: Universal Syntax Tree][unist].
66

7-
- retext, remark, hast, textlint
7+
* retext, remark, hast, textlint
88

99
## Installation
1010

11-
npm install unist-util-map
11+
```sh
12+
npm install unist-util-map
13+
```
1214

1315
## Usage
1416

@@ -60,22 +62,34 @@ assert.deepEqual(actual, expected);
6062

6163
## Tests
6264

63-
npm test
65+
```sh
66+
npm test
67+
```
6468

6569
## Contributing
6670

67-
1. Fork it!
68-
2. Create your feature branch: `git checkout -b my-new-feature`
69-
3. Commit your changes: `git commit -am 'Add some feature'`
70-
4. Push to the branch: `git push origin my-new-feature`
71-
5. Submit a pull request :D
71+
1. Fork it!
72+
2. Create your feature branch: `git checkout -b my-new-feature`
73+
3. Commit your changes: `git commit -am 'Add some feature'`
74+
4. Push to the branch: `git push origin my-new-feature`
75+
5. Submit a pull request :D
7276

73-
## Contribute
77+
See [`contribute.md` in `syntax-tree/unist`][contributing] for ways to get
78+
started.
7479

75-
See [`contribute.md` in `syntax-tree/unist`](https://github.com/syntax-tree/unist/blob/master/contributing.md) for ways to get started.
76-
77-
This organisation has a [Code of Conduct](https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md). By interacting with this repository, organisation, or community you agree to abide by its terms.
80+
This organisation has a [Code of Conduct][coc]. By interacting with this
81+
repository, organisation, or community you agree to abide by its terms.
7882

7983
## License
8084

8185
MIT
86+
87+
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-map.svg
88+
89+
[build-page]: https://travis-ci.org/syntax-tree/unist-util-map
90+
91+
[unist]: https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree"
92+
93+
[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
94+
95+
[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md

lib/unist-util-map.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
// LICENSE : MIT
2-
"use strict";
3-
var ObjectAssign = require("object-assign");
4-
module.exports = function map(ast, mapFn) {
5-
return (function preorder(node, index, parent) {
6-
const newNode = ObjectAssign({}, mapFn(node, index, parent));
7-
if (node.children) {
8-
newNode.children = node.children.map(function (child, index) {
9-
return preorder(child, index, node);
10-
});
11-
}
12-
return newNode;
13-
}(ast, null, null));
14-
};
2+
'use strict'
3+
4+
var assign = require('object-assign')
5+
6+
module.exports = map
7+
8+
function map(ast, iteratee) {
9+
return preorder(ast, null, null)
10+
11+
function preorder(node, index, parent) {
12+
var children = node.children
13+
var newNode = assign({}, iteratee(node, index, parent))
14+
15+
if (children) {
16+
newNode.children = children.map(bound)
17+
}
18+
19+
return newNode
20+
21+
function bound(child, index) {
22+
return preorder(child, index, node)
23+
}
24+
}
25+
}

package.json

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,36 @@
2323
"object-assign": "^4.0.1"
2424
},
2525
"devDependencies": {
26-
"@textlint/text-to-ast": "^3.0.0",
27-
"mocha": "^5.0.0"
26+
"mocha": "^5.0.0",
27+
"prettier": "^1.12.1",
28+
"remark-cli": "^5.0.0",
29+
"remark-preset-wooorm": "^4.0.0",
30+
"xo": "^0.20.3"
2831
},
2932
"scripts": {
30-
"test": "mocha"
33+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
34+
"test-api": "mocha",
35+
"test": "npm run format && npm run test-api"
36+
},
37+
"prettier": {
38+
"tabWidth": 2,
39+
"useTabs": false,
40+
"singleQuote": true,
41+
"bracketSpacing": false,
42+
"semi": false,
43+
"trailingComma": "none"
44+
},
45+
"xo": {
46+
"prettier": true,
47+
"esnext": false,
48+
"rules": {
49+
"no-var": "off",
50+
"prefer-arrow-callback": "off"
51+
}
52+
},
53+
"remarkConfig": {
54+
"plugins": [
55+
"preset-wooorm"
56+
]
3157
}
3258
}

test.js

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,90 @@
11
// LICENSE : MIT
2-
"use strict";
3-
const assert = require("assert");
4-
const map = require(".");
5-
const parse = require("@textlint/text-to-ast").parse;
6-
const Syntax = require("@textlint/text-to-ast").Syntax;
7-
const ObjectAssign = require("object-assign");
8-
describe('should not traverse into children of filtered out nodes', function (t) {
9-
it("should map specified node", function () {
10-
const ast = {
11-
"type": "root",
12-
"children": [
13-
{
14-
"type": "node",
15-
"children": [
16-
{"type": "leaf", "value": "1"}
17-
]
18-
},
19-
{"type": "leaf", "value": "2"}
20-
]
21-
};
22-
const actual = map(ast, function (node) {
23-
if (node.type === "leaf") {
24-
return ObjectAssign({}, node, {
25-
value: "CHANGED"
26-
});
27-
}
28-
// no change
29-
return node;
30-
});
31-
const expected = {
32-
"type": "root",
33-
"children": [
34-
{
35-
"type": "node",
36-
"children": [
37-
{"type": "leaf", "value": "CHANGED"}
38-
]
39-
},
40-
{"type": "leaf", "value": "CHANGED"}
41-
]
42-
};
43-
assert.deepEqual(actual, expected);
44-
});
45-
context("when return null", function () {
46-
it("should map as empty object", function () {
47-
const ast = {
48-
"type": "root",
49-
"children": [
50-
{
51-
"type": "node",
52-
"children": [
53-
{"type": "leaf", "value": "1"}
54-
]
55-
},
56-
{"type": "leaf", "value": "2"}
57-
]
58-
};
59-
const actual = map(ast, function (node) {
60-
if (node.type === "leaf") {
61-
return null;
62-
}
63-
// no change
64-
return node;
65-
});
66-
const expected = {
67-
"type": "root",
68-
"children": [
69-
{
70-
"type": "node",
71-
"children": [
72-
{}
73-
]
74-
},
75-
{}
76-
]
77-
};
78-
assert.deepEqual(actual, expected);
79-
});
80-
});
2+
'use strict'
813

82-
context("when pass empty object", function () {
83-
it("should work map", function () {
84-
const ast = {};
85-
const actual = map(ast, function (node) {
86-
return {
87-
value: "test"
88-
};
89-
});
90-
const expected = {};
91-
assert.deepEqual(actual, {
92-
value: "test"
93-
});
94-
});
95-
});
96-
});
4+
/* eslint-env mocha */
975

6+
const assert = require('assert')
7+
const assign = require('object-assign')
8+
const map = require('.')
9+
10+
describe('should not traverse into children of filtered out nodes', function() {
11+
it('should map specified node', function() {
12+
const ast = {
13+
type: 'root',
14+
children: [
15+
{
16+
type: 'node',
17+
children: [{type: 'leaf', value: '1'}]
18+
},
19+
{type: 'leaf', value: '2'}
20+
]
21+
}
22+
const actual = map(ast, function(node) {
23+
if (node.type === 'leaf') {
24+
return assign({}, node, {
25+
value: 'CHANGED'
26+
})
27+
}
28+
// No change
29+
return node
30+
})
31+
const expected = {
32+
type: 'root',
33+
children: [
34+
{
35+
type: 'node',
36+
children: [{type: 'leaf', value: 'CHANGED'}]
37+
},
38+
{type: 'leaf', value: 'CHANGED'}
39+
]
40+
}
41+
assert.deepEqual(actual, expected)
42+
})
43+
context('when return null', function() {
44+
it('should map as empty object', function() {
45+
const ast = {
46+
type: 'root',
47+
children: [
48+
{
49+
type: 'node',
50+
children: [{type: 'leaf', value: '1'}]
51+
},
52+
{type: 'leaf', value: '2'}
53+
]
54+
}
55+
const actual = map(ast, function(node) {
56+
if (node.type === 'leaf') {
57+
return null
58+
}
59+
// No change
60+
return node
61+
})
62+
const expected = {
63+
type: 'root',
64+
children: [
65+
{
66+
type: 'node',
67+
children: [{}]
68+
},
69+
{}
70+
]
71+
}
72+
assert.deepEqual(actual, expected)
73+
})
74+
})
75+
76+
context('when pass empty object', function() {
77+
it('should work map', function() {
78+
const ast = {}
79+
const actual = map(ast, function() {
80+
return {
81+
value: 'test'
82+
}
83+
})
84+
const expected = {
85+
value: 'test'
86+
}
87+
assert.deepEqual(actual, expected)
88+
})
89+
})
90+
})

0 commit comments

Comments
 (0)