Skip to content

Commit 94d9538

Browse files
committed
Refactor code-style
1 parent 0a94289 commit 94d9538

File tree

4 files changed

+53
-60
lines changed

4 files changed

+53
-60
lines changed

index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ export class Index {
1717
/** @type {Map.<unknown, Array.<Node>>} */
1818
this.index = new Map()
1919
/** @type {KeyFunction} */
20-
this.key =
21-
typeof prop === 'string' ? (/** @type {Node} */ node) => node[prop] : prop
20+
this.key = typeof prop === 'string' ? (node) => node[prop] : prop
2221

2322
if (tree) {
24-
visit(tree, test, (/** @type {Node} */ node) => {
23+
visit(tree, test, (node) => {
2524
this.add(node)
2625
})
2726
}
@@ -39,15 +38,13 @@ export class Index {
3938
* @param {Node} node
4039
*/
4140
add(node) {
42-
var key = this.key(node)
43-
/** @type {Array.<Node>} */
44-
var nodes
41+
const key = this.key(node)
4542

4643
if (!this.index.has(key)) {
4744
this.index.set(key, [])
4845
}
4946

50-
nodes = this.index.get(key)
47+
const nodes = this.index.get(key)
5148

5249
if (!nodes.includes(node)) {
5350
nodes.push(node)
@@ -60,9 +57,9 @@ export class Index {
6057
* @param {Node} node
6158
*/
6259
remove(node) {
63-
var key = this.key(node)
64-
var nodes = this.index.get(key)
65-
var pos = nodes ? nodes.indexOf(node) : -1
60+
const key = this.key(node)
61+
const nodes = this.index.get(key)
62+
const pos = nodes ? nodes.indexOf(node) : -1
6663

6764
if (pos !== -1) {
6865
nodes.splice(pos, 1)

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@
7575
"trailingComma": "none"
7676
},
7777
"xo": {
78-
"prettier": true,
79-
"rules": {
80-
"no-var": "off",
81-
"prefer-arrow-callback": "off"
82-
}
78+
"prettier": true
8379
},
8480
"remarkConfig": {
8581
"plugins": [

readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ npm install unist-util-index
2525
## Use
2626

2727
```js
28-
import fs from 'fs'
29-
import remark from 'remark'
30-
import toString from 'mdast-util-to-string'
28+
import fs from 'node:fs'
29+
import {remark} from 'remark'
30+
import {toString} from 'mdast-util-to-string'
3131
import {Index} from 'unist-util-index'
3232

3333
// Parse and read this repo’s readme:
34-
var tree = remark.parse(fs.readFileSync('readme.md'))
34+
const tree = remark.parse(fs.readFileSync('readme.md'))
3535

3636
// Index on heading depth:
37-
var index = new Index('depth', tree, 'heading')
37+
const indexOnDepth = new Index('depth', tree, 'heading')
3838

39-
console.log(index.get(2).map(toString))
39+
console.log(indexOnDepth.get(2).map(toString))
4040

4141
// Index on definition identifier:
42-
index = new Index('identifier', tree, 'definition')
42+
const indexOnIdentifier = new Index('identifier', tree, 'definition')
4343

44-
console.log(index.get('unist').map(node => node.url))
44+
console.log(indexOnIdentifier.get('unist').map(node => node.url))
4545
```
4646

4747
Yields:

test.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {u} from 'unist-builder'
99
import {select} from 'unist-util-select'
1010
import {Index} from './index.js'
1111

12-
test('Index', function (t) {
13-
var node = {type: 'a', id: 1}
14-
var alt = {type: 'b', id: 1}
15-
var tree = {type: 'root', children: [node, alt]}
16-
var instance = new Index('id')
12+
test('Index', (t) => {
13+
const node = {type: 'a', id: 1}
14+
const alt = {type: 'b', id: 1}
15+
const tree = {type: 'root', children: [node, alt]}
16+
let instance = new Index('id')
1717
instance.add(node)
1818

1919
t.deepEqual(
@@ -55,14 +55,14 @@ test('Index', function (t) {
5555
t.end()
5656
})
5757

58-
test('index.add', function (t) {
59-
var ast = u('root', [u('node', {word: 'foo'}), u('node', {word: 'bar'})])
60-
var extraNode = u('node', {word: 'foo'})
58+
test('index.add', (t) => {
59+
const ast = u('root', [u('node', {word: 'foo'}), u('node', {word: 'bar'})])
60+
const extraNode = u('node', {word: 'foo'})
6161

62-
var index = new Index('word', ast)
62+
const index = new Index('word', ast)
6363
t.deepEqual(index.get('foo'), [select('[word=foo]', ast)])
6464

65-
var result = index.add(extraNode)
65+
const result = index.add(extraNode)
6666
t.deepEqual(index.get('foo'), [select('[word=foo]', ast), extraNode])
6767

6868
t.equal(result, index, 'returns this')
@@ -76,9 +76,9 @@ test('index.add', function (t) {
7676
t.end()
7777
})
7878

79-
test('index.get', function (t) {
80-
t.test('get', function (st) {
81-
var ast = u('node', {color: 'black', id: 0}, [
79+
test('index.get', (t) => {
80+
t.test('get', (st) => {
81+
const ast = u('node', {color: 'black', id: 0}, [
8282
u('node', {color: 'black', id: 1}, [
8383
u('node', {color: 'red', id: 2}, [
8484
u('node', {color: 'black', id: 3}),
@@ -101,7 +101,7 @@ test('index.get', function (t) {
101101
])
102102
])
103103

104-
var index = new Index('color', ast)
104+
const index = new Index('color', ast)
105105

106106
st.deepEqual(index.get('black'), [
107107
select('[id=0]', ast),
@@ -129,32 +129,32 @@ test('index.get', function (t) {
129129
st.end()
130130
})
131131

132-
t.test('degenerate keys', function (st) {
133-
st.test('Object.prototype keys', function (sst) {
134-
var ast = u('node', {word: '__proto__', id: 0}, [
132+
t.test('degenerate keys', (st) => {
133+
st.test('Object.prototype keys', (sst) => {
134+
const ast = u('node', {word: '__proto__', id: 0}, [
135135
u('node', {word: 'constructor', id: 1}),
136136
u('node', {word: 'toString', id: 2})
137137
])
138-
var index = new Index('word', ast)
138+
const index = new Index('word', ast)
139139

140140
sst.deepEqual(index.get('__proto__'), [select('[id=0]', ast)])
141141
sst.deepEqual(index.get('constructor'), [select('[id=1]', ast)])
142142
sst.deepEqual(index.get('toString'), [select('[id=2]', ast)])
143143
sst.end()
144144
})
145145

146-
st.test('identity keys', function (sst) {
147-
var id1 = {foo: 'bar'}
148-
var id2 = {foo: 'bar'}
149-
var ast = u('root', [
146+
st.test('identity keys', (sst) => {
147+
const id1 = {foo: 'bar'}
148+
const id2 = {foo: 'bar'}
149+
const ast = u('root', [
150150
u('node', {word: false, id: 0}),
151151
u('node', {word: 'false', id: 1}),
152152
u('node', {word: 1, id: 2}),
153153
u('node', {word: '1', id: 3}),
154154
u('node', {word: id1, id: 4}),
155155
u('node', {word: id2, id: 5})
156156
])
157-
var index = new Index('word', ast)
157+
const index = new Index('word', ast)
158158

159159
sst.deepEqual(index.get(false), [select('[id=0]', ast)])
160160
sst.deepEqual(index.get('false'), [select('[id=1]', ast)])
@@ -169,29 +169,29 @@ test('index.get', function (t) {
169169
st.end()
170170
})
171171

172-
t.test('empty index', function (st) {
172+
t.test('empty index', (st) => {
173173
st.deepEqual(new Index('foo', null).get('bar'), [])
174174
st.deepEqual(new Index('foo').get('bar'), [])
175175
st.end()
176176
})
177177

178-
t.test('Index filter', function (st) {
179-
var ast = u('root', [
178+
t.test('Index filter', (st) => {
179+
const ast = u('root', [
180180
u('node', {word: 'foo'}),
181181
u('node', {word: 'bar'}),
182182
u('skip', {word: 'foo'}),
183183
u('skip', {word: 'bar'})
184184
])
185185

186-
st.test('type test', function (sst) {
187-
var index = new Index('word', ast, 'node')
186+
st.test('type test', (sst) => {
187+
const index = new Index('word', ast, 'node')
188188
sst.deepEqual(index.get('foo'), [select('node[word="foo"]', ast)])
189189
sst.deepEqual(index.get('bar'), [select('node[word="bar"]', ast)])
190190
sst.end()
191191
})
192192

193-
st.test('function test', function (sst) {
194-
var index = new Index('word', ast, filter)
193+
st.test('function test', (sst) => {
194+
const index = new Index('word', ast, filter)
195195
sst.deepEqual(index.get('foo'), [select('node[word="foo"]', ast)])
196196
sst.deepEqual(index.get('bar'), [select('node[word="bar"]', ast)])
197197
sst.end()
@@ -209,19 +209,19 @@ test('index.get', function (t) {
209209
st.end()
210210
})
211211

212-
t.test('computed keys', function (st) {
212+
t.test('computed keys', (st) => {
213213
/**
214214
* @typedef {{x: number, y: number, z: number}} FunkyNode
215215
*/
216216

217-
var ast = u('root', {x: 0, y: 4, id: 0}, [
217+
const ast = u('root', {x: 0, y: 4, id: 0}, [
218218
u('node', {x: 3, y: 2, id: 1}),
219219
u('node', {x: 2, y: 2, id: 2}),
220220
u('node', {x: 3, y: 1, id: 3}),
221221
u('node', {x: 4, y: 1, id: 4})
222222
])
223223
// @ts-ignore it’s fine
224-
var index = new Index(xPlusY, ast)
224+
const index = new Index(xPlusY, ast)
225225
st.deepEqual(index.get(4), [
226226
select('[id=0]', ast),
227227
select('[id=2]', ast),
@@ -249,20 +249,20 @@ test('index.get', function (t) {
249249
t.end()
250250
})
251251

252-
test('index.remove', function (t) {
253-
var ast = u('root', [
252+
test('index.remove', (t) => {
253+
const ast = u('root', [
254254
u('bad', {word: 'foo'}),
255255
u('node', {word: 'foo'}),
256256
u('node', {word: 'bar'})
257257
])
258258

259-
var index = new Index('word', ast)
259+
const index = new Index('word', ast)
260260
t.deepEqual(index.get('foo'), [
261261
select('bad[word=foo]', ast),
262262
select('node[word=foo]', ast)
263263
])
264264

265-
var result = index.remove(select('bad', ast))
265+
const result = index.remove(select('bad', ast))
266266
t.deepEqual(index.get('foo'), [select('node[word=foo]', ast)])
267267

268268
t.equal(result, index, 'returns this')

0 commit comments

Comments
 (0)