Skip to content

Commit ba7742f

Browse files
committed
Refactor code-style
1 parent 09d53fe commit ba7742f

File tree

5 files changed

+351
-441
lines changed

5 files changed

+351
-441
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-heading-range.js
3+
mdast-util-heading-range.min.js

index.js

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

3-
var toString = require('mdast-util-to-string');
3+
var toString = require('mdast-util-to-string')
44

5-
module.exports = headingRange;
5+
module.exports = headingRange
66

7-
var splice = [].splice;
7+
var splice = [].splice
88

9-
/* Search `node` with `options` and invoke `callback`. */
9+
// Search `node` with `options` and invoke `callback`.
1010
function headingRange(node, options, callback) {
11-
var test = options;
12-
var ignoreFinalDefinitions = false;
11+
var test = options
12+
var ignoreFinalDefinitions = false
1313

14-
/* Object, not regex. */
14+
// Object, not regex.
1515
if (test && typeof test === 'object' && !('exec' in test)) {
16-
ignoreFinalDefinitions = test.ignoreFinalDefinitions === true;
17-
test = test.test;
16+
ignoreFinalDefinitions = test.ignoreFinalDefinitions === true
17+
test = test.test
1818
}
1919

2020
if (typeof test === 'string') {
21-
test = toExpression(test);
21+
test = toExpression(test)
2222
}
2323

24-
/* Regex */
24+
// Regex
2525
if (test && 'exec' in test) {
26-
test = wrapExpression(test);
26+
test = wrapExpression(test)
2727
}
2828

2929
if (typeof test !== 'function') {
3030
throw new Error(
31-
'Expected `string`, `regexp`, or `function` for `test`, ' +
32-
'not `' + test + '`'
33-
);
31+
'Expected `string`, `regexp`, or `function` for `test`, not `' +
32+
test +
33+
'`'
34+
)
3435
}
3536

36-
search(node, test, ignoreFinalDefinitions, callback);
37+
search(node, test, ignoreFinalDefinitions, callback)
3738
}
3839

39-
/* Search a node for heading range. */
40+
// Search a node for heading range.
4041
function search(root, test, skip, callback) {
41-
var index = -1;
42-
var children = root.children;
43-
var length = children.length;
44-
var depth = null;
45-
var start = null;
46-
var end = null;
47-
var nodes;
48-
var clean;
49-
var child;
42+
var index = -1
43+
var children = root.children
44+
var length = children.length
45+
var depth = null
46+
var start = null
47+
var end = null
48+
var nodes
49+
var clean
50+
var child
5051

5152
while (++index < length) {
52-
child = children[index];
53+
child = children[index]
5354

5455
if (closing(child, depth)) {
55-
end = index;
56-
break;
56+
end = index
57+
break
5758
}
5859

5960
if (opening(child, depth, test)) {
60-
start = index;
61-
depth = child.depth;
61+
start = index
62+
depth = child.depth
6263
}
6364
}
6465

6566
if (start !== null) {
6667
if (end === null) {
67-
end = length;
68+
end = length
6869
}
6970

7071
if (skip) {
7172
while (end > start) {
72-
child = children[end - 1];
73+
child = children[end - 1]
7374

7475
if (!definition(child)) {
75-
break;
76+
break
7677
}
7778

78-
end--;
79+
end--
7980
}
8081
}
8182

@@ -88,57 +89,56 @@ function search(root, test, skip, callback) {
8889
start: start,
8990
end: children[end] ? end : null
9091
}
91-
);
92+
)
9293

93-
clean = [];
94-
index = -1;
95-
length = nodes && nodes.length;
94+
clean = []
95+
index = -1
96+
length = nodes && nodes.length
9697

97-
/* Ensure no empty nodes are inserted. This could
98-
* be the case if `end` is in `nodes` but no `end`
99-
* node exists. */
98+
// Ensure no empty nodes are inserted. This could be the case if `end` is
99+
// in `nodes` but no `end` node exists.
100100
while (++index < length) {
101101
if (nodes[index]) {
102-
clean.push(nodes[index]);
102+
clean.push(nodes[index])
103103
}
104104
}
105105

106106
if (nodes) {
107-
splice.apply(children, [start, end - start + 1].concat(clean));
107+
splice.apply(children, [start, end - start + 1].concat(clean))
108108
}
109109
}
110110
}
111111

112-
/* Transform a string into an applicable expression. */
112+
// Transform a string into an applicable expression.
113113
function toExpression(value) {
114-
return new RegExp('^(' + value + ')$', 'i');
114+
return new RegExp('^(' + value + ')$', 'i')
115115
}
116116

117-
/* Wrap an expression into an assertion function. */
117+
// Wrap an expression into an assertion function.
118118
function wrapExpression(expression) {
119-
return assertion;
119+
return assertion
120120

121-
/* Assert `value` matches the bound `expression`. */
121+
// Assert `value` matches the bound `expression`.
122122
function assertion(value) {
123-
return expression.test(value);
123+
return expression.test(value)
124124
}
125125
}
126126

127-
/* Check if `node` is a heading. */
127+
// Check if `node` is a heading.
128128
function heading(node) {
129-
return node && node.type === 'heading';
129+
return node && node.type === 'heading'
130130
}
131131

132-
/* Check if `node` is the main heading. */
132+
// Check if `node` is the main heading.
133133
function opening(node, depth, test) {
134-
return depth === null && heading(node) && test(toString(node), node);
134+
return depth === null && heading(node) && test(toString(node), node)
135135
}
136136

137-
/* Check if `node` is the next heading. */
137+
// Check if `node` is the next heading.
138138
function closing(node, depth) {
139-
return depth && heading(node) && node.depth <= depth;
139+
return depth && heading(node) && node.depth <= depth
140140
}
141141

142142
function definition(node) {
143-
return node.type === 'definition' || node.type === 'footnoteDefinition';
143+
return node.type === 'definition' || node.type === 'footnoteDefinition'
144144
}

package.json

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,48 @@
2424
},
2525
"devDependencies": {
2626
"browserify": "^16.0.0",
27-
"esmangle": "^1.0.0",
2827
"nyc": "^13.0.0",
28+
"prettier": "^1.14.3",
2929
"remark": "^10.0.0",
3030
"remark-cli": "^6.0.0",
3131
"remark-preset-wooorm": "^4.0.0",
3232
"tape": "^4.4.0",
33+
"tinyify": "^2.4.3",
3334
"xo": "^0.23.0"
3435
},
3536
"scripts": {
36-
"build-md": "remark . --quiet --frail --output",
37-
"build-bundle": "browserify index.js --no-builtins -s mdastUtilHeadingRange > mdast-util-heading-range.js",
38-
"build-mangle": "esmangle mdast-util-heading-range.js > mdast-util-heading-range.min.js",
39-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
40-
"lint": "xo",
41-
"test-api": "node test.js",
37+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
38+
"build-bundle": "browserify . -s mdastUtilHeadingRange > mdast-util-heading-range.js",
39+
"build-mangle": "browserify . -s mdastUtilHeadingRange -p tinyify > mdast-util-heading-range.min.js",
40+
"build": "npm run build-bundle && npm run build-mangle",
41+
"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+
"nyc": {
46+
"check-coverage": true,
47+
"lines": 100,
48+
"functions": 100,
49+
"branches": 100
50+
},
51+
"prettier": {
52+
"tabWidth": 2,
53+
"useTabs": false,
54+
"singleQuote": true,
55+
"bracketSpacing": false,
56+
"semi": false,
57+
"trailingComma": "none"
4458
},
4559
"xo": {
46-
"space": true,
60+
"prettier": true,
4761
"esnext": false,
4862
"rules": {
4963
"unicorn/prefer-type-error": "off"
5064
},
51-
"ignore": [
65+
"ignores": [
5266
"mdast-util-heading-range.js"
5367
]
5468
},
55-
"nyc": {
56-
"check-coverage": true,
57-
"lines": 100,
58-
"functions": 100,
59-
"branches": 100
60-
},
6169
"remarkConfig": {
6270
"plugins": [
6371
"preset-wooorm"

readme.md

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@ Bar.
2525
And our script, `example.js`, looks as follows:
2626

2727
```javascript
28-
var vfile = require('to-vfile');
29-
var remark = require('remark');
30-
var heading = require('mdast-util-heading-range');
28+
var vfile = require('to-vfile')
29+
var remark = require('remark')
30+
var heading = require('mdast-util-heading-range')
3131

3232
remark()
3333
.use(plugin)
34-
.process(vfile.readSync('example.md'), function (err, file) {
35-
if (err) throw err;
36-
console.log(String(file));
37-
});
34+
.process(vfile.readSync('example.md'), function(err, file) {
35+
if (err) throw err
36+
console.log(String(file))
37+
})
3838

3939
function plugin() {
40-
return transformer;
41-
function transformer(tree) {
42-
heading(tree, 'foo', mutate);
40+
return transform
41+
42+
function transform(tree) {
43+
heading(tree, 'foo', mutate)
4344
}
45+
4446
function mutate(start, nodes, end) {
4547
return [
4648
start,
4749
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
4850
end
49-
];
51+
]
5052
}
5153
}
5254
```
@@ -71,14 +73,17 @@ A Section is a heading that passes `test`, until the next heading of the same
7173
or lower depth, or the end of the document. If `ignoreFinalDefinitions: true`,
7274
final definitions “in” the section are excluded.
7375

74-
###### `options`
76+
##### `options`
77+
78+
###### `options.test`
7579

76-
* `test` (`string`, `RegExp`, [`Function`][test])
77-
— Heading to look for.
78-
When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
79-
when `RegExp`, wrapped in `function (value) {expression.test(value)}`
80-
* `ignoreFinalDefinitions` (`boolean`, default: `false`)
81-
— Ignore final definitions otherwise in the section
80+
Heading to look for (`string`, `RegExp`, [`Function`][test]).
81+
When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
82+
when `RegExp`, wrapped in `function (value) {expression.test(value)}`
83+
84+
###### `options.ignoreFinalDefinitions`
85+
86+
Ignore final definitions otherwise in the section (`boolean`, default: `false`).
8287

8388
#### `function test(value, node)`
8489

@@ -93,16 +98,27 @@ itself ([`Heading`][heading]) to check if it’s the one to look for.
9398

9499
Callback invoked when a range is found.
95100

96-
###### Parameters
101+
##### Parameters
102+
103+
###### `start`
104+
105+
Start of range ([`Heading`][heading]).
106+
107+
###### `nodes`
108+
109+
Nodes between `start` and `end` ([`Array.<Node>`][node]).
110+
111+
###### `end`
112+
113+
End of range, if any ([`Node?`][node]).
114+
115+
###### `scope`
97116

98-
* `start` ([`Heading`][heading]) — Start of range
99-
* `nodes` ([`Array.<Node>`][node]) — Nodes between `start` and `end`
100-
* `end` ([`Node?`][node]) — End of range, if any
101-
* `scope` (`Object`):
117+
Extra info (`Object`):
102118

103-
* `parent` ([`Node`][node]) — Parent of the range
104-
* `start` (`number`) — Index of `start` in `parent`
105-
* `end` (`number?`) — Index of `end` in `parent`
119+
* `parent` ([`Node`][node]) — Parent of the range
120+
* `start` (`number`) — Index of `start` in `parent`
121+
* `end` (`number?`) — Index of `end` in `parent`
106122

107123
## Contribute
108124

0 commit comments

Comments
 (0)