Skip to content

Commit

Permalink
Refactor to improve bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 31, 2020
1 parent 652e249 commit f8f1c81
Showing 1 changed file with 38 additions and 53 deletions.
91 changes: 38 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,56 @@ module.exports = zone
var splice = [].splice

function zone(node, name, callback) {
var nodes = []
var start = null
var scope = null
var level = 0
var position
var level
var marker
var scope

visit(node, gather)

// Gather one dimensional zones.
function gather(node, index, parent) {
var type = test(node)

if (scope && parent === scope) {
if (type === 'start') {
level++
}

if (type === 'end') {
level--
var info = commentMarker(node)
var match =
info && info.name === name && info.attributes.match(/(start|end)\b/)
var type = match && match[0]
var start
var result

if (type) {
if (!scope && type === 'start') {
level = 0
marker = node
scope = parent
}

if (type === 'end' && level === 0) {
nodes = callback(start, nodes, node, {
start: index - nodes.length - 1,
end: index,
parent: scope
})

if (nodes) {
splice.apply(
scope.children,
[position, index - position + 1].concat(nodes)
)
if (scope && parent === scope) {
if (type === 'start') {
level++
} else {
level--
}

start = null
scope = null
position = null
nodes = []
} else {
nodes.push(node)
}
}
if (type === 'end' && !level) {
start = scope.children.indexOf(marker)

if (!scope && type === 'start') {
level = 1
position = index
start = node
scope = parent
}
}
result = callback(
marker,
scope.children.slice(start + 1, index),
node,
{start: start, end: index, parent: parent}
)

// Test if `node` matches the bound settings.
function test(node) {
var marker = commentMarker(node)
var attributes
var head
if (result) {
splice.apply(
scope.children,
[start, index - start + 1].concat(result)
)
}

if (!marker || marker.name !== name) {
return null
marker = undefined
scope = undefined
}
}
}

attributes = marker.attributes
head = attributes.match(/(start|end)\b/)

return head ? head[0] : null
}
}

0 comments on commit f8f1c81

Please # to comment.