Skip to content

Commit f1bbbb5

Browse files
committed
Refactor code-style
1 parent 85d9230 commit f1bbbb5

File tree

5 files changed

+432
-544
lines changed

5 files changed

+432
-544
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,svg,remarkrc,eslintrc}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 89 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,116 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2015 Titus Wormer
4-
* @license MIT
5-
* @module mdast:util:heading-range
6-
* @fileoverview Markdown heading as ranges in mdast.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env commonjs */
12-
13-
/*
14-
* Dependencies.
15-
*/
16-
3+
/* Dependencies. */
174
var toString = require('mdast-util-to-string');
185

19-
/*
20-
* Methods.
21-
*/
6+
/* Expose. */
7+
module.exports = headingRange;
228

9+
/* Methods. */
2310
var splice = [].splice;
2411

25-
/**
26-
* Transform a string into an applicable expression.
27-
*
28-
* @param {string} value - Value to transform.
29-
* @return {RegExp} - Expression.
30-
*/
31-
function toExpression(value) {
32-
return new RegExp('^(' + value + ')$', 'i');
33-
}
34-
35-
/**
36-
* Wrap an expression into an assertion function.
37-
*
38-
* @param {RegExp} expression - Expression to test.
39-
* @return {Function} - Assertion.
40-
*/
41-
function wrapExpression(expression) {
42-
/**
43-
* Assert `value` matches the bound `expression`.
44-
*
45-
* @param {string} value - Value to check.
46-
* @return {boolean} - Whether `value` matches.
47-
*/
48-
function assertion(value) {
49-
return expression.test(value);
50-
}
51-
52-
return assertion;
53-
}
12+
/* Search `node` for `heading` and invoke `callback`. */
13+
function headingRange(node, heading, callback) {
14+
var test = heading;
5415

55-
/**
56-
* Check if `node` is a heading.
57-
*
58-
* @param {Node} node - Node to check.
59-
* @return {boolean} - Whether `node` is a heading.
60-
*/
61-
function isHeading(node) {
62-
return node && node.type === 'heading';
63-
}
16+
if (typeof test === 'string') {
17+
test = toExpression(test);
18+
}
6419

65-
/**
66-
* Check if `node` is the main heading.
67-
*
68-
* @param {Node} node - Node to check.
69-
* @param {number?} depth - Depth to search for.
70-
* @param {function(string): boolean} test - Tester.
71-
* @return {boolean} - Whether `node` is an opening
72-
* heading.
73-
*/
74-
function isOpeningHeading(node, depth, test) {
75-
return depth === null && isHeading(node) && test(toString(node), node);
76-
}
20+
if ('test' in test) {
21+
test = wrapExpression(test);
22+
}
7723

78-
/**
79-
* Check if `node` is the next heading.
80-
*
81-
* @param {Node} node - Node to check.
82-
* @param {number?} depth - Depth of the opening heading.
83-
* @return {boolean} - Whether `node` is a closing
84-
* heading.
85-
*/
86-
function isClosingHeading(node, depth) {
87-
return depth && isHeading(node) && node.depth <= depth;
24+
search(node, test, callback);
8825
}
8926

90-
/**
91-
* Search a node for heading range.
92-
*
93-
* @param {Node} root - Node to search.
94-
* @param {Function} test - Assertion.
95-
* @param {Function} callback - Callback invoked when
96-
* found.
97-
*/
27+
/* Search a node for heading range. */
9828
function search(root, test, callback) {
99-
var index = -1;
100-
var children = root.children;
101-
var length = children.length;
102-
var depth = null;
103-
var start = null;
104-
var end = null;
105-
var nodes;
106-
var clean;
107-
var child;
29+
var index = -1;
30+
var children = root.children;
31+
var length = children.length;
32+
var depth = null;
33+
var start = null;
34+
var end = null;
35+
var nodes;
36+
var clean;
37+
var child;
38+
39+
while (++index < length) {
40+
child = children[index];
41+
42+
if (isClosingHeading(child, depth)) {
43+
end = index;
44+
break;
45+
}
10846

109-
while (++index < length) {
110-
child = children[index];
47+
if (isOpeningHeading(child, depth, test)) {
48+
start = index;
49+
depth = child.depth;
50+
}
51+
}
11152

112-
if (isClosingHeading(child, depth)) {
113-
end = index;
114-
break;
115-
}
53+
if (start !== null) {
54+
if (end === null) {
55+
end = length + 1;
56+
}
11657

117-
if (isOpeningHeading(child, depth, test)) {
118-
start = index;
119-
depth = child.depth;
120-
}
58+
nodes = callback(
59+
children[start],
60+
children.slice(start + 1, end),
61+
children[end],
62+
{
63+
parent: root,
64+
start: start,
65+
end: end in children ? end : null
66+
}
67+
);
68+
69+
clean = [];
70+
index = -1;
71+
length = nodes && nodes.length;
72+
73+
/* Ensure no empty nodes are inserted. This could
74+
* be the case if `end` is in `nodes` but no `end`
75+
* node exists. */
76+
while (++index < length) {
77+
if (nodes[index]) {
78+
clean.push(nodes[index]);
79+
}
12180
}
12281

123-
if (start !== null) {
124-
if (end === null) {
125-
end = length + 1;
126-
}
127-
128-
nodes = callback(
129-
children[start],
130-
children.slice(start + 1, end),
131-
children[end],
132-
{
133-
'parent': root,
134-
'start': start,
135-
'end': end in children ? end : null
136-
}
137-
);
138-
139-
clean = [];
140-
index = -1;
141-
length = nodes && nodes.length;
142-
143-
/*
144-
* Ensure no empty nodes are inserted. This could
145-
* be the case if `end` is in `nodes` but no `end`
146-
* node exists.
147-
*/
148-
149-
while (++index < length) {
150-
if (nodes[index]) {
151-
clean.push(nodes[index]);
152-
}
153-
}
154-
155-
if (nodes) {
156-
splice.apply(children, [start, end - start + 1].concat(clean));
157-
}
82+
if (nodes) {
83+
splice.apply(children, [start, end - start + 1].concat(clean));
15884
}
85+
}
15986
}
16087

161-
/**
162-
* Search `node` for `heading` and invoke `callback`.
163-
*
164-
* @param {Node} node - Node to search in.
165-
* @param {string|RegExp|Function} heading - Heading to
166-
* search for.
167-
* @param {Function} callback - Callback invoked when
168-
* found.
169-
*/
170-
function headingRange(node, heading, callback) {
171-
var test = heading;
88+
/* Transform a string into an applicable expression. */
89+
function toExpression(value) {
90+
return new RegExp('^(' + value + ')$', 'i');
91+
}
17292

173-
if (typeof test === 'string') {
174-
test = toExpression(test);
175-
}
93+
/* Wrap an expression into an assertion function. */
94+
function wrapExpression(expression) {
95+
return assertion;
17696

177-
if ('test' in test) {
178-
test = wrapExpression(test);
179-
}
97+
/* Assert `value` matches the bound `expression`. */
98+
function assertion(value) {
99+
return expression.test(value);
100+
}
101+
}
180102

181-
search(node, test, callback);
103+
/* Check if `node` is a heading. */
104+
function isHeading(node) {
105+
return node && node.type === 'heading';
182106
}
183107

184-
/*
185-
* Expose.
186-
*/
108+
/* Check if `node` is the main heading. */
109+
function isOpeningHeading(node, depth, test) {
110+
return depth === null && isHeading(node) && test(toString(node), node);
111+
}
187112

188-
module.exports = headingRange;
113+
/* Check if `node` is the next heading. */
114+
function isClosingHeading(node, depth) {
115+
return depth && isHeading(node) && node.depth <= depth;
116+
}

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
],
2727
"devDependencies": {
2828
"browserify": "^13.0.0",
29-
"eslint": "^2.0.0",
3029
"esmangle": "^1.0.0",
3130
"istanbul": "^0.4.0",
32-
"jscs": "^3.0.0",
33-
"jscs-jsdoc": "^2.0.0",
3431
"remark": "^5.0.0",
3532
"remark-cli": "^1.0.0",
3633
"remark-comment-config": "^4.0.0",
@@ -39,18 +36,23 @@
3936
"remark-toc": "^3.0.0",
4037
"remark-usage": "^4.0.0",
4138
"remark-validate-links": "^4.0.0",
42-
"tape": "^4.4.0"
39+
"tape": "^4.4.0",
40+
"xo": "^0.17.1"
4341
},
4442
"scripts": {
4543
"build-md": "remark . --quiet --frail",
4644
"build-bundle": "browserify index.js --no-builtins -s mdastUtilHeadingRange > mdast-util-heading-range.js",
4745
"build-mangle": "esmangle mdast-util-heading-range.js > mdast-util-heading-range.min.js",
4846
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
49-
"lint-api": "eslint .",
50-
"lint-style": "jscs --reporter inline .",
51-
"lint": "npm run lint-api && npm run lint-style",
47+
"lint": "xo",
5248
"test-api": "node test.js",
5349
"test-coverage": "istanbul cover test.js",
5450
"test": "npm run build && npm run lint && npm run test-coverage"
51+
},
52+
"xo": {
53+
"space": true,
54+
"ignore": [
55+
"mdast-util-heading-range.js"
56+
]
5557
}
5658
}

0 commit comments

Comments
 (0)