Skip to content

Commit e5388f7

Browse files
authored
Change signature to use node first, test second
Closes GH-6. Closes GH-8. Reviewed-by: Christian Murphy <[email protected]>
1 parent dfbfc14 commit e5388f7

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ module.exports = is
66

77
is.convert = convert
88

9-
// Assert if `test` passes for `node`. When a `parent` node is known the
10-
// `index` of node.
9+
// Assert if `test` passes for `node`.
10+
// When a `parent` node is known the `index` of node should also be given.
1111
// eslint-disable-next-line max-params
12-
function is(test, node, index, parent, context) {
12+
function is(node, test, index, parent, context) {
1313
var hasParent = parent !== null && parent !== undefined
1414
var hasIndex = index !== null && index !== undefined
1515
var check = convert(test)
@@ -21,7 +21,7 @@ function is(test, node, index, parent, context) {
2121
throw new Error('Expected positive finite index or child node')
2222
}
2323

24-
if (hasParent && (!is(null, parent) || !parent.children)) {
24+
if (hasParent && (!is(parent) || !parent.children)) {
2525
throw new Error('Expected parent node')
2626
}
2727

readme.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,49 @@ function test(node, n) {
3131
}
3232

3333
is() // => false
34-
is(null, {children: []}) // => false
35-
is(null, node) // => true
36-
is('strong', node) // => true
37-
is('emphasis', node) // => false
34+
is({children: []}) // => false
35+
is(node) // => true
36+
is(node, 'strong') // => true
37+
is(node, 'emphasis') // => false
3838

3939
is(node, node) // => true
40-
is({type: 'paragraph'}, parent) // => true
41-
is({type: 'strong'}, parent) // => false
40+
is(parent, {type: 'paragraph'}) // => true
41+
is(parent, {type: 'strong'}) // => false
4242

43-
is(test, node) // => false
44-
is(test, node, 4, parent) // => false
45-
is(test, node, 5, parent) // => true
43+
is(node, test) // => false
44+
is(node, test, 4, parent) // => false
45+
is(node, test, 5, parent) // => true
4646
```
4747

4848
## API
4949

50-
### `is(test, node[, index, parent[, context]])`
50+
### `is(node[, test[, index, parent[, context]]])`
5151

5252
###### Parameters
5353

54+
* `node` ([`Node`][node]) — Node to check.
5455
* `test` ([`Function`][test], `string`, `Object`, or `Array.<Test>`, optional)
5556
— When not given, checks if `node` is a [`Node`][node].
5657
When `string`, works like passing `node => node.type === test`.
5758
When `array`, checks if any one of the subtests pass.
5859
When `object`, checks that all keys in `test` are in `node`,
5960
and that they have strictly equal values
60-
* `node` ([`Node`][node]) — Node to check. `false` is returned
6161
* `index` (`number`, optional) — [Index][] of `node` in `parent`
6262
* `parent` ([`Node`][node], optional) — [Parent][] of `node`
6363
* `context` (`*`, optional) — Context object to invoke `test` with
6464

6565
###### Returns
6666

67-
`boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object
68-
with `type` set to a non-empty `string`).
67+
`boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object with
68+
`type` set to a non-empty `string`).
6969

7070
#### `function test(node[, index, parent])`
7171

7272
###### Parameters
7373

74-
* `node` (`Node`) — Node to test
75-
* `index` (`number?`) — Position of `node` in `parent`
76-
* `parent` (`Node?`) — Parent of `node`
74+
* `node` ([`Node`][node]) — Node to check
75+
* `index` (`number?`) — [Index][] of `node` in `parent`
76+
* `parent` ([`Node?`][node]) — [Parent][] of `node`
7777

7878
###### Context
7979

test.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,88 @@ test('unist-util-is', function(t) {
99

1010
t.throws(
1111
function() {
12-
is(false)
12+
is(null, false)
1313
},
1414
/Expected function, string, or object as test/,
1515
'should throw when `test` is invalid'
1616
)
1717

1818
t.throws(
1919
function() {
20-
is(null, node, -1, parent)
20+
is(node, null, -1, parent)
2121
},
2222
/Expected positive finite index or child node/,
2323
'should throw when `index` is invalid (#1)'
2424
)
2525

2626
t.throws(
2727
function() {
28-
is(null, node, Infinity, parent)
28+
is(node, null, Infinity, parent)
2929
},
3030
/Expected positive finite index or child node/,
3131
'should throw when `index` is invalid (#2)'
3232
)
3333

3434
t.throws(
3535
function() {
36-
is(null, node, false, parent)
36+
is(node, null, false, parent)
3737
},
3838
/Expected positive finite index or child node/,
3939
'should throw when `index` is invalid (#3)'
4040
)
4141

4242
t.throws(
4343
function() {
44-
is(null, node, 0, {})
44+
is(node, null, 0, {})
4545
},
4646
/Expected parent node/,
4747
'should throw when `parent` is invalid (#1)'
4848
)
4949

5050
t.throws(
5151
function() {
52-
is(null, node, 0, {
53-
type: 'paragraph'
54-
})
52+
is(node, null, 0, {type: 'paragraph'})
5553
},
5654
/Expected parent node/,
5755
'should throw when `parent` is invalid (#2)'
5856
)
5957

6058
t.throws(
6159
function() {
62-
is(null, node, 0)
60+
is(node, null, 0)
6361
},
6462
/Expected both parent and index/,
6563
'should throw `parent` xor `index` are given (#1)'
6664
)
6765

6866
t.throws(
6967
function() {
70-
is(null, node, null, parent)
68+
is(node, null, null, parent)
7169
},
7270
/Expected both parent and index/,
7371
'should throw `parent` xor `index` are given (#2)'
7472
)
7573

7674
t.notok(is(), 'should not fail without node')
77-
t.ok(is(null, node), 'should check if given a node (#1)')
78-
t.notok(is(null, {children: []}), 'should check if given a node (#2)')
75+
t.ok(is(node), 'should check if given a node (#1)')
76+
t.notok(is({children: []}, null), 'should check if given a node (#2)')
7977

80-
t.ok(is('strong', node), 'should match types (#1)')
81-
t.notok(is('emphasis', node), 'should match types (#2)')
78+
t.ok(is(node, 'strong'), 'should match types (#1)')
79+
t.notok(is(node, 'emphasis'), 'should match types (#2)')
8280

8381
t.ok(is(node, node), 'should match partially (#1)')
84-
t.ok(is({type: 'strong'}, node), 'should match partially (#2)')
85-
t.ok(is({type: 'paragraph'}, parent), 'should match partially (#3)')
86-
t.notok(is({type: 'paragraph'}, node), 'should match partially (#4)')
82+
t.ok(is(node, {type: 'strong'}), 'should match partially (#2)')
83+
t.ok(is(parent, {type: 'paragraph'}), 'should match partially (#3)')
84+
t.notok(is(node, {type: 'paragraph'}), 'should match partially (#4)')
8785

8886
t.test('should accept a test', function(st) {
8987
function test(node, n) {
9088
return n === 5
9189
}
9290

93-
t.notok(is(test, node))
94-
t.notok(is(test, node, 0, parent))
95-
st.ok(is(test, node, 5, parent))
91+
t.notok(is(node, test))
92+
t.notok(is(node, test, 0, parent))
93+
st.ok(is(node, test, 5, parent))
9694

9795
st.end()
9896
})
@@ -109,18 +107,18 @@ test('unist-util-is', function(t) {
109107
st.equal(c, parent)
110108
}
111109

112-
is(test, node, 5, parent, context)
110+
is(node, test, 5, parent, context)
113111
})
114112

115-
t.ok(is(['strong', 'emphasis'], node), 'should match arrays (#1)')
116-
t.notok(is(['b', 'i'], node), 'should match arrays (#2)')
113+
t.ok(is(node, ['strong', 'emphasis']), 'should match arrays (#1)')
114+
t.notok(is(node, ['b', 'i']), 'should match arrays (#2)')
117115

118116
t.test('should match arrays (#3)', function(st) {
119117
var context = {foo: 'bar'}
120118

121119
st.plan(5)
122120

123-
st.ok(is([test, 'strong'], node, 5, parent, context))
121+
st.ok(is(node, [test, 'strong'], 5, parent, context))
124122

125123
function test(a, b, c) {
126124
st.equal(this, context)

0 commit comments

Comments
 (0)