Skip to content

Commit febf7cb

Browse files
jmagaramzth
andauthored
Add RegExp.flags external and corresponding tests (#7461)
* Add RegExp.flags external and corresponding tests * fix tests * changelog --------- Co-authored-by: Gabriel Nordeborn <gabbe.nord@gmail.com>
1 parent af43e11 commit febf7cb

File tree

7 files changed

+85
-3
lines changed

7 files changed

+85
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 12.0.0-alpha.14 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461
18+
1519
#### :bug: Bug fix
1620

1721
- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497

runtime/Stdlib_RegExp.res

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

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

runtime/Stdlib_RegExp.resi

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

302+
/**
303+
`flags(regexp)` returns a string consisting of all the flags set on this `RegExp`.
304+
305+
See [`RegExp.flags`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags) on MDN.
306+
307+
## Examples
308+
```rescript
309+
let regexp = RegExp.fromString("\\w+", ~flags="gi")
310+
Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp
311+
```
312+
*/
313+
@get
314+
external flags: t => string = "flags"
315+
302316
/**
303317
`ignore(regExp)` ignores the provided regExp and returns unit.
304318
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";
5+
6+
let eq = Primitive_object.equal;
7+
8+
Test.run([
9+
[
10+
"Core_RegExpTest.res",
11+
5,
12+
13,
13+
33
14+
],
15+
"RegExp.flags basic"
16+
], new RegExp("\\w+", "gi").flags, eq, "gi");
17+
18+
Test.run([
19+
[
20+
"Core_RegExpTest.res",
21+
13,
22+
13,
23+
35
24+
],
25+
"RegExp.flags sorting"
26+
], new RegExp("\\w+", "igd").flags, eq, "dgi");
27+
28+
Test.run([
29+
[
30+
"Core_RegExpTest.res",
31+
20,
32+
20,
33+
40
34+
],
35+
"RegExp.flags empty"
36+
], new RegExp("\\w+").flags, eq, "");
37+
38+
export {
39+
eq,
40+
}
41+
/* Not a pure module */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let eq = (a, b) => a == b
2+
3+
// Test for RegExp.flags
4+
Test.run(
5+
__POS_OF__("RegExp.flags basic"),
6+
RegExp.fromStringWithFlags("\\w+", ~flags="gi")->RegExp.flags,
7+
eq,
8+
"gi",
9+
)
10+
11+
// Test for alphabetical sorting of flags
12+
Test.run(
13+
__POS_OF__("RegExp.flags sorting"),
14+
RegExp.fromStringWithFlags("\\w+", ~flags="igd")->RegExp.flags,
15+
eq,
16+
"dgi",
17+
)
18+
19+
// Test with no flags
20+
Test.run(__POS_OF__("RegExp.flags empty"), RegExp.fromString("\\w+")->RegExp.flags, eq, "")

tests/tests/src/core/Core_TestSuite.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Core_TestTests from "./Core_TestTests.mjs";
77
import * as Core_ArrayTests from "./Core_ArrayTests.mjs";
88
import * as Core_ErrorTests from "./Core_ErrorTests.mjs";
99
import * as Core_FloatTests from "./Core_FloatTests.mjs";
10+
import * as Core_RegExpTest from "./Core_RegExpTest.mjs";
1011
import * as Core_ObjectTests from "./Core_ObjectTests.mjs";
1112
import * as Core_PromiseTest from "./Core_PromiseTest.mjs";
1213
import * as Core_ResultTests from "./Core_ResultTests.mjs";
@@ -84,8 +85,6 @@ let PatternMatching = Core_DictTests.PatternMatching;
8485

8586
let Has = Core_DictTests.Has;
8687

87-
let eq = Core_IteratorTests.eq;
88-
8988
let iterator = Core_IteratorTests.iterator;
9089

9190
let syncResult = Core_IteratorTests.syncResult;
@@ -94,6 +93,8 @@ let asyncResult = Core_IteratorTests.asyncResult;
9493

9594
let asyncIterator = Core_IteratorTests.asyncIterator;
9695

96+
let eq = Core_RegExpTest.eq;
97+
9798
export {
9899
bign,
99100
TestError,
@@ -130,10 +131,10 @@ export {
130131
intDict,
131132
PatternMatching,
132133
Has,
133-
eq,
134134
iterator,
135135
syncResult,
136136
asyncResult,
137137
asyncIterator,
138+
eq,
138139
}
139140
/* Core_IntTests Not a pure module */

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)