Skip to content

Commit 687f92d

Browse files
committed
Add some functions to Belt.String
1 parent ce02575 commit 687f92d

File tree

8 files changed

+184
-43
lines changed

8 files changed

+184
-43
lines changed

jscomp/others/belt.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ module Int = Belt_Int
266266
module Float = Belt_Float
267267

268268

269+
(** {!Belt.String}
270+
271+
Utilities for String.
272+
*)
273+
274+
module String = Belt_String
275+
276+
269277
(** {!Belt.Debug}
270278
271279
Utilities for set up debugging

jscomp/others/belt_String.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ external concatMany : t -> t array -> t = "concat" [@@bs.send] [@@bs.splice]
3636

3737
external endsWith : t -> t -> bool = "endsWith" [@@bs.send]
3838

39-
external indexOf : t -> t -> int option = "indexOf" [@@bs.send]
39+
external indexOf : t -> t -> int = "indexOf" [@@bs.send]
4040

4141
let indexOf s searchValue =
4242
match indexOf s searchValue with
@@ -59,23 +59,23 @@ external splitAtMost: t -> t -> int -> t array = "split" [@@bs.send]
5959

6060
external startsWith : t -> t -> bool = "startsWith" [@@bs.send]
6161

62-
external substring : t -> from:int -> len:int -> t = "substring" [@@bs.send]
62+
external substr : t -> from:int -> len:int -> t = "substr" [@@bs.send]
6363

64-
external substringToEnd : t -> from:int -> t = "substring" [@@bs.send]
64+
external substrToEnd : t -> from:int -> t = "substr" [@@bs.send]
6565

6666
external slice : t -> from:int -> to_:int -> t = "slice" [@@bs.send]
6767

68-
external sliceToEnd : t -> from:int = "slice" [@@bs.send]
68+
external sliceToEnd : t -> from:int -> t = "slice" [@@bs.send]
6969

7070
external trim : t -> t = "trim" [@@bs.send]
7171

7272
external trimStart : t -> t = "trimStart" [@@bs.send] (** ES2015 *)
7373

7474
external trimEnd : t -> t = "trimEnd" [@@bs.send] (** ES2015 *)
7575

76-
external padStart : t -> t = "padStart" [@@bs.send] (** ES2015 *)
76+
external padStart : t -> int -> t -> t = "padStart" [@@bs.send] (** ES2015 *)
7777

78-
external padEnd : t -> t = "padEnd" [@@bs.send] (** ES2015 *)
78+
external padEnd : t -> int -> t -> t = "padEnd" [@@bs.send] (** ES2015 *)
7979

8080
external toLowerCase : t -> t = "toLowerCase" [@@bs.send]
8181

jscomp/others/belt_String.mli

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,34 +192,34 @@ external startsWith : t -> t -> bool = "startsWith" [@@bs.send]
192192

193193

194194
(**
195-
[substring str ~from: n] returns the substring of [str] from position [n] to the end of the string.
195+
[substr str ~from: n] returns the substring of [str] from position [n] to the end of the string.
196196
197197
If [n] is less than zero, the starting position is the length of [str] - [n].
198198
199199
If [n] is greater than or equal to the length of [str], returns the empty string.
200200
201201
@example {[
202-
substring "abcdefghij" ~from: 3 ~len: 2 = "de"
203-
substring "abcdefghij" ~from: (-3) ~len: 3 = "hij"
204-
substring "abcdefghij" ~from: 12 ~len: 2= ""
202+
substr "abcdefghij" ~from: 3 ~len: 2 = "de"
203+
substr "abcdefghij" ~from: (-3) ~len: 3 = "hij"
204+
substr "abcdefghij" ~from: 12 ~len: 2= ""
205205
]}
206206
*)
207-
external substring : t -> from:int -> len:int -> t = "substring" [@@bs.send]
207+
external substr : t -> from:int -> len:int -> t = "substr" [@@bs.send]
208208

209209
(**
210-
[substringToEnd str ~from: n] returns the substring of [str] from position [n] to the end of the string.
210+
[substrToEnd str ~from: n] returns the substring of [str] from position [n] to the end of the string.
211211
212212
If [n] is less than zero, the starting position is the length of [str] - [n].
213213
214214
If [n] is greater than or equal to the length of [str], returns the empty string.
215215
216216
@example {[
217-
substringToEnd "abcdefghij" ~from: 3 = "defghij"
218-
substringToEnd "abcdefghij" ~from: (-3) = "hij"
219-
substringToEnd "abcdefghij" ~from: 12 = ""
217+
substrToEnd "abcdefghij" ~from: 3 = "defghij"
218+
substrToEnd "abcdefghij" ~from: (-3) = "hij"
219+
substrToEnd "abcdefghij" ~from: 12 = ""
220220
]}
221221
*)
222-
external substringToEnd : t -> from:int -> t = "substring" [@@bs.send]
222+
external substrToEnd : t -> from:int -> t = "substr" [@@bs.send]
223223

224224
(** [slice from:n1 to_:n2 str] returns the substring of [str] starting at character [n1] up to but not including [n2]
225225
@@ -250,7 +250,7 @@ If [n] is greater than the length of [str], then [sliceToEnd] returns the empty
250250
sliceToEnd "abcdefg" ~from: 7 == "";;
251251
]}
252252
*)
253-
external sliceToEnd : t -> from:int = "slice" [@@bs.send]
253+
external sliceToEnd : t -> from:int -> t = "slice" [@@bs.send]
254254

255255
(**
256256
[trim str] returns a string that is [str] with whitespace stripped from both ends. Internal whitespace is not removed.
@@ -291,7 +291,7 @@ external trimEnd : t -> t = "trimEnd" [@@bs.send] (** ES2015 *)
291291
padStart "444" 3 "0" = "444"
292292
]}
293293
*)
294-
external padStart : t -> t = "padStart" [@@bs.send] (** ES2015 *)
294+
external padStart : t -> int -> t -> t = "padStart" [@@bs.send] (** ES2015 *)
295295

296296
(**
297297
[padEnd str int padStr] returns a string that is [str] padded to the right with padStr.
@@ -301,7 +301,7 @@ external padStart : t -> t = "padStart" [@@bs.send] (** ES2015 *)
301301
padEnd "444" 3 "0" = "444"
302302
]}
303303
*)
304-
external padEnd : t -> t = "padEnd" [@@bs.send] (** ES2015 *)
304+
external padEnd : t -> int -> t -> t = "padEnd" [@@bs.send] (** ES2015 *)
305305

306306
(**
307307
[toLowerCase str] converts [str] to lower case using the locale-insensitive case mappings in the Unicode Character Database. Notice that the conversion can give different results depending upon context, for example with the Greek letter sigma, which has two different lower case forms when it is the last character in a string or not.

jscomp/others/release.ninja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ build others/belt_SortArrayInt.cmj : cc others/belt_SortArrayInt.ml | others/bel
118118
build others/belt_SortArrayInt.cmi : cc others/belt_SortArrayInt.mli | runtime
119119
build others/belt_SortArrayString.cmj : cc others/belt_SortArrayString.ml | others/belt.cmi others/belt_Array.cmj others/belt_SortArrayString.cmi js_pkg runtime
120120
build others/belt_SortArrayString.cmi : cc others/belt_SortArrayString.mli | runtime
121+
build others/belt_String.cmj : cc others/belt_String.ml | others/belt.cmi others/belt_String.cmi others/js_re.cmi js_pkg runtime
122+
build others/belt_String.cmi : cc others/belt_String.mli | others/belt.cmi others/js_re.cmi js_pkg runtime
121123
build others/belt_internalAVLset.cmj : cc others/belt_internalAVLset.ml | others/belt.cmi others/belt_Array.cmj others/belt_Id.cmj others/belt_SortArray.cmj others/belt_internalAVLset.cmi js_pkg runtime
122124
build others/belt_internalAVLset.cmi : cc others/belt_internalAVLset.mli | others/belt.cmi others/belt_Id.cmi js_pkg runtime
123125
build others/belt_internalAVLtree.cmj : cc others/belt_internalAVLtree.ml | others/belt.cmi others/belt_Array.cmj others/belt_Id.cmj others/belt_SortArray.cmj others/belt_internalAVLtree.cmi js_pkg runtime
@@ -142,4 +144,4 @@ build others/node_module.cmi others/node_module.cmj : cc others/node_module.ml |
142144
build others/node_path.cmi others/node_path.cmj : cc others/node_path.ml | runtime
143145
build others/node_process.cmj : cc others/node_process.ml | others/js_dict.cmj others/node.cmi others/node_process.cmi js_pkg runtime
144146
build others/node_process.cmi : cc others/node_process.mli | others/js_dict.cmi others/node.cmi js_pkg runtime
145-
build others : phony others/belt_Array.cmi others/belt_Array.cmj others/belt_Debug.cmi others/belt_Debug.cmj others/belt_Float.cmi others/belt_Float.cmj others/belt_HashMap.cmi others/belt_HashMap.cmj others/belt_HashMapInt.cmi others/belt_HashMapInt.cmj others/belt_HashMapString.cmi others/belt_HashMapString.cmj others/belt_HashSet.cmi others/belt_HashSet.cmj others/belt_HashSetInt.cmi others/belt_HashSetInt.cmj others/belt_HashSetString.cmi others/belt_HashSetString.cmj others/belt_Id.cmi others/belt_Id.cmj others/belt_Int.cmi others/belt_Int.cmj others/belt_List.cmi others/belt_List.cmj others/belt_Map.cmi others/belt_Map.cmj others/belt_MapDict.cmi others/belt_MapDict.cmj others/belt_MapInt.cmi others/belt_MapInt.cmj others/belt_MapString.cmi others/belt_MapString.cmj others/belt_MutableMap.cmi others/belt_MutableMap.cmj others/belt_MutableMapInt.cmi others/belt_MutableMapInt.cmj others/belt_MutableMapString.cmi others/belt_MutableMapString.cmj others/belt_MutableQueue.cmi others/belt_MutableQueue.cmj others/belt_MutableSet.cmi others/belt_MutableSet.cmj others/belt_MutableSetInt.cmi others/belt_MutableSetInt.cmj others/belt_MutableSetString.cmi others/belt_MutableSetString.cmj others/belt_MutableStack.cmi others/belt_MutableStack.cmj others/belt_Option.cmi others/belt_Option.cmj others/belt_Range.cmi others/belt_Range.cmj others/belt_Result.cmi others/belt_Result.cmj others/belt_Set.cmi others/belt_Set.cmj others/belt_SetDict.cmi others/belt_SetDict.cmj others/belt_SetInt.cmi others/belt_SetInt.cmj others/belt_SetString.cmi others/belt_SetString.cmj others/belt_SortArray.cmi others/belt_SortArray.cmj others/belt_SortArrayInt.cmi others/belt_SortArrayInt.cmj others/belt_SortArrayString.cmi others/belt_SortArrayString.cmj others/belt_internalAVLset.cmi others/belt_internalAVLset.cmj others/belt_internalAVLtree.cmi others/belt_internalAVLtree.cmj others/belt_internalBuckets.cmi others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmi others/belt_internalBucketsType.cmj others/belt_internalMapInt.cmi others/belt_internalMapInt.cmj others/belt_internalMapString.cmi others/belt_internalMapString.cmj others/belt_internalSetBuckets.cmi others/belt_internalSetBuckets.cmj others/belt_internalSetInt.cmi others/belt_internalSetInt.cmj others/belt_internalSetString.cmi others/belt_internalSetString.cmj others/dom.cmi others/dom_storage.cmi others/dom_storage2.cmi others/node_buffer.cmi others/node_buffer.cmj others/node_child_process.cmi others/node_child_process.cmj others/node_fs.cmi others/node_fs.cmj others/node_module.cmi others/node_module.cmj others/node_path.cmi others/node_path.cmj others/node_process.cmi others/node_process.cmj
147+
build others : phony others/belt_Array.cmi others/belt_Array.cmj others/belt_Debug.cmi others/belt_Debug.cmj others/belt_Float.cmi others/belt_Float.cmj others/belt_HashMap.cmi others/belt_HashMap.cmj others/belt_HashMapInt.cmi others/belt_HashMapInt.cmj others/belt_HashMapString.cmi others/belt_HashMapString.cmj others/belt_HashSet.cmi others/belt_HashSet.cmj others/belt_HashSetInt.cmi others/belt_HashSetInt.cmj others/belt_HashSetString.cmi others/belt_HashSetString.cmj others/belt_Id.cmi others/belt_Id.cmj others/belt_Int.cmi others/belt_Int.cmj others/belt_List.cmi others/belt_List.cmj others/belt_Map.cmi others/belt_Map.cmj others/belt_MapDict.cmi others/belt_MapDict.cmj others/belt_MapInt.cmi others/belt_MapInt.cmj others/belt_MapString.cmi others/belt_MapString.cmj others/belt_MutableMap.cmi others/belt_MutableMap.cmj others/belt_MutableMapInt.cmi others/belt_MutableMapInt.cmj others/belt_MutableMapString.cmi others/belt_MutableMapString.cmj others/belt_MutableQueue.cmi others/belt_MutableQueue.cmj others/belt_MutableSet.cmi others/belt_MutableSet.cmj others/belt_MutableSetInt.cmi others/belt_MutableSetInt.cmj others/belt_MutableSetString.cmi others/belt_MutableSetString.cmj others/belt_MutableStack.cmi others/belt_MutableStack.cmj others/belt_Option.cmi others/belt_Option.cmj others/belt_Range.cmi others/belt_Range.cmj others/belt_Result.cmi others/belt_Result.cmj others/belt_Set.cmi others/belt_Set.cmj others/belt_SetDict.cmi others/belt_SetDict.cmj others/belt_SetInt.cmi others/belt_SetInt.cmj others/belt_SetString.cmi others/belt_SetString.cmj others/belt_SortArray.cmi others/belt_SortArray.cmj others/belt_SortArrayInt.cmi others/belt_SortArrayInt.cmj others/belt_SortArrayString.cmi others/belt_SortArrayString.cmj others/belt_String.cmi others/belt_String.cmj others/belt_internalAVLset.cmi others/belt_internalAVLset.cmj others/belt_internalAVLtree.cmi others/belt_internalAVLtree.cmj others/belt_internalBuckets.cmi others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmi others/belt_internalBucketsType.cmj others/belt_internalMapInt.cmi others/belt_internalMapInt.cmj others/belt_internalMapString.cmi others/belt_internalMapString.cmj others/belt_internalSetBuckets.cmi others/belt_internalSetBuckets.cmj others/belt_internalSetInt.cmi others/belt_internalSetInt.cmj others/belt_internalSetString.cmi others/belt_internalSetString.cmj others/dom.cmi others/dom_storage.cmi others/dom_storage2.cmi others/node_buffer.cmi others/node_buffer.cmj others/node_child_process.cmi others/node_child_process.cmj others/node_fs.cmi others/node_fs.cmj others/node_module.cmi others/node_module.cmj others/node_path.cmi others/node_path.cmj others/node_process.cmi others/node_process.cmj

jscomp/test/bs_string_test.js

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
5+
var Belt_Array = require("../../lib/js/belt_Array.js");
6+
var Belt_Option = require("../../lib/js/belt_Option.js");
7+
var Belt_String = require("../../lib/js/belt_String.js");
8+
var Caml_option = require("../../lib/js/caml_option.js");
59

610
var suites = /* record */[/* contents : [] */0];
711

@@ -28,9 +32,118 @@ eq("File \"bs_string_test.ml\", line 11, characters 5-12", "ghso ghso g".split("
2832
return x + ("-" + y);
2933
}), ""), "-ghso-ghso-g");
3034

31-
Mt.from_pair_suites("Bs_string_test", suites[0]);
35+
eq("File \"bs_string_test.ml\", line 18, characters 5-12", "foo".length, 3);
36+
37+
eq("File \"bs_string_test.ml\", line 19, characters 5-12", "".length, 0);
38+
39+
eq("File \"bs_string_test.ml\", line 22, characters 5-12", "foo"[1], "o");
40+
41+
eq("File \"bs_string_test.ml\", line 23, characters 5-12", "foo"[5], undefined);
42+
43+
eq("File \"bs_string_test.ml\", line 26, characters 5-12", "foo".concat("bar"), "foobar");
44+
45+
eq("File \"bs_string_test.ml\", line 29, characters 5-12", "foo".concat("bar", "baz"), "foobarbaz");
46+
47+
eq("File \"bs_string_test.ml\", line 30, characters 5-12", "".concat(), "");
48+
49+
eq("File \"bs_string_test.ml\", line 33, characters 5-12", "foo".endsWith("oo"), true);
50+
51+
eq("File \"bs_string_test.ml\", line 34, characters 5-12", "foo".endsWith("a"), false);
52+
53+
eq("File \"bs_string_test.ml\", line 37, characters 5-12", Belt_String.indexOf("foo", "oo"), 1);
54+
55+
eq("File \"bs_string_test.ml\", line 38, characters 5-12", Belt_String.indexOf("foo", "a"), undefined);
56+
57+
eq("File \"bs_string_test.ml\", line 41, characters 5-12", "foo".includes("oo"), true);
58+
59+
eq("File \"bs_string_test.ml\", line 42, characters 5-12", "foo".includes("a"), false);
60+
61+
eq("File \"bs_string_test.ml\", line 45, characters 5-12", "a".repeat(3), "aaa");
62+
63+
eq("File \"bs_string_test.ml\", line 46, characters 5-12", "a".repeat(0), "");
64+
65+
eq("File \"bs_string_test.ml\", line 49, characters 5-12", "hello world".replace("world", "you"), "hello you");
66+
67+
eq("File \"bs_string_test.ml\", line 50, characters 5-12", "hello world".replace("foo", "you"), "hello world");
68+
69+
eq("File \"bs_string_test.ml\", line 53, characters 5-12", "hello world".replace((/world/), "you"), "hello you");
70+
71+
eq("File \"bs_string_test.ml\", line 54, characters 5-12", "hello world world".replace((/world/g), "you"), "hello you you");
72+
73+
eq("File \"bs_string_test.ml\", line 55, characters 5-12", "hello world".replace((/foo/g), "you"), "hello world");
74+
75+
eq("File \"bs_string_test.ml\", line 59, characters 5-12", Caml_option.null_to_opt("hello world".match((/world/))) !== undefined, true);
76+
77+
eq("File \"bs_string_test.ml\", line 60, characters 5-12", Belt_Option.map(Caml_option.null_to_opt("hello world".match((/world/))), (function (x) {
78+
return x.length;
79+
})), 1);
80+
81+
eq("File \"bs_string_test.ml\", line 61, characters 5-12", Belt_Option.flatMap(Caml_option.null_to_opt("hello world".match((/world/))), (function (x) {
82+
return Belt_Array.get(x, 0);
83+
})), "world");
84+
85+
eq("File \"bs_string_test.ml\", line 62, characters 5-12", Caml_option.null_to_opt("hello world".match((/notfound/))), undefined);
86+
87+
eq("File \"bs_string_test.ml\", line 65, characters 5-12", "hello world foo".split(" "), /* array */[
88+
"hello",
89+
"world",
90+
"foo"
91+
]);
92+
93+
eq("File \"bs_string_test.ml\", line 68, characters 5-12", "hello world foo".split(" ", 1), /* array */["hello"]);
94+
95+
eq("File \"bs_string_test.ml\", line 71, characters 5-12", "hello world".startsWith("hello"), true);
96+
97+
eq("File \"bs_string_test.ml\", line 72, characters 5-12", "hello world".startsWith("world"), false);
98+
99+
eq("File \"bs_string_test.ml\", line 75, characters 5-12", "hello world".substr(1, 3), "ell");
100+
101+
eq("File \"bs_string_test.ml\", line 78, characters 5-12", "hello world".substr(1), "ello world");
102+
103+
eq("File \"bs_string_test.ml\", line 79, characters 5-12", "hello world".substr(11), "");
104+
105+
eq("File \"bs_string_test.ml\", line 82, characters 5-12", "hello world".slice(1, 3), "el");
106+
107+
eq("File \"bs_string_test.ml\", line 83, characters 5-12", "hello world".slice(11, 12), "");
108+
109+
eq("File \"bs_string_test.ml\", line 86, characters 5-12", "hello world".slice(1), "ello world");
110+
111+
eq("File \"bs_string_test.ml\", line 87, characters 5-12", "hello world".slice(11), "");
112+
113+
eq("File \"bs_string_test.ml\", line 90, characters 5-12", " hello world ".trim(), "hello world");
114+
115+
eq("File \"bs_string_test.ml\", line 91, characters 5-12", "\n\r\t hello world\n\r\t ".trim(), "hello world");
116+
117+
eq("File \"bs_string_test.ml\", line 94, characters 5-12", " hello world ".trimStart(), "hello world ");
118+
119+
eq("File \"bs_string_test.ml\", line 95, characters 5-12", "\n\r\t hello world\n\r\t ".trimStart(), "hello world\n\r\t ");
120+
121+
eq("File \"bs_string_test.ml\", line 98, characters 5-12", " hello world ".trimEnd(), " hello world");
122+
123+
eq("File \"bs_string_test.ml\", line 99, characters 5-12", "\n\r\t hello world\n\r\t ".trimEnd(), "\n\r\t hello world");
124+
125+
eq("File \"bs_string_test.ml\", line 102, characters 5-12", "4".padStart(4, "x"), "xxx4");
126+
127+
eq("File \"bs_string_test.ml\", line 103, characters 5-12", "4444".padStart(4, "x"), "4444");
128+
129+
eq("File \"bs_string_test.ml\", line 104, characters 5-12", "4".padStart(4, "xy"), "xyx4");
130+
131+
eq("File \"bs_string_test.ml\", line 107, characters 5-12", "4".padEnd(4, "x"), "4xxx");
132+
133+
eq("File \"bs_string_test.ml\", line 108, characters 5-12", "4444".padEnd(4, "x"), "4444");
134+
135+
eq("File \"bs_string_test.ml\", line 109, characters 5-12", "4".padEnd(4, "xy"), "4xyx");
136+
137+
eq("File \"bs_string_test.ml\", line 112, characters 5-12", "HeLLo WorLd".toLowerCase(), "hello world");
138+
139+
eq("File \"bs_string_test.ml\", line 115, characters 5-12", "HeLLo WorLd".toUpperCase(), "HELLO WORLD");
140+
141+
Mt.from_pair_suites("bs_string_test.ml", suites[0]);
142+
143+
var S = 0;
32144

33145
exports.suites = suites;
34146
exports.test_id = test_id;
35147
exports.eq = eq;
148+
exports.S = S;
36149
/* Not a pure module */

0 commit comments

Comments
 (0)