Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit4fe0e8d

Browse files
committed
Finish named capture groups support and test case
1 parent7c00f21 commit4fe0e8d

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

‎lib/Builder.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,26 @@ class Builder {
682682
returnresult
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+
constnames=this._captureNames
695+
696+
returnArray.prototype.reduce.call(result.slice(1),(result,current,index)=>{
697+
if(names[index]){
698+
result[names[index]]=current||''
699+
}
700+
701+
returnresult
702+
},result)
703+
}
704+
685705
/**
686706
* Just like match in String, but reset lastIndex.
687707
*
@@ -692,7 +712,8 @@ class Builder {
692712
constregex=this.get()
693713
constresult=regex.exec(target)
694714
regex.lastIndex=0
695-
returnresult
715+
716+
returnthis._mapCaptureIndexToName(result)
696717
}
697718

698719
/**
@@ -705,6 +726,7 @@ class Builder {
705726
lettemp=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‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function buildData(lines) {
3838
captures:{}
3939
}
4040
letinCapture=false
41-
letcaptures=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
}elseif(line.startsWith('match: "')){
6054
data.matches.push(applySpecialChars(line.slice(8,-1)))
6155
}elseif(line.startsWith('no match: "')){
@@ -66,23 +60,15 @@ function buildData(lines) {
6660
){
6761
inCapture=line.slice(13,-2)
6862
data.captures[inCapture]=[]
69-
}elseif(
70-
inCapture&&
71-
line.startsWith('-')
72-
){
63+
}elseif(inCapture&&line.startsWith('-')){
7364
constsplit=line.substr(1).split(': ')
74-
constindex=captures.indexOf(split[1].trim())
7565
lettarget=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-
constresult=Array.from(capture).slice(1).map((item)=>{
137-
returnitem===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+
constitem=expected[index]
126+
127+
for(letkeyinitem){
128+
if(typeofkey==='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)

[8]ページ先頭

©2009-2025 Movatter.jp