Skip to content

Commit 57b7c0e

Browse files
committed
Refactor code-style
1 parent 821bbc0 commit 57b7c0e

File tree

5 files changed

+235
-224
lines changed

5 files changed

+235
-224
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-to-nlcst.js
3+
mdast-util-to-nlcst.min.js

index.js

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
1-
'use strict';
1+
'use strict'
22

3-
var repeat = require('repeat-string');
4-
var vfileLocation = require('vfile-location');
5-
var position = require('unist-util-position');
6-
var toString = require('nlcst-to-string');
3+
var repeat = require('repeat-string')
4+
var vfileLocation = require('vfile-location')
5+
var position = require('unist-util-position')
6+
var toString = require('nlcst-to-string')
77

8-
module.exports = toNLCST;
8+
module.exports = toNLCST
99

10-
var ignore = [
11-
'table',
12-
'tableRow',
13-
'tableCell'
14-
];
10+
var ignore = ['table', 'tableRow', 'tableCell']
1511

16-
var source = [
17-
'inlineCode'
18-
];
12+
var source = ['inlineCode']
1913

20-
var newline = '\n';
14+
var newline = '\n'
2115

22-
/* Transform `tree` into `nlcst`. */
16+
// Transform `tree` into `nlcst`.
2317
function toNLCST(tree, file, Parser, options) {
24-
var settings = options || {};
25-
var parser;
18+
var settings = options || {}
19+
var parser
2620

27-
/* Warn for invalid parameters. */
21+
// Warn for invalid parameters.
2822
if (!tree || !tree.type) {
29-
throw new Error('mdast-util-to-nlcst expected node');
23+
throw new Error('mdast-util-to-nlcst expected node')
3024
}
3125

3226
if (!file || !file.messages) {
33-
throw new Error('mdast-util-to-nlcst expected file');
27+
throw new Error('mdast-util-to-nlcst expected file')
3428
}
3529

36-
/* Construct parser. */
30+
// Construct parser.
3731
if (!Parser) {
38-
throw new Error('mdast-util-to-nlcst expected parser');
32+
throw new Error('mdast-util-to-nlcst expected parser')
3933
}
4034

4135
if (
@@ -44,129 +38,135 @@ function toNLCST(tree, file, Parser, options) {
4438
!tree.position.start.column ||
4539
!tree.position.start.line
4640
) {
47-
throw new Error('mdast-util-to-nlcst expected position on nodes');
41+
throw new Error('mdast-util-to-nlcst expected position on nodes')
4842
}
4943

50-
parser = 'parse' in Parser ? Parser : new Parser();
51-
52-
/* Transform mdast into NLCST tokens, and pass these
53-
* into `parser.parse` to insert sentences, paragraphs
54-
* where needed. */
55-
return parser.parse(one({
56-
doc: String(file),
57-
location: vfileLocation(file),
58-
parser: parser,
59-
ignore: ignore.concat(settings.ignore || []),
60-
source: source.concat(settings.source || [])
61-
}, tree));
44+
parser = 'parse' in Parser ? Parser : new Parser()
45+
46+
// Transform mdast into NLCST tokens, and pass these into `parser.parse` to
47+
// insert sentences, paragraphs where needed.
48+
return parser.parse(
49+
one(
50+
{
51+
doc: String(file),
52+
location: vfileLocation(file),
53+
parser: parser,
54+
ignore: ignore.concat(settings.ignore || []),
55+
source: source.concat(settings.source || [])
56+
},
57+
tree
58+
)
59+
)
6260
}
6361

64-
/* Convert `node` into NLCST. */
62+
// Convert `node` into NLCST.
6563
function one(config, node) {
66-
var offset = config.location.toOffset;
67-
var parser = config.parser;
68-
var doc = config.doc;
69-
var type = node.type;
70-
var start = offset(position.start(node));
71-
var end = offset(position.end(node));
64+
var offset = config.location.toOffset
65+
var parser = config.parser
66+
var doc = config.doc
67+
var type = node.type
68+
var start = offset(position.start(node))
69+
var end = offset(position.end(node))
7270

7371
if (config.ignore.indexOf(type) === -1) {
7472
if (config.source.indexOf(type) !== -1) {
75-
return patch(config, [parser.tokenizeSource(doc.slice(start, end))], start);
73+
return patch(
74+
config,
75+
[parser.tokenizeSource(doc.slice(start, end))],
76+
start
77+
)
7678
}
7779

7880
if (node.children) {
79-
return all(config, node);
81+
return all(config, node)
8082
}
8183

8284
if (type === 'image' || type === 'imageReference') {
83-
return patch(config, parser.tokenize(node.alt), start + 2);
85+
return patch(config, parser.tokenize(node.alt), start + 2)
8486
}
8587

8688
if (type === 'text' || type === 'escape') {
87-
return patch(config, parser.tokenize(node.value), start);
89+
return patch(config, parser.tokenize(node.value), start)
8890
}
8991

9092
if (node.type === 'break') {
91-
return patch(config, [parser.tokenizeWhiteSpace('\n')], start);
93+
return patch(config, [parser.tokenizeWhiteSpace('\n')], start)
9294
}
9395
}
9496

95-
return null;
97+
return null
9698
}
9799

98-
/* Convert all nodes in `parent` (mdast) into NLCST. */
100+
// Convert all nodes in `parent` (mdast) into NLCST.
99101
function all(config, parent) {
100-
var children = parent.children;
101-
var length = children && children.length;
102-
var index = -1;
103-
var result = [];
104-
var child;
105-
var node;
106-
var pos;
107-
var prevEndLine;
108-
var prevOffset;
109-
var endLine;
102+
var children = parent.children
103+
var length = children && children.length
104+
var index = -1
105+
var result = []
106+
var child
107+
var node
108+
var pos
109+
var prevEndLine
110+
var prevOffset
111+
var endLine
110112

111113
while (++index < length) {
112-
node = children[index];
113-
pos = node.position;
114-
endLine = position.start(node).line;
114+
node = children[index]
115+
pos = node.position
116+
endLine = position.start(node).line
115117

116118
if (prevEndLine && endLine !== prevEndLine) {
117-
child = config.parser.tokenizeWhiteSpace(repeat(newline, endLine - prevEndLine));
118-
patch(config, [child], prevOffset);
119+
child = config.parser.tokenizeWhiteSpace(
120+
repeat(newline, endLine - prevEndLine)
121+
)
122+
patch(config, [child], prevOffset)
119123

120124
if (child.value.length < 2) {
121-
child.value = repeat(newline, 2);
125+
child.value = repeat(newline, 2)
122126
}
123127

124-
result.push(child);
128+
result.push(child)
125129
}
126130

127-
child = one(config, node);
131+
child = one(config, node)
128132

129133
if (child) {
130-
result = result.concat(child);
134+
result = result.concat(child)
131135
}
132136

133-
pos = position.end(node);
134-
prevEndLine = pos.line;
135-
prevOffset = pos.offset;
137+
pos = position.end(node)
138+
prevEndLine = pos.line
139+
prevOffset = pos.offset
136140
}
137141

138-
return result;
142+
return result
139143
}
140144

141-
/* Patch a position on each node in `nodes`.
142-
* `offset` is the offset in `file` this run of content
143-
* starts at. */
145+
// Patch a position on each node in `nodes`.
146+
// `offset` is the offset in `file` this run of content starts at.
144147
function patch(config, nodes, offset) {
145-
var position = config.location.toPosition;
146-
var length = nodes.length;
147-
var index = -1;
148-
var start = offset;
149-
var children;
150-
var node;
151-
var end;
148+
var position = config.location.toPosition
149+
var length = nodes.length
150+
var index = -1
151+
var start = offset
152+
var children
153+
var node
154+
var end
152155

153156
while (++index < length) {
154-
node = nodes[index];
155-
children = node.children;
157+
node = nodes[index]
158+
children = node.children
156159

157160
if (children) {
158-
patch(config, children, start);
161+
patch(config, children, start)
159162
}
160163

161-
end = start + toString(node).length;
164+
end = start + toString(node).length
162165

163-
node.position = {
164-
start: position(start),
165-
end: position(end)
166-
};
166+
node.position = {start: position(start), end: position(end)}
167167

168-
start = end;
168+
start = end
169169
}
170170

171-
return nodes;
171+
return nodes
172172
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"parse-dutch": "^4.0.0",
3737
"parse-english": "^4.0.0",
3838
"parse-latin": "^4.0.0",
39+
"prettier": "^1.14.3",
3940
"remark": "^10.0.0",
4041
"remark-cli": "^6.0.0",
4142
"remark-frontmatter": "^1.0.0",
@@ -45,23 +46,30 @@
4546
"xo": "^0.23.0"
4647
},
4748
"scripts": {
48-
"build-md": "remark *.md -qfo",
49+
"format": "remark *.md -qfo && prettier --write '**/*.js' && xo --fix",
4950
"build-bundle": "browserify index.js --bare -s mdastUtilToNLCST > mdast-util-to-nlcst.js",
5051
"build-mangle": "esmangle mdast-util-to-nlcst.js > mdast-util-to-nlcst.min.js",
51-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
52-
"lint": "xo",
52+
"build": "npm run build-bundle && npm run build-mangle",
5353
"test-api": "node test",
5454
"test-coverage": "nyc --reporter lcov tape test/index.js",
55-
"test": "npm run build && npm run lint && npm run test-coverage"
55+
"test": "npm run format && npm run build && npm run test-coverage"
5656
},
5757
"nyc": {
5858
"check-coverage": true,
5959
"lines": 100,
6060
"functions": 100,
6161
"branches": 100
6262
},
63+
"prettier": {
64+
"tabWidth": 2,
65+
"useTabs": false,
66+
"singleQuote": true,
67+
"bracketSpacing": false,
68+
"semi": false,
69+
"trailingComma": "none"
70+
},
6371
"xo": {
64-
"space": true,
72+
"prettier": true,
6573
"esnext": false,
6674
"rules": {
6775
"max-params": "off"

readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ npm install mdast-util-to-nlcst
1515
## Usage
1616

1717
```javascript
18-
var toNLCST = require('mdast-util-to-nlcst');
19-
var inspect = require('unist-util-inspect');
20-
var English = require('parse-english');
21-
var remark = require('remark');
22-
var vfile = require('vfile');
18+
var toNLCST = require('mdast-util-to-nlcst')
19+
var inspect = require('unist-util-inspect')
20+
var English = require('parse-english')
21+
var remark = require('remark')
22+
var vfile = require('vfile')
2323

24-
var file = vfile('Some *foo*sball.');
25-
var tree = remark().parse(file);
24+
var file = vfile('Some *foo*sball.')
25+
var tree = remark().parse(file)
2626

27-
var nlcst = toNLCST(tree, file, English);
27+
var nlcst = toNLCST(tree, file, English)
2828

29-
console.log(inspect(nlcst));
29+
console.log(inspect(nlcst))
3030
```
3131

3232
Yields:

0 commit comments

Comments
 (0)