Skip to content

Commit f8f1c81

Browse files
committed
Refactor to improve bundle size
1 parent 652e249 commit f8f1c81

File tree

1 file changed

+38
-53
lines changed

1 file changed

+38
-53
lines changed

index.js

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,56 @@ module.exports = zone
88
var splice = [].splice
99

1010
function zone(node, name, callback) {
11-
var nodes = []
12-
var start = null
13-
var scope = null
14-
var level = 0
15-
var position
11+
var level
12+
var marker
13+
var scope
1614

1715
visit(node, gather)
1816

1917
// Gather one dimensional zones.
2018
function gather(node, index, parent) {
21-
var type = test(node)
22-
23-
if (scope && parent === scope) {
24-
if (type === 'start') {
25-
level++
26-
}
27-
28-
if (type === 'end') {
29-
level--
19+
var info = commentMarker(node)
20+
var match =
21+
info && info.name === name && info.attributes.match(/(start|end)\b/)
22+
var type = match && match[0]
23+
var start
24+
var result
25+
26+
if (type) {
27+
if (!scope && type === 'start') {
28+
level = 0
29+
marker = node
30+
scope = parent
3031
}
3132

32-
if (type === 'end' && level === 0) {
33-
nodes = callback(start, nodes, node, {
34-
start: index - nodes.length - 1,
35-
end: index,
36-
parent: scope
37-
})
38-
39-
if (nodes) {
40-
splice.apply(
41-
scope.children,
42-
[position, index - position + 1].concat(nodes)
43-
)
33+
if (scope && parent === scope) {
34+
if (type === 'start') {
35+
level++
36+
} else {
37+
level--
4438
}
4539

46-
start = null
47-
scope = null
48-
position = null
49-
nodes = []
50-
} else {
51-
nodes.push(node)
52-
}
53-
}
40+
if (type === 'end' && !level) {
41+
start = scope.children.indexOf(marker)
5442

55-
if (!scope && type === 'start') {
56-
level = 1
57-
position = index
58-
start = node
59-
scope = parent
60-
}
61-
}
43+
result = callback(
44+
marker,
45+
scope.children.slice(start + 1, index),
46+
node,
47+
{start: start, end: index, parent: parent}
48+
)
6249

63-
// Test if `node` matches the bound settings.
64-
function test(node) {
65-
var marker = commentMarker(node)
66-
var attributes
67-
var head
50+
if (result) {
51+
splice.apply(
52+
scope.children,
53+
[start, index - start + 1].concat(result)
54+
)
55+
}
6856

69-
if (!marker || marker.name !== name) {
70-
return null
57+
marker = undefined
58+
scope = undefined
59+
}
60+
}
7161
}
72-
73-
attributes = marker.attributes
74-
head = attributes.match(/(start|end)\b/)
75-
76-
return head ? head[0] : null
7762
}
7863
}

0 commit comments

Comments
 (0)