From c1063d6081479c211ed26bba0b19e8d063cf3a5e Mon Sep 17 00:00:00 2001 From: glennsl Date: Tue, 13 Feb 2018 11:14:17 +0100 Subject: [PATCH] undo removal of Js.Null.to_opt --- jscomp/runtime/js_null.ml | 1 + jscomp/runtime/js_null.mli | 5 +- jscomp/test/js_null.js | 43 ++++++++++++++ jscomp/test/js_null_test.js | 112 +++++++++++++++++++++--------------- jscomp/test/js_null_test.ml | 10 ++-- 5 files changed, 121 insertions(+), 50 deletions(-) create mode 100644 jscomp/test/js_null.js diff --git a/jscomp/runtime/js_null.ml b/jscomp/runtime/js_null.ml index 2c0045fe0c..ed3b06f528 100644 --- a/jscomp/runtime/js_null.ml +++ b/jscomp/runtime/js_null.ml @@ -27,6 +27,7 @@ type + 'a t = 'a Js.null +external to_opt : 'a t -> 'a option = "#null_to_opt" external toOption : 'a t -> 'a option = "#null_to_opt" external return : 'a -> 'a t = "%identity" external test : 'a t -> bool = "#is_nil" diff --git a/jscomp/runtime/js_null.mli b/jscomp/runtime/js_null.mli index 1455a435b3..4582e51078 100644 --- a/jscomp/runtime/js_null.mli +++ b/jscomp/runtime/js_null.mli @@ -78,7 +78,7 @@ val iter : 'a t -> ('a -> unit [@bs]) -> unit val fromOption: 'a option -> 'a t val from_opt : 'a option -> 'a t -[@@ocaml.deprecated "Use fromOpiton instead"] +[@@ocaml.deprecated "Use fromOption instead"] (** Maps ['a Js.null] to ['a option] @@ -91,4 +91,7 @@ val from_opt : 'a option -> 'a t *) external toOption : 'a t -> 'a option = "#null_to_opt" +external to_opt : 'a t -> 'a option = "#null_to_opt" +[@@ocaml.deprecated "Use toOption instead"] + diff --git a/jscomp/test/js_null.js b/jscomp/test/js_null.js new file mode 100644 index 0000000000..be771ec738 --- /dev/null +++ b/jscomp/test/js_null.js @@ -0,0 +1,43 @@ +'use strict'; + + +function getExn(f) { + if (f !== null) { + return f; + } else { + throw new Error("Js.Null.getExn"); + } +} + +function bind(x, f) { + if (x !== null) { + return f(x); + } else { + return null; + } +} + +function iter(x, f) { + if (x !== null) { + return f(x); + } else { + return /* () */0; + } +} + +function fromOption(x) { + if (x) { + return x[0]; + } else { + return null; + } +} + +var from_opt = fromOption; + +exports.getExn = getExn; +exports.bind = bind; +exports.iter = iter; +exports.fromOption = fromOption; +exports.from_opt = from_opt; +/* No side effect */ diff --git a/jscomp/test/js_null_test.js b/jscomp/test/js_null_test.js index 0c89328977..0d54006206 100644 --- a/jscomp/test/js_null_test.js +++ b/jscomp/test/js_null_test.js @@ -27,109 +27,131 @@ var suites_001 = /* :: */[ ], /* :: */[ /* tuple */[ - "return", + "toOption - empty", (function () { return /* Eq */Block.__(0, [ - /* Some */["something"], - Js_primitive.null_to_opt("something") + /* None */0, + null === null ? /* None */0 : [null] ]); }) ], /* :: */[ /* tuple */[ - "test - empty", + "toOption - 'a", (function () { return /* Eq */Block.__(0, [ - /* true */1, - +(null === null) + /* Some */[/* () */0], + Js_primitive.null_to_opt(/* () */0) ]); }) ], /* :: */[ /* tuple */[ - "test - 'a", + "return", (function () { return /* Eq */Block.__(0, [ - /* false */0, - /* false */0 + /* Some */["something"], + Js_primitive.null_to_opt("something") ]); }) ], /* :: */[ /* tuple */[ - "bind - empty", + "test - empty", (function () { - return /* StrictEq */Block.__(2, [ - null, - Js_null.bind(null, (function (v) { - return v; - })) + return /* Eq */Block.__(0, [ + /* true */1, + +(null === null) ]); }) ], /* :: */[ /* tuple */[ - "bind - 'a", + "test - 'a", (function () { - return /* StrictEq */Block.__(2, [ - 4, - Js_null.bind(2, (function (n) { - return (n << 1); - })) + return /* Eq */Block.__(0, [ + /* false */0, + /* false */0 ]); }) ], /* :: */[ /* tuple */[ - "iter - empty", + "bind - empty", (function () { - var hit = [/* false */0]; - Js_null.iter(null, (function () { - hit[0] = /* true */1; - return /* () */0; - })); - return /* Eq */Block.__(0, [ - /* false */0, - hit[0] + return /* StrictEq */Block.__(2, [ + null, + Js_null.bind(null, (function (v) { + return v; + })) ]); }) ], /* :: */[ /* tuple */[ - "iter - 'a", + "bind - 'a", (function () { - var hit = [0]; - Js_null.iter(2, (function (v) { - hit[0] = v; - return /* () */0; - })); - return /* Eq */Block.__(0, [ - 2, - hit[0] + return /* StrictEq */Block.__(2, [ + 4, + Js_null.bind(2, (function (n) { + return (n << 1); + })) ]); }) ], /* :: */[ /* tuple */[ - "from_opt - None", + "iter - empty", (function () { + var hit = [/* false */0]; + Js_null.iter(null, (function () { + hit[0] = /* true */1; + return /* () */0; + })); return /* Eq */Block.__(0, [ - null, - Js_null.fromOption(/* None */0) + /* false */0, + hit[0] ]); }) ], /* :: */[ /* tuple */[ - "from_opt - Some", + "iter - 'a", (function () { + var hit = [0]; + Js_null.iter(2, (function (v) { + hit[0] = v; + return /* () */0; + })); return /* Eq */Block.__(0, [ 2, - Js_null.fromOption(/* Some */[2]) + hit[0] ]); }) ], - /* [] */0 + /* :: */[ + /* tuple */[ + "fromOption - None", + (function () { + return /* Eq */Block.__(0, [ + null, + Js_null.fromOption(/* None */0) + ]); + }) + ], + /* :: */[ + /* tuple */[ + "fromOption - Some", + (function () { + return /* Eq */Block.__(0, [ + 2, + Js_null.fromOption(/* Some */[2]) + ]); + }) + ], + /* [] */0 + ] + ] ] ] ] diff --git a/jscomp/test/js_null_test.ml b/jscomp/test/js_null_test.ml index cd21377306..eea07ea3f3 100644 --- a/jscomp/test/js_null_test.ml +++ b/jscomp/test/js_null_test.ml @@ -1,8 +1,10 @@ open Js_null let suites = Mt.[ - "to_opt - empty", (fun _ -> Eq(None, empty |> toOption)); - "to_opt - 'a", (fun _ -> Eq(Some (), return () |> toOption)); + "to_opt - empty", (fun _ -> Eq(None, empty |> to_opt)); + "to_opt - 'a", (fun _ -> Eq(Some (), return () |> to_opt)); + "toOption - empty", (fun _ -> Eq(None, empty |> toOption)); + "toOption - 'a", (fun _ -> Eq(Some (), return () |> toOption)); "return", (fun _ -> Eq(Some "something", return "something" |> toOption)); "test - empty", (fun _ -> Eq(true, empty |> test)); "test - 'a", (fun _ -> Eq(false, return () |> test)); @@ -18,7 +20,7 @@ let suites = Mt.[ let _ = iter (return 2) ((fun v -> hit := v) [@bs]) in Eq(2, !hit) ); - "from_opt - None", (fun _ -> Eq(empty, None |> fromOption)); - "from_opt - Some", (fun _ -> Eq(return 2, Some 2 |> fromOption)); + "fromOption - None", (fun _ -> Eq(empty, None |> fromOption)); + "fromOption - Some", (fun _ -> Eq(return 2, Some 2 |> fromOption)); ] ;; Mt.from_pair_suites __FILE__ suites