Skip to content

Add RegExp.flags external and corresponding tests #7461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/Stdlib_RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ module Result = {
@get external source: t => string = "source"
@get external sticky: t => bool = "sticky"
@get external unicode: t => bool = "unicode"
@get external flags: t => string = "flags"

external ignore: t => unit = "%ignore"
15 changes: 15 additions & 0 deletions runtime/Stdlib_RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,25 @@ Console.log(regexp2->RegExp.unicode) // Logs `true`, since `u` is set
@get
external unicode: t => bool = "unicode"

/**
`flags(regexp)` returns a string consisting of all the flags set on this `RegExp`.

See [`RegExp.flags`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags) on MDN.

## Examples
```rescript
let regexp = RegExp.fromString("\\w+", ~flags="gi")
Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp
```
*/
@get
external flags: t => string = "flags"

/**
`ignore(regExp)` ignores the provided regExp and returns unit.

This helper is useful when you want to discard a value (for example, the result of an operation with side effects)
without having to store or process it further.
*/
external ignore: t => unit = "%ignore"
//
18 changes: 18 additions & 0 deletions tests/tests/src/core/Core_RegExpTest.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Test for RegExp.flags
Test.run(
__POS_OF__("RegExp.flags basic"),
RegExp.fromStringWithFlags("\\w+", ~flags="gi")->RegExp.flags,
eq,
"gi",
)

// Test for alphabetical sorting of flags
Test.run(
__POS_OF__("RegExp.flags sorting"),
RegExp.fromStringWithFlags("\\w+", ~flags="igd")->RegExp.flags,
eq,
"dgi",
)

// Test with no flags
Test.run(__POS_OF__("RegExp.flags empty"), RegExp.fromString("\\w+")->RegExp.flags, eq, "")
1 change: 1 addition & 0 deletions tests/tests/src/core/Core_TestSuite.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include Core_JsonTests
include Core_NullableTests
include Core_DictTests
include Core_IteratorTests
include Core_RegExpTest
Loading