Skip to content

Commit 4fe0e8d

Browse files
committed
Finish named capture groups support and test case
1 parent 7c00f21 commit 4fe0e8d

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

lib/Builder.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,26 @@ class Builder {
682682
return result
683683
}
684684

685+
/**
686+
* Map capture index to name.
687+
* When `exec` give the result like: [ 'aa ', 'aa', index: 0, input: 'aa bb cc dd' ]
688+
* Then help to resolve to return: [ 'aa ', 'aa', index: 0, input: 'aa bb cc dd', [captureName]: 'aa' ]
689+
*
690+
* @param {object} result
691+
* @return {object}
692+
*/
693+
_mapCaptureIndexToName(result) {
694+
const names = this._captureNames
695+
696+
return Array.prototype.reduce.call(result.slice(1), (result, current, index) => {
697+
if (names[index]) {
698+
result[names[index]] = current || ''
699+
}
700+
701+
return result
702+
}, result)
703+
}
704+
685705
/**
686706
* Just like match in String, but reset lastIndex.
687707
*
@@ -692,7 +712,8 @@ class Builder {
692712
const regex = this.get()
693713
const result = regex.exec(target)
694714
regex.lastIndex = 0
695-
return result
715+
716+
return this._mapCaptureIndexToName(result)
696717
}
697718

698719
/**
@@ -705,6 +726,7 @@ class Builder {
705726
let temp = null
706727

707728
while (temp = regex.exec(target)) {
729+
temp = this._mapCaptureIndexToName(temp)
708730
result.push(temp)
709731
}
710732
regex.lastIndex = 0

test/rules-test.js

+23-26
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function buildData(lines) {
3838
captures: {}
3939
}
4040
let inCapture = false
41-
let captures = null // Remember captures' name and index.
4241

4342
lines.forEach((line) => {
4443
if (line === '' || line.startsWith('#')) {
@@ -50,12 +49,7 @@ function buildData(lines) {
5049
}
5150

5251
if (line.startsWith('srl: ')) {
53-
captures = []
54-
55-
data.srl = line.substr(5).replace(/as\s+"([^"]+)"/g, (s, c) => {
56-
captures.push(c)
57-
return ''
58-
})
52+
data.srl = line.substr(5)
5953
} else if (line.startsWith('match: "')) {
6054
data.matches.push(applySpecialChars(line.slice(8, -1)))
6155
} else if (line.startsWith('no match: "')) {
@@ -66,23 +60,15 @@ function buildData(lines) {
6660
) {
6761
inCapture = line.slice(13, -2)
6862
data.captures[inCapture] = []
69-
} else if (
70-
inCapture &&
71-
line.startsWith('-')
72-
) {
63+
} else if (inCapture && line.startsWith('-')) {
7364
const split = line.substr(1).split(': ')
74-
const index = captures.indexOf(split[1].trim())
7565
let target = data.captures[inCapture][Number(split[0])]
7666

7767
if (!target) {
7868
target = data.captures[inCapture][Number(split[0])] = []
7969
}
8070

81-
if (index !== -1) {
82-
target[index] = applySpecialChars(split[2].slice(1, -1))
83-
} else {
84-
target.push(applySpecialChars(split[2].slice(1, -1)))
85-
}
71+
target[split[1]] = applySpecialChars(split[2].slice(1, -1))
8672
}
8773
})
8874

@@ -133,15 +119,26 @@ function runAssertions(data) {
133119
)
134120

135121
matches.forEach((capture, index) => {
136-
const result = Array.from(capture).slice(1).map((item) => {
137-
return item === undefined ? '' : item
138-
})
139-
140-
assert.deepEqual(
141-
result,
142-
expected[index],
143-
`The capture group did not return the expected results for test ${test}.${getExpression(data.srl, query)}`
144-
)
122+
// const result = Array.from(capture).slice(1).map((item) => {
123+
// return item === undefined ? '' : item
124+
// })
125+
const item = expected[index]
126+
127+
for (let key in item) {
128+
if (typeof key === 'number') {
129+
assert.equal(
130+
capture[key + 1],
131+
item[key],
132+
`The capture group did not return the expected results for test ${test}.${getExpression(data.srl, query)}`
133+
)
134+
} else {
135+
assert.equal(
136+
capture[key],
137+
item[key],
138+
`The capture group did not return the expected results for test ${test}.${getExpression(data.srl, query)}`
139+
)
140+
}
141+
}
145142
})
146143

147144
assertionMade = true

0 commit comments

Comments
 (0)