Skip to content

Add RegExp.flags function with tests #254

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: main
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 src/Core__RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ 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"
17 changes: 17 additions & 0 deletions src/Core__RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,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 the flags of this `RegExp`.

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

## Examples
```rescript
let regexp1 = RegExp.fromStringWithFlags("\\w+", ~flags="gi")
Console.log(regexp1->RegExp.flags) // Logs `"gi"`, the flags of the RegExp

let regexp2 = RegExp.fromStringWithFlags("\\w+", ~flags="myu")
Console.log(regexp2->RegExp.flags) // Logs `"muy"`, the flags sorted alphabetically
```
*/
@get
external flags: t => string = "flags"
41 changes: 41 additions & 0 deletions test/RegExpTests.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 Caml_obj from "rescript/lib/es6/caml_obj.js";

var eq = Caml_obj.equal;

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

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

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

export {
eq ,
}
/* Not a pure module */
22 changes: 22 additions & 0 deletions test/RegExpTests.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
open RescriptCore

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 test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as ErrorTests from "./ErrorTests.mjs";
import * as FloatTests from "./FloatTests.mjs";
import * as ObjectTests from "./ObjectTests.mjs";
import * as PromiseTest from "./PromiseTest.mjs";
import * as RegExpTests from "./RegExpTests.mjs";
import * as ResultTests from "./ResultTests.mjs";
import * as IteratorTests from "./IteratorTests.mjs";
import * as NullableTests from "./NullableTests.mjs";
Expand Down Expand Up @@ -72,8 +73,6 @@ var decodeJsonTest = JsonTests.decodeJsonTest;

var shouldHandleNullableValues = NullableTests.shouldHandleNullableValues;

var eq = IteratorTests.eq;

var iterator = IteratorTests.iterator;

var syncResult = IteratorTests.syncResult;
Expand All @@ -82,6 +81,8 @@ var asyncResult = IteratorTests.asyncResult;

var asyncIterator = IteratorTests.asyncIterator;

var eq = RegExpTests.eq;

export {
bign ,
TestError ,
Expand Down Expand Up @@ -112,10 +113,10 @@ export {
o ,
decodeJsonTest ,
shouldHandleNullableValues ,
eq ,
iterator ,
syncResult ,
asyncResult ,
asyncIterator ,
eq ,
}
/* IntTests Not a pure module */
1 change: 1 addition & 0 deletions test/TestSuite.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include JsonTests
include NullableTests
include DictTests
include IteratorTests
include RegExpTests