Simon Priet 9e2991e668 init
2021-09-05 22:53:58 +02:00

44 lines
840 B
JavaScript

const Group = require('./group')
class GroupBuilder {
constructor() {
this._groupBuilders = []
this._capturing = true
}
add(groupBuilder) {
this._groupBuilders.push(groupBuilder)
}
build(match, nextGroupIndex) {
const groupIndex = nextGroupIndex()
const children = this._groupBuilders.map(gb =>
gb.build(match, nextGroupIndex)
)
return new Group(
match[groupIndex],
match.index[groupIndex],
match.index[groupIndex] + (match[groupIndex] || '').length,
children
)
}
setNonCapturing() {
this._capturing = false
}
get capturing() {
return this._capturing
}
get children() {
return this._groupBuilders
}
moveChildrenTo(groupBuilder) {
this._groupBuilders.forEach(child => groupBuilder.add(child))
}
}
module.exports = GroupBuilder