Skip to content

Commit

Permalink
fix: captureNames
Browse files Browse the repository at this point in the history
  • Loading branch information
2fd committed Dec 15, 2023
1 parent d9e25c6 commit 2331a8c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for t in lib/*.d.ts; do
sed -i '.tmp' -e 's/split(text: string): any;/split(text: string): string[];/g' $t
sed -i '.tmp' -e 's/splitn(text: string, limit: number): any;/splitn(text: string, limit: number): string[];/g' $t
sed -i '.tmp' -e 's/syntax(): any;/syntax(): Hir;/g' $t
sed -i '.tmp' -e 's/captureNames(): any\[\];/captureNames(): string[];/g' $t
sed -i '.tmp' -e 's/captureNames(): any\[\];/captureNames(): (string | null)[];/g' $t
sed -i '.tmp' -e 's/captures(text: string): any;/captures(text: string): Captures | undefined;/g' $t
sed -i '.tmp' -e 's/capturesAll(text: string): any;/capturesAll(text: string): Captures[];/g' $t
sed -i '.tmp' -e 's/matches(text: string): any\[\];/matches(text: string): number[];/g' $t
Expand All @@ -48,7 +48,7 @@ for t in lib/*.d.ts; do
sed -i -e 's/split(text: string): any;/split(text: string): string[];/g' $t
sed -i -e 's/splitn(text: string, limit: number): any;/splitn(text: string, limit: number): string[];/g' $t
sed -i -e 's/syntax(): any;/syntax(): Hir;/g' $t
sed -i -e 's/captureNames(): any\[\];/captureNames(): string[];/g' $t
sed -i -e 's/captureNames(): any\[\];/captureNames(): (string | null)[];/g' $t
sed -i -e 's/captures(text: string): any;/captures(text: string): Captures | undefined;/g' $t
sed -i -e 's/capturesAll(text: string): any;/capturesAll(text: string): Captures[];/g' $t
sed -i -e 's/matches(text: string): any\[\];/matches(text: string): number[];/g' $t
Expand Down
5 changes: 4 additions & 1 deletion src/rregex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ impl RRegex {
pub fn capture_names(&self) -> Vec<JsValue> {
self.regex
.capture_names()
.filter_map(|item| item.map(JsValue::from))
.map(|item| match item {
Some(name) => JsValue::from(name),
None => JsValue::NULL,
})
.collect()
}

Expand Down
1 change: 1 addition & 0 deletions test/rregex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe(`RRegex`, () => {
test(`captureNames`, () => {
const regex = new RRegex('(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})')
expect(regex.captureNames()).toEqual([
null,
"y",
"m",
"d",
Expand Down

0 comments on commit 2331a8c

Please sign in to comment.