Skip to content

Commit 4b4e243

Browse files
committed
Use ESM
1 parent b2c5358 commit 4b4e243

17 files changed

+151
-189
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
'use strict'
1+
import {any} from './lib/any.js'
2+
import {parse} from './lib/parse.js'
23

3-
exports.matches = matches
4-
exports.selectAll = selectAll
5-
exports.select = select
6-
7-
var any = require('./lib/any')
8-
var parse = require('./lib/parse')
9-
10-
function matches(selector, node) {
11-
return Boolean(
12-
any(parse(selector), node, {one: true, shallow: true, any: any})[0]
13-
)
4+
export function matches(selector, node) {
5+
return Boolean(any(parse(selector), node, {one: true, shallow: true, any})[0])
146
}
157

16-
function select(selector, node) {
17-
return any(parse(selector), node, {one: true, any: any})[0] || null
8+
export function select(selector, node) {
9+
return any(parse(selector), node, {one: true, any})[0] || null
1810
}
1911

20-
function selectAll(selector, node) {
21-
return any(parse(selector), node, {any: any})
12+
export function selectAll(selector, node) {
13+
return any(parse(selector), node, {any})
2214
}

lib/any.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
var zwitch = require('zwitch')
6-
var pseudo = require('./pseudo')
7-
var test = require('./test')
8-
var nest = require('./nest')
1+
import {zwitch} from 'zwitch'
2+
import {pseudo} from './pseudo.js'
3+
import {test} from './test.js'
4+
import {nest} from './nest.js'
95

106
var type = zwitch('type', {
117
unknown: unknownType,
128
invalid: invalidType,
13-
handlers: {
14-
selectors: selectors,
15-
ruleSet: ruleSet,
16-
rule: rule
17-
}
9+
handlers: {selectors, ruleSet, rule}
1810
})
1911

20-
function match(query, node, state) {
12+
export function any(query, node, state) {
2113
return query && node ? type(query, node, state) : []
2214
}
2315

@@ -50,7 +42,7 @@ function rule(query, tree, state) {
5042
null,
5143
configure(query, {
5244
scopeNodes: tree.type === 'root' ? tree.children : [tree],
53-
iterator: iterator,
45+
iterator,
5446
one: state.one,
5547
shallow: state.shallow,
5648
any: state.any
@@ -76,7 +68,7 @@ function configure(query, state) {
7668
var index = -1
7769

7870
while (++index < pseudos.length) {
79-
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
71+
if (pseudo.needsIndex.includes(pseudos[index].name)) {
8072
state.index = true
8173
break
8274
}
@@ -85,12 +77,14 @@ function configure(query, state) {
8577
return state
8678
}
8779

88-
/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
80+
// Shouldn’t be invoked, all data is handled.
81+
/* c8 ignore next 3 */
8982
function unknownType(query) {
9083
throw new Error('Unknown type `' + query.type + '`')
9184
}
9285

93-
/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
86+
// Shouldn’t be invoked, parser gives correct data.
87+
/* c8 ignore next 3 */
9488
function invalidType() {
9589
throw new Error('Invalid type')
9690
}
@@ -118,12 +112,13 @@ function collector(one) {
118112

119113
function collectOne(node) {
120114
if (one) {
121-
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
115+
/* Shouldn’t happen, safeguards performance problems. */
116+
/* c8 ignore next */
122117
if (found) throw new Error('Cannot collect multiple nodes')
123118

124119
found = true
125120
}
126121

127-
if (result.indexOf(node) < 0) result.push(node)
122+
if (!result.includes(node)) result.push(node)
128123
}
129124
}

lib/attribute.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
var zwitch = require('zwitch')
1+
import {zwitch} from 'zwitch'
62

73
var handle = zwitch('operator', {
84
unknown: unknownOperator,
@@ -16,7 +12,7 @@ var handle = zwitch('operator', {
1612
}
1713
})
1814

19-
function match(query, node) {
15+
export function attribute(query, node) {
2016
var attrs = query.attrs
2117
var index = -1
2218

@@ -29,25 +25,25 @@ function match(query, node) {
2925

3026
// [attr]
3127
function exists(query, node) {
32-
return node[query.name] != null
28+
return node[query.name] !== null && node[query.name] !== undefined
3329
}
3430

3531
// [attr=value]
3632
function exact(query, node) {
37-
return node[query.name] != null && String(node[query.name]) === query.value
33+
return exists(query, node) && String(node[query.name]) === query.value
3834
}
3935

4036
// [attr~=value]
4137
function containsArray(query, node) {
4238
var value = node[query.name]
4339

44-
if (value == null) return false
40+
if (value === null || value === undefined) return false
4541

4642
// If this is an array, and the query is contained in it, return true.
4743
if (
4844
typeof value === 'object' &&
4945
'length' in value &&
50-
value.indexOf(query.value) > -1
46+
value.includes(query.value)
5147
) {
5248
return true
5349
}
@@ -79,10 +75,11 @@ function ends(query, node) {
7975
// [attr*=value]
8076
function containsString(query, node) {
8177
var value = node[query.name]
82-
return typeof value === 'string' && value.indexOf(query.value) > -1
78+
return typeof value === 'string' && value.includes(query.value)
8379
}
8480

85-
/* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */
81+
// Shouldn’t be invoked, Parser throws an error instead.
82+
/* c8 ignore next 3 */
8683
function unknownOperator(query) {
8784
throw new Error('Unknown operator `' + query.operator + '`')
8885
}

lib/name.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
function match(query, node) {
1+
export function name(query, node) {
62
return query.tagName === '*' || query.tagName === node.type
73
}

lib/nest.js

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
var zwitch = require('zwitch')
1+
import {zwitch} from 'zwitch'
62

73
var own = {}.hasOwnProperty
84

@@ -17,36 +13,38 @@ var handle = zwitch('nestingOperator', {
1713
}
1814
})
1915

20-
function match(query, node, index, parent, state) {
16+
export function nest(query, node, index, parent, state) {
2117
return handle(query, node, index, parent, state)
2218
}
2319

24-
/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
20+
// Shouldn’t be invoked, parser gives correct data.
21+
/* c8 ignore next 3 */
2522
function unknownNesting(query) {
2623
throw new Error('Unexpected nesting `' + query.nestingOperator + '`')
2724
}
2825

2926
function topScan(query, node, index, parent, state) {
30-
/* istanbul ignore if - Shouldn’t happen. */
27+
// Shouldn’t happen.
28+
/* c8 ignore next 3 */
3129
if (parent) {
3230
throw new Error('topScan is supposed to be called from the root node')
3331
}
3432

35-
state.iterator.apply(null, arguments)
33+
state.iterator(...arguments)
3634

37-
if (!state.shallow) descendant.apply(this, arguments)
35+
if (!state.shallow) descendant.call(this, ...arguments)
3836
}
3937

4038
function descendant(query, node, index, parent, state) {
4139
var previous = state.iterator
4240

4341
state.iterator = iterator
4442

45-
child.apply(this, arguments)
43+
child.call(this, ...arguments)
4644

4745
function iterator(_, node, index, parent, state) {
4846
state.iterator = previous
49-
previous.apply(this, arguments)
47+
previous.call(this, ...arguments)
5048
state.iterator = iterator
5149

5250
if (state.one && state.found) return
@@ -56,13 +54,14 @@ function descendant(query, node, index, parent, state) {
5654
}
5755

5856
function child(query, node, index, parent, state) {
59-
if (!node.children || !node.children.length) return
57+
if (!node.children || node.children.length === 0) return
6058

6159
walkIterator(query, node, state).each().done()
6260
}
6361

6462
function adjacentSibling(query, node, index, parent, state) {
65-
/* istanbul ignore if - Shouldn’t happen. */
63+
// Shouldn’t happen.
64+
/* c8 ignore next */
6665
if (!parent) return
6766

6867
walkIterator(query, parent, state)
@@ -73,7 +72,8 @@ function adjacentSibling(query, node, index, parent, state) {
7372
}
7473

7574
function generalSibling(query, node, index, parent, state) {
76-
/* istanbul ignore if - Shouldn’t happen. */
75+
// Shouldn’t happen.
76+
/* c8 ignore next */
7777
if (!parent) return
7878

7979
walkIterator(query, parent, state)
@@ -91,7 +91,7 @@ function walkIterator(query, parent, state) {
9191
return {
9292
prefillTypeIndex: rangeDefaults(prefillTypeIndex),
9393
each: rangeDefaults(each),
94-
done: done
94+
done
9595
}
9696

9797
function done() {
@@ -149,8 +149,9 @@ function walkIterator(query, parent, state) {
149149
return rangeDefault
150150

151151
function rangeDefault(start, end) {
152-
if (start == null || start < 0) start = 0
153-
if (end == null || end > siblings.length) end = siblings.length
152+
if (start === null || start === undefined || start < 0) start = 0
153+
if (end === null || end === undefined || end > siblings.length)
154+
end = siblings.length
154155
return iterator.call(this, start, end)
155156
}
156157
}

lib/parse.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
'use strict'
1+
import {CssSelectorParser} from 'css-selector-parser'
2+
import nthCheck from 'nth-check'
3+
import {zwitch} from 'zwitch'
24

3-
module.exports = parse
5+
var nth = new Set([
6+
'nth-child',
7+
'nth-last-child',
8+
'nth-of-type',
9+
'nth-last-of-type'
10+
])
411

5-
var Parser = require('css-selector-parser').CssSelectorParser
6-
var zwitch = require('zwitch')
7-
var nthCheck = require('nth-check').default
8-
9-
var nth = ['nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type']
10-
11-
var parser = new Parser()
12+
var parser = new CssSelectorParser()
1213

1314
var compile = zwitch('type', {
14-
handlers: {
15-
selectors: selectors,
16-
ruleSet: ruleSet,
17-
rule: rule
18-
}
15+
handlers: {selectors, ruleSet, rule}
1916
})
2017

2118
parser.registerAttrEqualityMods('~', '^', '$', '*')
2219
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
2320
parser.registerNestingOperators('>', '+', '~')
2421

25-
function parse(selector) {
22+
export function parse(selector) {
2623
if (typeof selector !== 'string') {
2724
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
2825
}
@@ -53,8 +50,8 @@ function rule(query) {
5350
while (++index < pseudos.length) {
5451
pseudo = pseudos[index]
5552

56-
if (nth.indexOf(pseudo.name) > -1) {
57-
pseudo.value = nthCheck(pseudo.value)
53+
if (nth.has(pseudo.name)) {
54+
pseudo.value = nthCheck.default(pseudo.value)
5855
pseudo.valueType = 'function'
5956
}
6057
}

0 commit comments

Comments
 (0)