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

Merged
merged 4 commits into from
May 21, 2025
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 12.0.0-alpha.14 (Unreleased)

#### :rocket: New Feature

- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461

#### :bug: Bug fix

- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
Expand Down
1 change: 1 addition & 0 deletions runtime/Stdlib_RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,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"
14 changes: 14 additions & 0 deletions runtime/Stdlib_RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ 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.

Expand Down
41 changes: 41 additions & 0 deletions tests/tests/src/core/Core_RegExpTest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";

let eq = Primitive_object.equal;

Test.run([
[
"Core_RegExpTest.res",
5,
13,
33
],
"RegExp.flags basic"
], new RegExp("\\w+", "gi").flags, eq, "gi");

Test.run([
[
"Core_RegExpTest.res",
13,
13,
35
],
"RegExp.flags sorting"
], new RegExp("\\w+", "igd").flags, eq, "dgi");

Test.run([
[
"Core_RegExpTest.res",
20,
20,
40
],
"RegExp.flags empty"
], new RegExp("\\w+").flags, eq, "");

export {
eq,
}
/* Not a pure module */
20 changes: 20 additions & 0 deletions tests/tests/src/core/Core_RegExpTest.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let eq = (a, b) => a == b

// 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, "")
7 changes: 4 additions & 3 deletions tests/tests/src/core/Core_TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Core_TestTests from "./Core_TestTests.mjs";
import * as Core_ArrayTests from "./Core_ArrayTests.mjs";
import * as Core_ErrorTests from "./Core_ErrorTests.mjs";
import * as Core_FloatTests from "./Core_FloatTests.mjs";
import * as Core_RegExpTest from "./Core_RegExpTest.mjs";
import * as Core_ObjectTests from "./Core_ObjectTests.mjs";
import * as Core_PromiseTest from "./Core_PromiseTest.mjs";
import * as Core_ResultTests from "./Core_ResultTests.mjs";
Expand Down Expand Up @@ -84,8 +85,6 @@ let PatternMatching = Core_DictTests.PatternMatching;

let Has = Core_DictTests.Has;

let eq = Core_IteratorTests.eq;

let iterator = Core_IteratorTests.iterator;

let syncResult = Core_IteratorTests.syncResult;
Expand All @@ -94,6 +93,8 @@ let asyncResult = Core_IteratorTests.asyncResult;

let asyncIterator = Core_IteratorTests.asyncIterator;

let eq = Core_RegExpTest.eq;

export {
bign,
TestError,
Expand Down Expand Up @@ -130,10 +131,10 @@ export {
intDict,
PatternMatching,
Has,
eq,
iterator,
syncResult,
asyncResult,
asyncIterator,
eq,
}
/* Core_IntTests Not a pure module */
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