Skip to content

Commit 07effb3

Browse files
committed
Refactor to shout less
1 parent 035a9cf commit 07effb3

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ var whitespace = require('hast-util-whitespace')
99
var textContent = require('hast-util-to-string')
1010
var is = require('hast-util-is-element')
1111

12-
module.exports = toNLCST
12+
module.exports = toNlcst
1313

14-
// Elements representing source.
15-
var SOURCE = ['code']
16-
var IGNORE = ['script', 'style', 'svg', 'math', 'del']
17-
var EXPLICIT = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
14+
var source = ['code']
15+
var ignore = ['script', 'style', 'svg', 'math', 'del']
16+
var explicit = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
1817

19-
// Constants.
20-
var FLOW_ACCEPTING = [
18+
var flowAccepting = [
2119
'body',
2220
'article',
2321
'section',
@@ -43,8 +41,8 @@ var FLOW_ACCEPTING = [
4341
'dialog'
4442
]
4543

46-
// Transform `tree` to `nlcst`.
47-
function toNLCST(tree, file, Parser) {
44+
// Transform `tree` to nlcst.
45+
function toNlcst(tree, file, Parser) {
4846
var parser
4947
var location
5048
var results
@@ -72,8 +70,8 @@ function toNLCST(tree, file, Parser) {
7270
doc = String(file)
7371
parser = 'parse' in Parser ? Parser : new Parser()
7472

75-
// Transform HAST into NLCST tokens, and pass these into `parser.parse` to
76-
// insert sentences, paragraphs where needed.
73+
// Transform hast to nlcst, and pass these into `parser.parse` to insert
74+
// sentences, paragraphs where needed.
7775
results = []
7876

7977
find(tree)
@@ -93,12 +91,11 @@ function toNLCST(tree, file, Parser) {
9391
if (node.type === 'root') {
9492
findAll(children)
9593
} else if (is(node) && !ignored(node)) {
96-
if (is(node, EXPLICIT)) {
94+
if (is(node, explicit)) {
9795
// Explicit paragraph.
9896
add(node)
99-
} else if (is(node, FLOW_ACCEPTING)) {
100-
// Slightly simplified version of:
101-
// https://html.spec.whatwg.org/#paragraphs
97+
} else if (is(node, flowAccepting)) {
98+
// Slightly simplified version of: <https://html.spec.whatwg.org/#paragraphs>.
10299
implicit(flattenAll(children))
103100
} else {
104101
// Dig deeper.
@@ -210,7 +207,7 @@ function toNLCST(tree, file, Parser) {
210207
return patch(replacement, location, location.toOffset(position.start(node)))
211208
}
212209

213-
// Convert all `children` (HAST) to NLCST.
210+
// Convert all `children` (hast) to nlcst.
214211
function all(children) {
215212
var length = children && children.length
216213
var index = -1
@@ -228,10 +225,10 @@ function toNLCST(tree, file, Parser) {
228225
return result
229226
}
230227

231-
// Patch a position on each node in `nodes`. `offset` is the offset in
232-
// `file` this run of content starts at.
228+
// Patch a position on each node in `nodes`.
229+
// `offset` is the offset in `file` this run of content starts at.
233230
//
234-
// Note that NLCST nodes are concrete, meaning that their starting and ending
231+
// Note that nlcst nodes are concrete, meaning that their starting and ending
235232
// positions can be inferred from their content.
236233
function patch(nodes, location, offset) {
237234
var length = nodes.length
@@ -265,10 +262,10 @@ function toNLCST(tree, file, Parser) {
265262

266263
function sourced(node) {
267264
var props = node.properties
268-
return is(node) && (is(node, SOURCE) || props.dataNlcst === 'source')
265+
return is(node) && (is(node, source) || props.dataNlcst === 'source')
269266
}
270267

271268
function ignored(node) {
272269
var props = node.properties
273-
return is(node) && (is(node, IGNORE) || props.dataNlcst === 'ignore')
270+
return is(node) && (is(node, ignore) || props.dataNlcst === 'ignore')
274271
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hast-util-to-nlcst",
33
"version": "1.2.4",
4-
"description": "Transform HAST to NLCST",
4+
"description": "Transform hast to nlcst",
55
"license": "MIT",
66
"keywords": [
77
"hast",
@@ -50,8 +50,8 @@
5050
},
5151
"scripts": {
5252
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
53-
"build-bundle": "browserify . -s hastUtilToNLCST > hast-util-to-nlcst.js",
54-
"build-mangle": "browserify . -s hastUtilToNLCST -p tinyify > hast-util-to-nlcst.min.js",
53+
"build-bundle": "browserify . -s hastUtilToNlcst > hast-util-to-nlcst.js",
54+
"build-mangle": "browserify . -s hastUtilToNlcst -p tinyify > hast-util-to-nlcst.min.js",
5555
"build": "npm run build-bundle && npm run build-mangle",
5656
"test-api": "node test",
5757
"test-coverage": "nyc --reporter lcov tape test/index.js",

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ var rehype = require('rehype')
3939
var vfile = require('to-vfile')
4040
var English = require('parse-english')
4141
var inspect = require('unist-util-inspect')
42-
var toNLCST = require('hast-util-to-nlcst')
42+
var toNlcst = require('hast-util-to-nlcst')
4343

4444
var file = vfile.readSync('example.html')
4545
var tree = rehype().parse(file)
4646

47-
console.log(inspect(toNLCST(tree, file, English)))
47+
console.log(inspect(toNlcst(tree, file, English)))
4848
```
4949

5050
Which, when running, yields:

test/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,67 @@ var Dutch = require('parse-dutch')
1010
var English = require('parse-english')
1111
var negate = require('negate')
1212
var hidden = require('is-hidden')
13-
var toNLCST = require('..')
13+
var toNlcst = require('..')
1414

1515
test('hast-util-to-nlcst', function(t) {
1616
t.throws(
1717
function() {
18-
toNLCST()
18+
toNlcst()
1919
},
2020
/hast-util-to-nlcst expected node/,
21-
'should fail when not given an AST'
21+
'should fail when not given a tree'
2222
)
2323

2424
t.throws(
2525
function() {
26-
toNLCST({})
26+
toNlcst({})
2727
},
2828
/hast-util-to-nlcst expected node/,
29-
'should fail when not given an AST (#2)'
29+
'should fail when not given a tree (#2)'
3030
)
3131

3232
t.throws(
3333
function() {
34-
toNLCST({type: 'foo'})
34+
toNlcst({type: 'foo'})
3535
},
3636
/hast-util-to-nlcst expected file/,
3737
'should fail when not given a file'
3838
)
3939

4040
t.throws(
4141
function() {
42-
toNLCST({type: 'foo'})
42+
toNlcst({type: 'foo'})
4343
},
4444
/hast-util-to-nlcst expected file/,
4545
'should fail when not given a file (#2)'
4646
)
4747

4848
t.throws(
4949
function() {
50-
toNLCST({type: 'text', value: 'foo'}, {foo: 'bar'})
50+
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
5151
},
5252
/hast-util-to-nlcst expected file/,
5353
'should fail when not given a file (#3)'
5454
)
5555

5656
t.throws(
5757
function() {
58-
toNLCST({type: 'text', value: 'foo'}, vfile('foo'))
58+
toNlcst({type: 'text', value: 'foo'}, vfile('foo'))
5959
},
6060
/hast-util-to-nlcst expected parser/,
6161
'should fail without parser'
6262
)
6363

6464
t.throws(
6565
function() {
66-
toNLCST({type: 'text', value: 'foo'}, vfile(), Latin)
66+
toNlcst({type: 'text', value: 'foo'}, vfile(), Latin)
6767
},
6868
/hast-util-to-nlcst expected position on nodes/,
6969
'should fail when not given positional information'
7070
)
7171

7272
t.doesNotThrow(function() {
73-
toNLCST(
73+
toNlcst(
7474
{
7575
type: 'text',
7676
value: 'foo',
@@ -85,7 +85,7 @@ test('hast-util-to-nlcst', function(t) {
8585
}, 'should accept a parser constructor')
8686

8787
t.doesNotThrow(function() {
88-
toNLCST(
88+
toNlcst(
8989
{
9090
type: 'text',
9191
value: 'foo',
@@ -101,7 +101,7 @@ test('hast-util-to-nlcst', function(t) {
101101

102102
t.throws(
103103
function() {
104-
toNLCST(
104+
toNlcst(
105105
{
106106
type: 'text',
107107
value: 'foo',
@@ -116,7 +116,7 @@ test('hast-util-to-nlcst', function(t) {
116116
)
117117

118118
t.test('should accept nodes without offsets', function(st) {
119-
var node = toNLCST(
119+
var node = toNlcst(
120120
{
121121
type: 'text',
122122
value: 'foo',
@@ -147,7 +147,7 @@ test('Fixtures', function(t) {
147147
var input = path.join(root, fixture, 'input.html')
148148
var output = path.join(root, fixture, 'output.json')
149149
var file = vfile(fs.readFileSync(input))
150-
var actual = toNLCST(rehype().parse(file), file, Latin)
150+
var actual = toNlcst(rehype().parse(file), file, Latin)
151151
var expected
152152

153153
try {

0 commit comments

Comments
 (0)