Skip to content

Commit 5613239

Browse files
committed
Change parameter order to simplify signature
1 parent 2b40329 commit 5613239

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import {visit} from 'unist-util-visit'
22

33
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-
4+
constructor(prop, tree, filter) {
5+
if (filter === null || filter === undefined) {
156
filter = trueConst
167
}
178

189
this.index = new Map()
19-
this.keyfn = typeof prop === 'string' ? pluck : prop
10+
this.keyfn = typeof prop === 'string' ? (node) => node[prop] : prop
2011

2112
if (tree) {
2213
visit(tree, filter, (node) => this.add(node))

readme.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ var Index = require('unist-util-index')
3434
var tree = remark.parse(fs.readFileSync('readme.md'))
3535

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

3939
console.log(index.get(2).map(toString))
4040

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

4444
console.log(index.get('unist').map(node => node.url))
4545
```
@@ -56,28 +56,22 @@ Yields:
5656
This package exports the following identifiers: `Index`.
5757
There is no default export.
5858

59-
### `class Index([tree[, test], ]prop|keyFn)`
59+
### `class Index(prop|keyFn[, tree[, test]])`
6060

6161
Create an index data structure that maps keys (calculated by `keyFn` function or
6262
the values at `prop` in each node) to a list of nodes.
6363

6464
If `tree` is given, the index is initialized with all nodes, optionally filtered
6565
by `test`.
6666

67-
###### Signatures
68-
69-
* `new Index(prop|keyFn)`
70-
* `new Index(tree, prop|keyFn)`
71-
* `new Index(tree, test, prop|keyFn)`
72-
7367
###### Parameters
7468

75-
* `tree` ([`Node?`][node]) — [Tree][] to index
76-
* `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
77-
[type][])
7869
* `prop` (`string`) — Property to look up in each node to find keys
7970
* `keyFn` ([`Function`][keyfn]) — Function called with each node to calculate
8071
keys
72+
* `tree` ([`Node?`][node]) — [Tree][] to index
73+
* `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
74+
[type][])
8175

8276
###### Returns
8377

test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ test('Index', function (t) {
2525
'new Index(keyFn)'
2626
)
2727

28-
instance = new Index(tree, 'id')
28+
instance = new Index('id', tree)
2929

3030
t.deepEqual(
3131
[instance instanceof Index, instance.get(1)],
3232
[true, [node, alt]],
33-
'new Index(tree, prop)'
33+
'new Index(prop, tree)'
3434
)
3535

36-
instance = new Index(tree, filter, 'id')
36+
instance = new Index('id', tree, filter)
3737

3838
t.deepEqual(
3939
[instance instanceof Index, instance.get(1)],
4040
[true, [node]],
41-
'new Index(tree, filter, prop)'
41+
'new Index(prop, tree, filter)'
4242
)
4343

4444
t.end()
@@ -56,7 +56,7 @@ test('index.add', function (t) {
5656
var ast = u('root', [u('node', {word: 'foo'}), u('node', {word: 'bar'})])
5757
var extraNode = u('node', {word: 'foo'})
5858

59-
var index = new Index(ast, 'word')
59+
var index = new Index('word', ast)
6060
t.deepEqual(index.get('foo'), [select('[word=foo]', ast)])
6161

6262
var result = index.add(extraNode)
@@ -98,7 +98,7 @@ test('index.get', function (t) {
9898
])
9999
])
100100

101-
var index = new Index(ast, 'color')
101+
var index = new Index('color', ast)
102102

103103
st.deepEqual(index.get('black'), [
104104
select('[id=0]', ast),
@@ -132,7 +132,7 @@ test('index.get', function (t) {
132132
u('node', {word: 'constructor', id: 1}),
133133
u('node', {word: 'toString', id: 2})
134134
])
135-
var index = new Index(ast, 'word')
135+
var index = new Index('word', ast)
136136

137137
sst.deepEqual(index.get('__proto__'), [select('[id=0]', ast)])
138138
sst.deepEqual(index.get('constructor'), [select('[id=1]', ast)])
@@ -151,7 +151,7 @@ test('index.get', function (t) {
151151
u('node', {word: id1, id: 4}),
152152
u('node', {word: id2, id: 5})
153153
])
154-
var index = new Index(ast, 'word')
154+
var index = new Index('word', ast)
155155

156156
sst.deepEqual(index.get(false), [select('[id=0]', ast)])
157157
sst.deepEqual(index.get('false'), [select('[id=1]', ast)])
@@ -167,7 +167,7 @@ test('index.get', function (t) {
167167
})
168168

169169
t.test('empty index', function (st) {
170-
st.deepEqual(new Index(null, 'foo').get('bar'), [])
170+
st.deepEqual(new Index('foo', null).get('bar'), [])
171171
st.deepEqual(new Index('foo').get('bar'), [])
172172
st.end()
173173
})
@@ -181,14 +181,14 @@ test('index.get', function (t) {
181181
])
182182

183183
st.test('type test', function (sst) {
184-
var index = new Index(ast, 'node', 'word')
184+
var index = new Index('word', ast, 'node')
185185
sst.deepEqual(index.get('foo'), [select('node[word="foo"]', ast)])
186186
sst.deepEqual(index.get('bar'), [select('node[word="bar"]', ast)])
187187
sst.end()
188188
})
189189

190190
st.test('function test', function (sst) {
191-
var index = new Index(ast, filter, 'word')
191+
var index = new Index('word', ast, filter)
192192
sst.deepEqual(index.get('foo'), [select('node[word="foo"]', ast)])
193193
sst.deepEqual(index.get('bar'), [select('node[word="bar"]', ast)])
194194
sst.end()
@@ -210,7 +210,7 @@ test('index.get', function (t) {
210210
])
211211
var index
212212

213-
index = new Index(ast, xPlusY)
213+
index = new Index(xPlusY, ast)
214214
st.deepEqual(index.get(4), [
215215
select('[id=0]', ast),
216216
select('[id=2]', ast),
@@ -219,7 +219,7 @@ test('index.get', function (t) {
219219
st.deepEqual(index.get(0), [])
220220
st.deepEqual(index.get(5), [select('[id=1]', ast), select('[id=4]', ast)])
221221

222-
st.deepEqual(new Index(ast, 'node', xPlusY).get(4), [
222+
st.deepEqual(new Index(xPlusY, ast, 'node').get(4), [
223223
select('[id=2]', ast),
224224
select('[id=3]', ast)
225225
])
@@ -241,7 +241,7 @@ test('index.remove', function (t) {
241241
u('node', {word: 'bar'})
242242
])
243243

244-
var index = new Index(ast, 'word')
244+
var index = new Index('word', ast)
245245
t.deepEqual(index.get('foo'), [
246246
select('bad[word=foo]', ast),
247247
select('node[word=foo]', ast)

0 commit comments

Comments
 (0)