Skip to content

Commit d48ac1c

Browse files
committed
Refactor to improve bundle size
1 parent afebbaa commit d48ac1c

File tree

6 files changed

+174
-223
lines changed

6 files changed

+174
-223
lines changed

lib/any.js

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@
33
module.exports = match
44

55
var zwitch = require('zwitch')
6-
var needsIndex = require('./pseudo').needsIndex
6+
var pseudo = require('./pseudo')
77
var test = require('./test')
88
var nest = require('./nest')
99

10-
var type = zwitch('type')
11-
var handlers = type.handlers
12-
13-
type.unknown = unknownType
14-
type.invalid = invalidType
15-
handlers.selectors = selectors
16-
handlers.ruleSet = ruleSet
17-
handlers.rule = rule
10+
var type = zwitch('type', {
11+
unknown: unknownType,
12+
invalid: invalidType,
13+
handlers: {
14+
selectors: selectors,
15+
ruleSet: ruleSet,
16+
rule: rule
17+
}
18+
})
1819

1920
function match(query, node, state) {
2021
return query && node ? type(query, node, state) : []
2122
}
2223

2324
function selectors(query, node, state) {
2425
var collect = collector(state.one)
25-
var ruleSets = query.selectors
26-
var length = ruleSets.length
2726
var index = -1
2827

29-
while (++index < length) {
30-
collect(ruleSet(ruleSets[index], node, state))
28+
while (++index < query.selectors.length) {
29+
collect(ruleSet(query.selectors[index], node, state))
3130
}
3231

3332
return collect.result
@@ -39,22 +38,27 @@ function ruleSet(query, node, state) {
3938

4039
function rule(query, tree, state) {
4140
var collect = collector(state.one)
42-
var options = {
43-
scopeNodes: tree.type === 'root' ? tree.children : [tree],
44-
iterator: match,
45-
one: state.one,
46-
shallow: state.shallow
47-
}
4841

4942
if (state.shallow && query.rule) {
5043
throw new Error('Expected selector without nesting')
5144
}
5245

53-
nest(query, tree, 0, null, configure(query, options))
46+
nest(
47+
query,
48+
tree,
49+
0,
50+
null,
51+
configure(query, {
52+
scopeNodes: tree.type === 'root' ? tree.children : [tree],
53+
iterator: iterator,
54+
one: state.one,
55+
shallow: state.shallow
56+
})
57+
)
5458

5559
return collect.result
5660

57-
function match(query, node, index, parent, state) {
61+
function iterator(query, node, index, parent, state) {
5862
if (test(query, node, index, parent, state)) {
5963
if (query.rule) {
6064
nest(query.rule, node, index, parent, configure(query.rule, state))
@@ -64,21 +68,20 @@ function rule(query, tree, state) {
6468
}
6569
}
6670
}
71+
}
6772

68-
function configure(query, state) {
69-
var pseudos = query.pseudos
70-
var length = pseudos && pseudos.length
71-
var index = -1
73+
function configure(query, state) {
74+
var pseudos = query.pseudos || []
75+
var index = -1
7276

73-
while (++index < length) {
74-
if (needsIndex.indexOf(pseudos[index].name) !== -1) {
75-
state.index = true
76-
break
77-
}
77+
while (++index < pseudos.length) {
78+
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
79+
state.index = true
80+
break
7881
}
79-
80-
return state
8182
}
83+
84+
return state
8285
}
8386

8487
/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
@@ -100,35 +103,26 @@ function collector(one) {
100103
return collect
101104

102105
/* Append nodes to array, filtering out duplicates. */
103-
function collect(source) {
104-
if ('length' in source) {
105-
collectAll()
106-
} else {
107-
collectOne(source)
108-
}
109-
110-
function collectAll() {
111-
var length = source.length
112-
var index = -1
106+
function collect(node) {
107+
var index = -1
113108

114-
while (++index < length) {
115-
collectOne(source[index])
109+
if ('length' in node) {
110+
while (++index < node.length) {
111+
collectOne(node[index])
116112
}
113+
} else {
114+
collectOne(node)
117115
}
116+
}
118117

119-
function collectOne(node) {
120-
if (one) {
121-
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
122-
if (found) {
123-
throw new Error('Cannot collect multiple nodes')
124-
}
125-
126-
found = true
127-
}
118+
function collectOne(node) {
119+
if (one) {
120+
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
121+
if (found) throw new Error('Cannot collect multiple nodes')
128122

129-
if (result.indexOf(node) === -1) {
130-
result.push(node)
131-
}
123+
found = true
132124
}
125+
126+
if (result.indexOf(node) < 0) result.push(node)
133127
}
134128
}

lib/attribute.js

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,56 @@ module.exports = match
44

55
var zwitch = require('zwitch')
66

7-
var handle = zwitch('operator')
8-
var handlers = handle.handlers
9-
10-
handle.unknown = unknownOperator
11-
handle.invalid = exists
12-
handlers['='] = exact
13-
handlers['^='] = begins
14-
handlers['$='] = ends
15-
handlers['*='] = containsString
16-
handlers['~='] = containsArray
7+
var handle = zwitch('operator', {
8+
unknown: unknownOperator,
9+
invalid: exists,
10+
handlers: {
11+
'=': exact,
12+
'^=': begins,
13+
'$=': ends,
14+
'*=': containsString,
15+
'~=': containsArray
16+
}
17+
})
1718

1819
function match(query, node) {
1920
var attrs = query.attrs
20-
var length = attrs.length
2121
var index = -1
22-
var attr
2322

24-
while (++index < length) {
25-
attr = attrs[index]
26-
27-
if (!handle(attr, node)) {
28-
return false
29-
}
23+
while (++index < attrs.length) {
24+
if (!handle(attrs[index], node)) return false
3025
}
3126

3227
return true
3328
}
3429

3530
// [attr]
3631
function exists(query, node) {
37-
return has(node, query.name)
32+
return node[query.name] != null
3833
}
3934

4035
// [attr=value]
4136
function exact(query, node) {
42-
return has(node, query.name) && String(node[query.name]) === query.value
37+
return node[query.name] != null && String(node[query.name]) === query.value
4338
}
4439

4540
// [attr~=value]
4641
function containsArray(query, node) {
47-
var value
48-
49-
if (has(node, query.name)) {
50-
value = node[query.name]
51-
52-
// If this is an array, and the query is contained in it, return true.
53-
if (
54-
typeof value === 'object' &&
55-
'length' in value &&
56-
value.indexOf(query.value) !== -1
57-
) {
58-
return true
59-
}
60-
61-
// For all other values, return whether this is an exact match.
62-
return String(value) === query.value
42+
var value = node[query.name]
43+
44+
if (value == null) return false
45+
46+
// If this is an array, and the query is contained in it, return true.
47+
if (
48+
typeof value === 'object' &&
49+
'length' in value &&
50+
value.indexOf(query.value) > -1
51+
) {
52+
return true
6353
}
6454

65-
return false
55+
// For all other values, return whether this is an exact match.
56+
return String(value) === query.value
6657
}
6758

6859
// [attr^=value]
@@ -88,14 +79,10 @@ function ends(query, node) {
8879
// [attr*=value]
8980
function containsString(query, node) {
9081
var value = node[query.name]
91-
return typeof value === 'string' && value.indexOf(query.value) !== -1
82+
return typeof value === 'string' && value.indexOf(query.value) > -1
9283
}
9384

9485
/* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */
9586
function unknownOperator(query) {
9687
throw new Error('Unknown operator `' + query.operator + '`')
9788
}
98-
99-
function has(node, name) {
100-
return node[name] !== null && node[name] !== undefined
101-
}

0 commit comments

Comments
 (0)