Skip to content

Commit 99650d0

Browse files
authored
Merge pull request #1908 from glennsl/re-setlastindex
Add Js.Re.setLastIndex
2 parents 3970ebf + b5528f2 commit 99650d0

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

jscomp/others/js_re.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ done
131131
*)
132132
external lastIndex : t -> int = "" [@@bs.get]
133133

134+
(** sets the index at which the next match will start its search from *)
135+
external setLastIndex : t -> int -> unit = "lastIndex" [@@bs.set]
136+
134137
(** returns a bool indicating whether the [multiline] flag is set *)
135138
external multiline : t -> bool = "" [@@bs.get]
136139

jscomp/test/js_re_test.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,45 +128,66 @@ var suites_001 = /* :: */[
128128
],
129129
/* :: */[
130130
/* tuple */[
131-
"t_multiline",
131+
"t_setLastIndex",
132132
(function () {
133+
var re = (/na/g);
134+
var before = re.lastIndex;
135+
re.lastIndex = 42;
136+
var after = re.lastIndex;
133137
return /* Eq */Block.__(0, [
134-
/* false */0,
135-
+(/./ig).multiline
138+
/* tuple */[
139+
0,
140+
42
141+
],
142+
/* tuple */[
143+
before,
144+
after
145+
]
136146
]);
137147
})
138148
],
139149
/* :: */[
140150
/* tuple */[
141-
"t_source",
151+
"t_multiline",
142152
(function () {
143153
return /* Eq */Block.__(0, [
144-
"f.+o",
145-
(/f.+o/ig).source
154+
/* false */0,
155+
+(/./ig).multiline
146156
]);
147157
})
148158
],
149159
/* :: */[
150160
/* tuple */[
151-
"t_sticky",
161+
"t_source",
152162
(function () {
153163
return /* Eq */Block.__(0, [
154-
/* true */1,
155-
+(/./yg).sticky
164+
"f.+o",
165+
(/f.+o/ig).source
156166
]);
157167
})
158168
],
159169
/* :: */[
160170
/* tuple */[
161-
"t_unicode",
171+
"t_sticky",
162172
(function () {
163173
return /* Eq */Block.__(0, [
164-
/* false */0,
165-
+(/./yg).unicode
174+
/* true */1,
175+
+(/./yg).sticky
166176
]);
167177
})
168178
],
169-
/* [] */0
179+
/* :: */[
180+
/* tuple */[
181+
"t_unicode",
182+
(function () {
183+
return /* Eq */Block.__(0, [
184+
/* false */0,
185+
+(/./yg).unicode
186+
]);
187+
})
188+
],
189+
/* [] */0
190+
]
170191
]
171192
]
172193
]

jscomp/test/js_re_test.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ let suites = Mt.[
6060

6161
Eq(4, re |> Js.Re.lastIndex)
6262
);
63+
"t_setLastIndex", (fun _ ->
64+
let re = [%re "/na/g"] in
65+
66+
let before = Js.Re.lastIndex re in
67+
let () = Js.Re.setLastIndex re 42 in
68+
let after = Js.Re.lastIndex re in
69+
70+
Eq((0, 42), (before, after))
71+
);
6372
"t_multiline", (fun _ ->
6473
Eq(false, [%re "/./ig"] |> Js.Re.multiline)
6574
);

0 commit comments

Comments
 (0)