Skip to content

Commit 251c6fc

Browse files
committed
Refactor code-style
1 parent 37d114d commit 251c6fc

File tree

5 files changed

+128
-96
lines changed

5 files changed

+128
-96
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
mdast-util-compact.js
3+
mdast-util-compact.min.js

index.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
'use strict';
1+
'use strict'
22

3-
/* Dependencies. */
4-
var visit = require('unist-util-visit');
5-
var modify = require('unist-util-modify-children');
3+
var visit = require('unist-util-visit')
4+
var modify = require('unist-util-modify-children')
65

7-
/* Expose. */
8-
module.exports = compact;
6+
module.exports = compact
97

10-
/* Make an MDAST tree compact by merging adjacent text
11-
* nodes. */
8+
/* Make an MDAST tree compact by merging adjacent text nodes. */
129
function compact(tree, commonmark) {
13-
var modifier = modify(iterator);
10+
var modifier = modify(iterator)
1411

15-
visit(tree, visitor);
12+
visit(tree, visitor)
1613

17-
return tree;
14+
return tree
1815

1916
function visitor(node) {
2017
if (node.children) {
21-
modifier(node);
18+
modifier(node)
2219
}
2320
}
2421

2522
function iterator(child, index, parent) {
26-
var siblings = parent.children;
27-
var prev = index && siblings[index - 1];
23+
var siblings = parent.children
24+
var prev = index && siblings[index - 1]
2825

2926
if (
3027
prev &&
@@ -33,40 +30,41 @@ function compact(tree, commonmark) {
3330
mergeable(child, commonmark)
3431
) {
3532
if (child.value) {
36-
prev.value += child.value;
33+
prev.value += child.value
3734
}
3835

3936
if (child.children) {
40-
prev.children = prev.children.concat(child.children);
37+
prev.children = prev.children.concat(child.children)
4138
}
4239

43-
siblings.splice(index, 1);
40+
siblings.splice(index, 1)
4441

4542
if (prev.position && child.position) {
46-
prev.position.end = child.position.end;
43+
prev.position.end = child.position.end
4744
}
4845

49-
return index;
46+
return index
5047
}
5148
}
5249
}
5350

5451
function mergeable(node, commonmark) {
55-
var start;
56-
var end;
52+
var start
53+
var end
5754

5855
if (node.type === 'text') {
5956
if (!node.position) {
60-
return true;
57+
return true
6158
}
6259

63-
start = node.position.start;
64-
end = node.position.end;
60+
start = node.position.start
61+
end = node.position.end
6562

6663
/* Only merge nodes which occupy the same size as their `value`. */
67-
return start.line !== end.line ||
68-
end.column - start.column === node.value.length;
64+
return (
65+
start.line !== end.line || end.column - start.column === node.value.length
66+
)
6967
}
7068

71-
return commonmark && node.type === 'blockquote';
69+
return commonmark && node.type === 'blockquote'
7270
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"devDependencies": {
2626
"browserify": "^16.0.0",
2727
"nyc": "^12.0.0",
28+
"prettier": "^1.14.2",
2829
"remark-cli": "^5.0.0",
2930
"remark-preset-wooorm": "^4.0.0",
3031
"tape": "^4.0.0",
@@ -33,17 +34,24 @@
3334
"xo": "^0.22.0"
3435
},
3536
"scripts": {
36-
"build-md": "remark . --quiet --frail --output",
37+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3738
"build-bundle": "browserify . -s mdastUtilCompact > mdast-util-compact.js",
3839
"build-mangle": "browserify . -s mdastUtilCompact -p tinyify > mdast-util-compact.min.js",
39-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
40-
"lint": "xo",
40+
"build": "npm run build-bundle && npm run build-mangle",
4141
"test-api": "node test",
4242
"test-coverage": "nyc --reporter lcov tape test.js",
43-
"test": "npm run build && npm run lint && npm run test-coverage"
43+
"test": "npm run format && npm run build && npm run test-coverage"
44+
},
45+
"prettier": {
46+
"tabWidth": 2,
47+
"useTabs": false,
48+
"singleQuote": true,
49+
"bracketSpacing": false,
50+
"semi": false,
51+
"trailingComma": "none"
4452
},
4553
"xo": {
46-
"space": true,
54+
"prettier": true,
4755
"esnext": false,
4856
"ignores": [
4957
"mdast-util-compact.js"

readme.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ npm install mdast-util-compact
1414
## Usage
1515

1616
```javascript
17-
var u = require('unist-builder');
18-
var compact = require('mdast-util-compact');
17+
var u = require('unist-builder')
18+
var compact = require('mdast-util-compact')
1919

20-
var tree = u('strong', [
21-
u('text', 'alpha'),
22-
u('text', ' '),
23-
u('text', 'bravo')
24-
]);
20+
var tree = u('strong', [u('text', 'alpha'), u('text', ' '), u('text', 'bravo')])
2521

26-
compact(tree);
22+
compact(tree)
2723

28-
console.log(tree);
24+
console.log(tree)
2925
```
3026

3127
Yields:

test.js

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,114 @@
1-
'use strict';
1+
'use strict'
22

3-
var test = require('tape');
4-
var u = require('unist-builder');
5-
var compact = require('.');
3+
var test = require('tape')
4+
var u = require('unist-builder')
5+
var compact = require('.')
66

7-
test('compact()', function (t) {
7+
test('compact()', function(t) {
88
t.same(
9-
compact(u('paragraph', [
10-
u('text', 'alpha'),
11-
u('text', ' '),
12-
u('text', 'bravo')
13-
])),
9+
compact(
10+
u('paragraph', [u('text', 'alpha'), u('text', ' '), u('text', 'bravo')])
11+
),
1412
u('paragraph', [u('text', 'alpha bravo')]),
1513
'should compact texts'
16-
);
14+
)
1715

1816
t.same(
19-
compact(u('paragraph', [
20-
u('text', {position: {
21-
start: {line: 1, column: 1},
22-
end: {line: 1, column: 6}
23-
}}, 'alpha'),
24-
u('text', {position: {
25-
start: {line: 1, column: 6},
26-
end: {line: 1, column: 7}
27-
}}, ' '),
28-
u('text', {position: {
29-
start: {line: 1, column: 7},
30-
end: {line: 1, column: 12}
31-
}}, 'bravo')
32-
])),
33-
u('paragraph', [u('text', {position: {
34-
start: {line: 1, column: 1},
35-
end: {line: 1, column: 12}
36-
}}, 'alpha bravo')]),
17+
compact(
18+
u('paragraph', [
19+
u(
20+
'text',
21+
{
22+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 6}}
23+
},
24+
'alpha'
25+
),
26+
u(
27+
'text',
28+
{
29+
position: {start: {line: 1, column: 6}, end: {line: 1, column: 7}}
30+
},
31+
' '
32+
),
33+
u(
34+
'text',
35+
{
36+
position: {start: {line: 1, column: 7}, end: {line: 1, column: 12}}
37+
},
38+
'bravo'
39+
)
40+
])
41+
),
42+
u('paragraph', [
43+
u(
44+
'text',
45+
{
46+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 12}}
47+
},
48+
'alpha bravo'
49+
)
50+
]),
3751
'should merge positions'
38-
);
52+
)
3953

4054
t.same(
41-
compact(u('paragraph', [
42-
u('text', 'at'),
43-
u('text', {position: {
44-
start: {line: 1, column: 3},
45-
end: {line: 1, column: 8}
46-
}}, '&'),
47-
u('text', 't')
48-
])),
55+
compact(
56+
u('paragraph', [
57+
u('text', 'at'),
58+
u(
59+
'text',
60+
{
61+
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
62+
},
63+
'&'
64+
),
65+
u('text', 't')
66+
])
67+
),
4968
u('paragraph', [
5069
u('text', 'at'),
51-
u('text', {position: {
52-
start: {line: 1, column: 3},
53-
end: {line: 1, column: 8}
54-
}}, '&'),
70+
u(
71+
'text',
72+
{
73+
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
74+
},
75+
'&'
76+
),
5577
u('text', 't')
5678
]),
5779
'should not compact texts with incompatible positions'
58-
);
80+
)
5981

6082
t.same(
61-
compact(u('root', [
62-
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
63-
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
64-
])),
83+
compact(
84+
u('root', [
85+
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
86+
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
87+
])
88+
),
6589
u('root', [
6690
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
6791
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
6892
]),
6993
'should not compact blockquotes'
70-
);
94+
)
7195

7296
t.same(
73-
compact(u('root', [
74-
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
75-
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
76-
]), true),
97+
compact(
98+
u('root', [
99+
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
100+
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
101+
]),
102+
true
103+
),
77104
u('root', [
78105
u('blockquote', [
79106
u('paragraph', [u('text', 'Alpha.')]),
80107
u('paragraph', [u('text', 'Bravo.')])
81108
])
82109
]),
83110
'should compact blockquotes in commonmark mode'
84-
);
111+
)
85112

86-
t.end();
87-
});
113+
t.end()
114+
})

0 commit comments

Comments
 (0)