Skip to content

Commit 75562a0

Browse files
committed
added Pervasive string conversion compat
1 parent 81fef79 commit 75562a0

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

jscomp/runtime/pervasives.res

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,47 @@ module Pervasives = {
260260
external incr: ref<int> => unit = "%incr"
261261
external decr: ref<int> => unit = "%decr"
262262

263+
/* String conversion functions */
264+
265+
@deprecated("Use Core instead. This will be removed in v13")
266+
let string_of_bool = b =>
267+
if b {
268+
"true"
269+
} else {
270+
"false"
271+
}
272+
273+
@deprecated("Use Core instead. This will be removed in v13")
274+
let bool_of_string = param =>
275+
switch param {
276+
| "true" => true
277+
| "false" => false
278+
| _ => invalid_arg("bool_of_string")
279+
}
280+
281+
@deprecated("Use Core instead. This will be removed in v13")
282+
let bool_of_string_opt = param =>
283+
switch param {
284+
| "true" => Some(true)
285+
| "false" => Some(false)
286+
| _ => None
287+
}
288+
289+
@deprecated("Use Core instead. This will be removed in v13")
290+
external string_of_int: int => string = "String"
291+
292+
@deprecated("Use Core instead. This will be removed in v13") @scope("Number")
293+
external int_of_string: string => int = "parseInt"
294+
295+
let int_of_string_opt = s =>
296+
switch int_of_string(s) {
297+
| n if n == %raw("NaN") => None
298+
| n => Some(n)
299+
}
300+
301+
@deprecated("Use Core instead. This will be removed in v13")
302+
external string_get: (string, int) => char = "%string_safe_get"
303+
263304
/* List operations -- more in module List */
264305

265306
@deprecated("Use Core instead. This will be removed in v13")

jscomp/test/test_pervasive.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/test_pervasives3.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/es6/pervasives.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,49 @@ function char_of_int(n) {
6161
return n;
6262
}
6363

64+
function string_of_bool(b) {
65+
if (b) {
66+
return "true";
67+
} else {
68+
return "false";
69+
}
70+
}
71+
72+
function bool_of_string(param) {
73+
switch (param) {
74+
case "false" :
75+
return false;
76+
case "true" :
77+
return true;
78+
default:
79+
throw {
80+
RE_EXN_ID: "Invalid_argument",
81+
_1: "bool_of_string",
82+
Error: new Error()
83+
};
84+
}
85+
}
86+
87+
function bool_of_string_opt(param) {
88+
switch (param) {
89+
case "false" :
90+
return false;
91+
case "true" :
92+
return true;
93+
default:
94+
return;
95+
}
96+
}
97+
98+
function int_of_string_opt(s) {
99+
let n = Number.parseInt(s);
100+
if (n === NaN) {
101+
return;
102+
} else {
103+
return n;
104+
}
105+
}
106+
64107
function $at(l1, l2) {
65108
if (l1) {
66109
return {
@@ -87,6 +130,10 @@ let Pervasives = {
87130
epsilon_float: 2.22044604925031308e-16,
88131
classify_float: classify_float,
89132
char_of_int: char_of_int,
133+
string_of_bool: string_of_bool,
134+
bool_of_string: bool_of_string,
135+
bool_of_string_opt: bool_of_string_opt,
136+
int_of_string_opt: int_of_string_opt,
90137
$at: $at
91138
};
92139

@@ -118,6 +165,10 @@ export {
118165
epsilon_float,
119166
classify_float,
120167
char_of_int,
168+
string_of_bool,
169+
bool_of_string,
170+
bool_of_string_opt,
171+
int_of_string_opt,
121172
$at,
122173
}
123174
/* No side effect */

lib/js/pervasives.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,49 @@ function char_of_int(n) {
6161
return n;
6262
}
6363

64+
function string_of_bool(b) {
65+
if (b) {
66+
return "true";
67+
} else {
68+
return "false";
69+
}
70+
}
71+
72+
function bool_of_string(param) {
73+
switch (param) {
74+
case "false" :
75+
return false;
76+
case "true" :
77+
return true;
78+
default:
79+
throw {
80+
RE_EXN_ID: "Invalid_argument",
81+
_1: "bool_of_string",
82+
Error: new Error()
83+
};
84+
}
85+
}
86+
87+
function bool_of_string_opt(param) {
88+
switch (param) {
89+
case "false" :
90+
return false;
91+
case "true" :
92+
return true;
93+
default:
94+
return;
95+
}
96+
}
97+
98+
function int_of_string_opt(s) {
99+
let n = Number.parseInt(s);
100+
if (n === NaN) {
101+
return;
102+
} else {
103+
return n;
104+
}
105+
}
106+
64107
function $at(l1, l2) {
65108
if (l1) {
66109
return {
@@ -87,6 +130,10 @@ let Pervasives = {
87130
epsilon_float: 2.22044604925031308e-16,
88131
classify_float: classify_float,
89132
char_of_int: char_of_int,
133+
string_of_bool: string_of_bool,
134+
bool_of_string: bool_of_string,
135+
bool_of_string_opt: bool_of_string_opt,
136+
int_of_string_opt: int_of_string_opt,
90137
$at: $at
91138
};
92139

@@ -117,5 +164,9 @@ exports.min_float = min_float;
117164
exports.epsilon_float = epsilon_float;
118165
exports.classify_float = classify_float;
119166
exports.char_of_int = char_of_int;
167+
exports.string_of_bool = string_of_bool;
168+
exports.bool_of_string = bool_of_string;
169+
exports.bool_of_string_opt = bool_of_string_opt;
170+
exports.int_of_string_opt = int_of_string_opt;
120171
exports.$at = $at;
121172
/* No side effect */

0 commit comments

Comments
 (0)