Skip to content

Commit 432c1f1

Browse files
committed
Refactor code-style
1 parent a877a58 commit 432c1f1

File tree

5 files changed

+164
-140
lines changed

5 files changed

+164
-140
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
hast-util-find-and-replace.js
3+
hast-util-find-and-replace.min.js

index.js

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

3-
var array = require('isarray');
4-
var regexp = require('is-regex');
5-
var visit = require('unist-util-visit-parents');
6-
var is = require('hast-util-is-element');
7-
var escape = require('escape-string-regexp');
3+
var array = require('isarray')
4+
var regexp = require('is-regex')
5+
var visit = require('unist-util-visit-parents')
6+
var is = require('hast-util-is-element')
7+
var escape = require('escape-string-regexp')
88

9-
var IGNORE = ['title', 'script', 'style', 'svg', 'math'];
9+
var IGNORE = ['title', 'script', 'style', 'svg', 'math']
1010

11-
module.exports = findAndReplace;
11+
module.exports = findAndReplace
1212

13-
findAndReplace.ignore = IGNORE;
13+
findAndReplace.ignore = IGNORE
1414

1515
function findAndReplace(tree, find, replace, options) {
16-
var settings;
17-
var schema;
16+
var settings
17+
var schema
1818

1919
if (typeof find === 'string' || regexp(find)) {
20-
schema = [[find, replace]];
20+
schema = [[find, replace]]
2121
} else {
22-
schema = find;
23-
options = replace;
22+
schema = find
23+
options = replace
2424
}
2525

26-
settings = options || {};
26+
settings = options || {}
2727

28-
search(tree, settings, handlerFactory(toPairs(schema)));
28+
search(tree, settings, handlerFactory(toPairs(schema)))
2929

30-
return tree;
30+
return tree
3131

3232
function handlerFactory(pairs) {
33-
var pair = pairs[0];
33+
var pair = pairs[0]
3434

35-
return handler;
35+
return handler
3636

3737
function handler(node, parent) {
38-
var siblings = parent.children;
39-
var pos = siblings.indexOf(node);
40-
var value = node.value;
41-
var find = pair[0];
42-
var replace = pair[1];
43-
var lastIndex = 0;
44-
var nodes = [];
45-
var subvalue;
46-
var index;
47-
var length;
48-
var match;
49-
var subhandler;
50-
51-
find.lastIndex = 0;
52-
53-
match = find.exec(value);
38+
var siblings = parent.children
39+
var pos = siblings.indexOf(node)
40+
var value = node.value
41+
var find = pair[0]
42+
var replace = pair[1]
43+
var lastIndex = 0
44+
var nodes = []
45+
var subvalue
46+
var index
47+
var length
48+
var match
49+
var subhandler
50+
51+
find.lastIndex = 0
52+
53+
match = find.exec(value)
5454

5555
while (match) {
56-
index = match.index;
57-
subvalue = value.slice(lastIndex, index);
56+
index = match.index
57+
subvalue = value.slice(lastIndex, index)
5858

5959
if (subvalue) {
60-
nodes.push({type: 'text', value: subvalue});
60+
nodes.push({type: 'text', value: subvalue})
6161
}
6262

63-
subvalue = replace.apply(null, match);
63+
subvalue = replace.apply(null, match)
6464

6565
if (subvalue) {
6666
if (typeof subvalue === 'string') {
67-
subvalue = {type: 'text', value: subvalue};
67+
subvalue = {type: 'text', value: subvalue}
6868
}
6969

70-
nodes.push(subvalue);
70+
nodes.push(subvalue)
7171
}
7272

73-
lastIndex = index + match[0].length;
73+
lastIndex = index + match[0].length
7474

7575
if (!find.global) {
76-
break;
76+
break
7777
}
7878

79-
match = find.exec(value);
79+
match = find.exec(value)
8080
}
8181

8282
if (index === undefined) {
83-
nodes = [node];
83+
nodes = [node]
8484
} else {
85-
subvalue = value.slice(lastIndex);
85+
subvalue = value.slice(lastIndex)
8686

8787
if (subvalue) {
88-
nodes.push({type: 'text', value: subvalue});
88+
nodes.push({type: 'text', value: subvalue})
8989
}
9090

91-
parent.children = siblings.slice(0, pos).concat(nodes).concat(siblings.slice(pos + 1));
91+
parent.children = siblings
92+
.slice(0, pos)
93+
.concat(nodes)
94+
.concat(siblings.slice(pos + 1))
9295
}
9396

9497
if (pairs.length <= 1) {
95-
return;
98+
return
9699
}
97100

98-
length = nodes.length;
99-
index = -1;
100-
subhandler = handlerFactory(pairs.slice(1));
101+
length = nodes.length
102+
index = -1
103+
subhandler = handlerFactory(pairs.slice(1))
101104

102105
while (++index < length) {
103-
node = nodes[index];
106+
node = nodes[index]
104107

105108
if (node.type === 'text') {
106-
subhandler(node, parent);
109+
subhandler(node, parent)
107110
} else {
108-
search(node, settings, subhandler);
111+
search(node, settings, subhandler)
109112
}
110113
}
111114
}
112115
}
113116
}
114117

115118
function search(tree, options, handler) {
116-
var ignore = options.ignore || IGNORE;
117-
var result = [];
119+
var ignore = options.ignore || IGNORE
120+
var result = []
118121

119-
visit(tree, 'text', visitor);
122+
visit(tree, 'text', visitor)
120123

121-
return result;
124+
return result
122125

123126
function visitor(node, parents) {
124-
var length = parents.length;
125-
var index = length;
127+
var length = parents.length
128+
var index = length
126129

127130
while (index--) {
128131
if (is(parents[index], ignore)) {
129-
return;
132+
return
130133
}
131134
}
132135

133-
handler(node, parents[length - 1]);
136+
handler(node, parents[length - 1])
134137
}
135138
}
136139

137140
function toPairs(schema) {
138-
var result = [];
139-
var key;
140-
var length;
141-
var index;
141+
var result = []
142+
var key
143+
var length
144+
var index
142145

143146
if (typeof schema !== 'object') {
144-
throw new Error('Expected array or object as schema');
147+
throw new Error('Expected array or object as schema')
145148
}
146149

147150
if (array(schema)) {
148-
length = schema.length;
149-
index = -1;
151+
length = schema.length
152+
index = -1
150153

151154
while (++index < length) {
152-
result.push([toExpression(schema[index][0]), toFunction(schema[index][1])]);
155+
result.push([
156+
toExpression(schema[index][0]),
157+
toFunction(schema[index][1])
158+
])
153159
}
154160
} else {
155161
for (key in schema) {
156-
result.push([toExpression(key), toFunction(schema[key])]);
162+
result.push([toExpression(key), toFunction(schema[key])])
157163
}
158164
}
159165

160-
return result;
166+
return result
161167
}
162168

163169
function toExpression(find) {
164-
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find;
170+
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find
165171
}
166172

167173
function toFunction(replace) {
168-
return typeof replace === 'function' ? replace : returner;
174+
return typeof replace === 'function' ? replace : returner
169175

170176
function returner() {
171-
return replace;
177+
return replace
172178
}
173179
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,31 @@
3232
"esmangle": "^1.0.1",
3333
"hastscript": "^3.0.1",
3434
"nyc": "^12.0.0",
35+
"prettier": "^1.13.5",
3536
"remark-cli": "^5.0.0",
3637
"remark-preset-wooorm": "^4.0.0",
3738
"tape": "^4.4.0",
3839
"xo": "^0.21.0"
3940
},
4041
"scripts": {
41-
"build-md": "remark . --quiet --frail --output",
42+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
4243
"build-bundle": "browserify index.js --bare -s hastUtilFindAndReplace > hast-util-find-and-replace.js",
4344
"build-mangle": "esmangle hast-util-find-and-replace.js > hast-util-find-and-replace.min.js",
44-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
45-
"lint": "xo",
45+
"build": "npm run build-bundle && npm run build-mangle",
4646
"test-api": "node test",
4747
"test-coverage": "nyc --reporter lcov tape test.js",
48-
"test": "npm run build && npm run lint && npm run test-coverage"
48+
"test": "npm run format && npm run build && npm run test-coverage"
49+
},
50+
"prettier": {
51+
"tabWidth": 2,
52+
"useTabs": false,
53+
"singleQuote": true,
54+
"bracketSpacing": false,
55+
"semi": false,
56+
"trailingComma": "none"
4957
},
5058
"xo": {
51-
"space": true,
59+
"prettier": true,
5260
"esnext": false,
5361
"rules": {
5462
"unicorn/prefer-type-error": "off",

readme.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ npm install hast-util-find-and-replace
1313
## Usage
1414

1515
```javascript
16-
var h = require('hastscript');
17-
var inspect = require('unist-util-inspect');
18-
var findAndReplace = require('hast-util-find-and-replace');
16+
var h = require('hastscript')
17+
var inspect = require('unist-util-inspect')
18+
var findAndReplace = require('hast-util-find-and-replace')
1919

2020
var tree = h('p', [
2121
'Some ',
@@ -25,17 +25,19 @@ var tree = h('p', [
2525
', and ',
2626
h('code', 'code'),
2727
'.'
28-
]);
28+
])
2929

30-
findAndReplace(tree, 'and', 'or');
30+
findAndReplace(tree, 'and', 'or')
3131

32-
findAndReplace(tree, {emphasis: 'em', importance: 'strong'});
32+
findAndReplace(tree, {emphasis: 'em', importance: 'strong'})
3333

34-
findAndReplace(tree, {code: function ($0) {
35-
return h('a', {href: '//example.com#' + $0}, $0);
36-
}});
34+
findAndReplace(tree, {
35+
code: function($0) {
36+
return h('a', {href: '//example.com#' + $0}, $0)
37+
}
38+
})
3739

38-
console.log(inspect(tree));
40+
console.log(inspect(tree))
3941
```
4042

4143
Yields:

0 commit comments

Comments
 (0)