Skip to content

Commit 2b40329

Browse files
committed
Refactor to use class
1 parent 558daea commit 2b40329

File tree

3 files changed

+40
-98
lines changed

3 files changed

+40
-98
lines changed

index.js

+39-59
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,60 @@
11
import {visit} from 'unist-util-visit'
22

3-
Index.prototype.get = get
4-
Index.prototype.add = add
5-
Index.prototype.remove = remove
6-
7-
export function Index(tree, filter, prop) {
8-
var self
9-
10-
if (!(this instanceof Index)) {
11-
return new Index(tree, filter, prop)
12-
}
13-
14-
if (prop === null || prop === undefined) {
15-
if (filter === null || filter === undefined) {
16-
prop = tree
17-
tree = null
18-
} else {
19-
prop = filter
3+
export class Index {
4+
constructor(tree, filter, prop) {
5+
var pluck = (node) => node[prop]
6+
7+
if (prop === null || prop === undefined) {
8+
if (filter === null || filter === undefined) {
9+
prop = tree
10+
tree = null
11+
} else {
12+
prop = filter
13+
}
14+
15+
filter = trueConst
2016
}
2117

22-
filter = trueConst
23-
}
24-
25-
self = this
26-
27-
this.index = new Map()
28-
this.keyfn = typeof prop === 'string' ? pluck : prop
29-
30-
if (tree) {
31-
visit(tree, filter, add)
32-
}
33-
34-
return this
18+
this.index = new Map()
19+
this.keyfn = typeof prop === 'string' ? pluck : prop
3520

36-
function pluck(node) {
37-
return node[prop]
21+
if (tree) {
22+
visit(tree, filter, (node) => this.add(node))
23+
}
3824
}
3925

40-
function add(node) {
41-
self.add(node)
26+
get(key) {
27+
return this.index.get(key) || []
4228
}
43-
}
4429

45-
function get(key) {
46-
return this.index.get(key) || []
47-
}
30+
add(node) {
31+
var key = this.keyfn(node)
32+
var nodes
4833

49-
function add(node) {
50-
var self = this
51-
var key = self.keyfn(node)
52-
var nodes
34+
if (!this.index.has(key)) {
35+
this.index.set(key, [])
36+
}
5337

54-
if (!self.index.has(key)) {
55-
self.index.set(key, [])
56-
}
38+
nodes = this.index.get(key)
5739

58-
nodes = self.index.get(key)
40+
if (!nodes.includes(node)) {
41+
nodes.push(node)
42+
}
5943

60-
if (nodes.indexOf(node) === -1) {
61-
nodes.push(node)
44+
return this
6245
}
6346

64-
return self
65-
}
47+
remove(node) {
48+
var key = this.keyfn(node)
49+
var nodes = this.index.get(key)
50+
var pos = nodes ? nodes.indexOf(node) : -1
6651

67-
function remove(node) {
68-
var self = this
69-
var key = self.keyfn(node)
70-
var nodes = self.index.get(key)
71-
var pos = nodes ? nodes.indexOf(node) : -1
52+
if (pos !== -1) {
53+
nodes.splice(pos, 1)
54+
}
7255

73-
if (pos !== -1) {
74-
nodes.splice(pos, 1)
56+
return this
7557
}
76-
77-
return self
7858
}
7959

8060
function trueConst() {

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@
6868
"xo": {
6969
"prettier": true,
7070
"rules": {
71-
"unicorn/no-this-assignment": "off",
72-
"unicorn/prefer-includes": "off",
7371
"no-var": "off",
7472
"prefer-arrow-callback": "off"
7573
}

test.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,11 @@ import {u} from 'unist-builder'
33
import {select} from 'unist-util-select'
44
import {Index} from './index.js'
55

6-
var index = Index
7-
86
test('Index', function (t) {
97
var node = {type: 'a', id: 1}
108
var alt = {type: 'b', id: 1}
119
var tree = {type: 'root', children: [node, alt]}
12-
var instance = index('id')
13-
instance.add(node)
14-
15-
t.deepEqual(
16-
[instance instanceof Index, instance.get(1)],
17-
[true, [node]],
18-
'index(prop)'
19-
)
20-
21-
instance = new Index('id')
10+
var instance = new Index('id')
2211
instance.add(node)
2312

2413
t.deepEqual(
@@ -27,15 +16,6 @@ test('Index', function (t) {
2716
'new Index(prop)'
2817
)
2918

30-
instance = index(keyFn)
31-
instance.add(node)
32-
33-
t.deepEqual(
34-
[instance instanceof Index, instance.get(1)],
35-
[true, [node]],
36-
'index(keyFn)'
37-
)
38-
3919
instance = new Index(keyFn)
4020
instance.add(node)
4121

@@ -45,14 +25,6 @@ test('Index', function (t) {
4525
'new Index(keyFn)'
4626
)
4727

48-
instance = index(tree, 'id')
49-
50-
t.deepEqual(
51-
[instance instanceof Index, instance.get(1)],
52-
[true, [node, alt]],
53-
'index(tree, prop)'
54-
)
55-
5628
instance = new Index(tree, 'id')
5729

5830
t.deepEqual(
@@ -61,14 +33,6 @@ test('Index', function (t) {
6133
'new Index(tree, prop)'
6234
)
6335

64-
instance = index(tree, filter, 'id')
65-
66-
t.deepEqual(
67-
[instance instanceof Index, instance.get(1)],
68-
[true, [node]],
69-
'index(tree, filter, prop)'
70-
)
71-
7236
instance = new Index(tree, filter, 'id')
7337

7438
t.deepEqual(

0 commit comments

Comments
 (0)