Skip to content

Commit 92c284b

Browse files
committed
Add RegExp.flags external and corresponding tests
1 parent bece9c7 commit 92c284b

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

runtime/Stdlib_RegExp.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module Result = {
2222
@get external source: t => string = "source"
2323
@get external sticky: t => bool = "sticky"
2424
@get external unicode: t => bool = "unicode"
25+
@get external flags: t => string = "flags"
2526

2627
external ignore: t => unit = "%ignore"

runtime/Stdlib_RegExp.resi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,25 @@ Console.log(regexp2->RegExp.unicode) // Logs `true`, since `u` is set
298298
@get
299299
external unicode: t => bool = "unicode"
300300

301+
/**
302+
`flags(regexp)` returns a string consisting of all the flags set on this `RegExp`.
303+
304+
See [`RegExp.flags`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags) on MDN.
305+
306+
## Examples
307+
```rescript
308+
let regexp = RegExp.fromString("\\w+", ~flags="gi")
309+
Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp
310+
```
311+
*/
312+
@get
313+
external flags: t => string = "flags"
314+
301315
/**
302316
`ignore(regExp)` ignores the provided regExp and returns unit.
303317
304318
This helper is useful when you want to discard a value (for example, the result of an operation with side effects)
305319
without having to store or process it further.
306320
*/
307321
external ignore: t => unit = "%ignore"
322+
//
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test for RegExp.flags
2+
Test.run(
3+
__POS_OF__("RegExp.flags basic"),
4+
RegExp.fromStringWithFlags("\\w+", ~flags="gi")->RegExp.flags,
5+
eq,
6+
"gi",
7+
)
8+
9+
// Test for alphabetical sorting of flags
10+
Test.run(
11+
__POS_OF__("RegExp.flags sorting"),
12+
RegExp.fromStringWithFlags("\\w+", ~flags="igd")->RegExp.flags,
13+
eq,
14+
"dgi",
15+
)
16+
17+
// Test with no flags
18+
Test.run(__POS_OF__("RegExp.flags empty"), RegExp.fromString("\\w+")->RegExp.flags, eq, "")

tests/tests/src/core/Core_TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ include Core_JsonTests
1111
include Core_NullableTests
1212
include Core_DictTests
1313
include Core_IteratorTests
14+
include Core_RegExpTest

0 commit comments

Comments
 (0)