Skip to content

Commit aac9e5f

Browse files
committed
Refactor code-style
1 parent 99a30d5 commit aac9e5f

File tree

5 files changed

+175
-169
lines changed

5 files changed

+175
-169
lines changed

.prettierignore

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

index.js

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

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

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

77
var single = {
88
'-': true, // Hyphen-minus
99
'–': true, // En dash
1010
'—': true, // Em dash
1111
':': true, // Colon
1212
';': true // Semi-colon
13-
};
13+
}
1414

1515
/* Pair delimiters. From common sense, and wikipedia:
1616
* Mostly from https://en.wikipedia.org/wiki/Quotation_mark. */
@@ -30,8 +30,8 @@ var pairs = {
3030
'"': {
3131
'"': true
3232
},
33-
'\'': {
34-
'\'': true
33+
"'": {
34+
"'": true
3535
},
3636
'‘': {
3737
'’': true
@@ -80,122 +80,123 @@ var pairs = {
8080
'「': {
8181
'」': true
8282
}
83-
};
83+
}
8484

8585
/* Check if the node in `parent` at `position` is enclosed
8686
* by matching delimiters. */
8787
function isLiteral(parent, index) {
8888
if (!(parent && parent.children)) {
89-
throw new Error('Parent must be a node');
89+
throw new Error('Parent must be a node')
9090
}
9191

9292
if (isNaN(index)) {
93-
throw new Error('Index must be a number');
93+
throw new Error('Index must be a number')
9494
}
9595

9696
if (
9797
(!hasWordsBefore(parent, index) && nextDelimiter(parent, index, single)) ||
98-
(!hasWordsAfter(parent, index) && previousDelimiter(parent, index, single)) ||
98+
(!hasWordsAfter(parent, index) &&
99+
previousDelimiter(parent, index, single)) ||
99100
isWrapped(parent, index, pairs)
100101
) {
101-
return true;
102+
return true
102103
}
103104

104-
return false;
105+
return false
105106
}
106107

107108
/* Check if the node in `parent` at `position` is enclosed
108109
* by matching delimiters. */
109110
function isWrapped(parent, position, delimiters) {
110-
var prev = previousDelimiter(parent, position, delimiters);
111-
var next;
111+
var prev = previousDelimiter(parent, position, delimiters)
112+
var next
112113

113114
if (prev) {
114-
next = nextDelimiter(parent, position, delimiters[toString(prev)]);
115+
next = nextDelimiter(parent, position, delimiters[toString(prev)])
115116
}
116117

117-
return Boolean(next);
118+
return Boolean(next)
118119
}
119120

120121
/* Find the previous delimiter before `position` in
121122
* `parent`. Returns the delimiter node when found. */
122123
function previousDelimiter(parent, position, delimiters) {
123-
var siblings = parent.children;
124-
var index = position;
125-
var result;
124+
var siblings = parent.children
125+
var index = position
126+
var result
126127

127128
while (index--) {
128-
result = delimiterCheck(siblings[index], delimiters);
129+
result = delimiterCheck(siblings[index], delimiters)
129130

130131
if (result === null) {
131-
continue;
132+
continue
132133
}
133134

134-
return result;
135+
return result
135136
}
136137

137-
return null;
138+
return null
138139
}
139140

140141
/* Find the next delimiter after `position` in
141142
* `parent`. Returns the delimiter node when found. */
142143
function nextDelimiter(parent, position, delimiters) {
143-
var siblings = parent.children;
144-
var index = position;
145-
var length = siblings.length;
146-
var result;
144+
var siblings = parent.children
145+
var index = position
146+
var length = siblings.length
147+
var result
147148

148149
while (++index < length) {
149-
result = delimiterCheck(siblings[index], delimiters);
150+
result = delimiterCheck(siblings[index], delimiters)
150151

151152
if (result === null) {
152-
continue;
153+
continue
153154
}
154155

155-
return result;
156+
return result
156157
}
157158

158-
return null;
159+
return null
159160
}
160161

161162
/* Check if `node` is in `delimiters`. */
162163
function delimiterCheck(node, delimiters) {
163-
var type = node.type;
164+
var type = node.type
164165

165166
if (type === 'WordNode' || type === 'SourceNode') {
166-
return false;
167+
return false
167168
}
168169

169170
if (type === 'WhiteSpaceNode') {
170-
return null;
171+
return null
171172
}
172173

173-
return toString(node) in delimiters ? node : false;
174+
return toString(node) in delimiters ? node : false
174175
}
175176

176177
/* Check if there are word nodes before `position`
177178
* in `parent`. */
178179
function hasWordsBefore(parent, position) {
179-
return containsWord(parent, 0, position);
180+
return containsWord(parent, 0, position)
180181
}
181182

182183
/* Check if there are word nodes before `position`
183184
* in `parent`. */
184185
function hasWordsAfter(parent, position) {
185-
return containsWord(parent, position + 1, parent.children.length);
186+
return containsWord(parent, position + 1, parent.children.length)
186187
}
187188

188189
/* Check if parent contains word-nodes between
189190
* `start` and `end`. */
190191
function containsWord(parent, start, end) {
191-
var siblings = parent.children;
192-
var index = start - 1;
192+
var siblings = parent.children
193+
var index = start - 1
193194

194195
while (++index < end) {
195196
if (siblings[index].type === 'WordNode') {
196-
return true;
197+
return true
197198
}
198199
}
199200

200-
return false;
201+
return false
201202
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"browserify": "^16.0.0",
2828
"esmangle": "^1.0.0",
2929
"nyc": "^11.0.0",
30+
"prettier": "^1.12.1",
3031
"remark-cli": "^5.0.0",
3132
"remark-preset-wooorm": "^4.0.0",
3233
"retext": "^5.0.0",
@@ -35,23 +36,30 @@
3536
"xo": "^0.20.0"
3637
},
3738
"scripts": {
38-
"build-md": "remark . --quiet --frail --output",
39+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3940
"build-bundle": "browserify index.js --bare -s nlcstIsLiteral > nlcst-is-literal.js",
4041
"build-mangle": "esmangle < nlcst-is-literal.js > nlcst-is-literal.min.js",
41-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
42-
"lint": "xo",
42+
"build": "npm run build-bundle && npm run build-mangle",
4343
"test-api": "node test",
4444
"test-coverage": "nyc --reporter lcov tape test.js",
45-
"test": "npm run build && npm run lint && npm run test-coverage"
45+
"test": "npm run format && npm run build && npm run test-coverage"
4646
},
4747
"nyc": {
4848
"check-coverage": true,
4949
"lines": 100,
5050
"functions": 100,
5151
"branches": 100
5252
},
53+
"prettier": {
54+
"tabWidth": 2,
55+
"useTabs": false,
56+
"singleQuote": true,
57+
"bracketSpacing": false,
58+
"semi": false,
59+
"trailingComma": "none"
60+
},
5361
"xo": {
54-
"space": true,
62+
"prettier": true,
5563
"esnext": false,
5664
"rules": {
5765
"guard-for-in": "off",

readme.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ The word — quux — is meant as a literal.
3333
And our script, `example.js`, looks as follows:
3434

3535
```javascript
36-
var vfile = require('to-vfile');
37-
var unified = require('unified');
38-
var english = require('retext-english');
39-
var visit = require('unist-util-visit');
40-
var toString = require('nlcst-to-string');
41-
var literal = require('nlcst-is-literal');
36+
var vfile = require('to-vfile')
37+
var unified = require('unified')
38+
var english = require('retext-english')
39+
var visit = require('unist-util-visit')
40+
var toString = require('nlcst-to-string')
41+
var literal = require('nlcst-is-literal')
4242

43-
var file = vfile.readSync('example.txt');
44-
var tree = unified().use(english).parse(file);
43+
var file = vfile.readSync('example.txt')
44+
var tree = unified()
45+
.use(english)
46+
.parse(file)
4547

46-
visit(tree, 'WordNode', visitor);
48+
visit(tree, 'WordNode', visitor)
4749

4850
function visitor(node, index, parent) {
4951
if (literal(parent, index)) {
50-
console.log(toString(node));
52+
console.log(toString(node))
5153
}
5254
}
5355
```

0 commit comments

Comments
 (0)