Skip to content

Commit

Permalink
added Pervasive string conversion compat
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Sep 25, 2024
1 parent 81fef79 commit 75562a0
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
41 changes: 41 additions & 0 deletions jscomp/runtime/pervasives.res
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,47 @@ module Pervasives = {
external incr: ref<int> => unit = "%incr"
external decr: ref<int> => unit = "%decr"

/* String conversion functions */

@deprecated("Use Core instead. This will be removed in v13")
let string_of_bool = b =>
if b {
"true"
} else {
"false"
}

@deprecated("Use Core instead. This will be removed in v13")
let bool_of_string = param =>
switch param {
| "true" => true
| "false" => false
| _ => invalid_arg("bool_of_string")
}

@deprecated("Use Core instead. This will be removed in v13")
let bool_of_string_opt = param =>
switch param {
| "true" => Some(true)
| "false" => Some(false)
| _ => None
}

@deprecated("Use Core instead. This will be removed in v13")
external string_of_int: int => string = "String"

@deprecated("Use Core instead. This will be removed in v13") @scope("Number")
external int_of_string: string => int = "parseInt"

let int_of_string_opt = s =>
switch int_of_string(s) {
| n if n == %raw("NaN") => None
| n => Some(n)
}

@deprecated("Use Core instead. This will be removed in v13")
external string_get: (string, int) => char = "%string_safe_get"

/* List operations -- more in module List */

@deprecated("Use Core instead. This will be removed in v13")
Expand Down
12 changes: 12 additions & 0 deletions jscomp/test/test_pervasive.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions jscomp/test/test_pervasives3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions lib/es6/pervasives.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,49 @@ function char_of_int(n) {
return n;
}

function string_of_bool(b) {
if (b) {
return "true";
} else {
return "false";
}
}

function bool_of_string(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
throw {
RE_EXN_ID: "Invalid_argument",
_1: "bool_of_string",
Error: new Error()
};
}
}

function bool_of_string_opt(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
return;
}
}

function int_of_string_opt(s) {
let n = Number.parseInt(s);
if (n === NaN) {
return;
} else {
return n;
}
}

function $at(l1, l2) {
if (l1) {
return {
Expand All @@ -87,6 +130,10 @@ let Pervasives = {
epsilon_float: 2.22044604925031308e-16,
classify_float: classify_float,
char_of_int: char_of_int,
string_of_bool: string_of_bool,
bool_of_string: bool_of_string,
bool_of_string_opt: bool_of_string_opt,
int_of_string_opt: int_of_string_opt,
$at: $at
};

Expand Down Expand Up @@ -118,6 +165,10 @@ export {
epsilon_float,
classify_float,
char_of_int,
string_of_bool,
bool_of_string,
bool_of_string_opt,
int_of_string_opt,
$at,
}
/* No side effect */
51 changes: 51 additions & 0 deletions lib/js/pervasives.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,49 @@ function char_of_int(n) {
return n;
}

function string_of_bool(b) {
if (b) {
return "true";
} else {
return "false";
}
}

function bool_of_string(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
throw {
RE_EXN_ID: "Invalid_argument",
_1: "bool_of_string",
Error: new Error()
};
}
}

function bool_of_string_opt(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
return;
}
}

function int_of_string_opt(s) {
let n = Number.parseInt(s);
if (n === NaN) {
return;
} else {
return n;
}
}

function $at(l1, l2) {
if (l1) {
return {
Expand All @@ -87,6 +130,10 @@ let Pervasives = {
epsilon_float: 2.22044604925031308e-16,
classify_float: classify_float,
char_of_int: char_of_int,
string_of_bool: string_of_bool,
bool_of_string: bool_of_string,
bool_of_string_opt: bool_of_string_opt,
int_of_string_opt: int_of_string_opt,
$at: $at
};

Expand Down Expand Up @@ -117,5 +164,9 @@ exports.min_float = min_float;
exports.epsilon_float = epsilon_float;
exports.classify_float = classify_float;
exports.char_of_int = char_of_int;
exports.string_of_bool = string_of_bool;
exports.bool_of_string = bool_of_string;
exports.bool_of_string_opt = bool_of_string_opt;
exports.int_of_string_opt = int_of_string_opt;
exports.$at = $at;
/* No side effect */

0 comments on commit 75562a0

Please sign in to comment.