diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f580aa..4136dcc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## main +### API changes + +- Add `Result.forEach` https://github.com/rescript-association/rescript-core/pull/116 +- Add `Array.fromOption` https://github.com/rescript-association/rescript-core/pull/111 + ## 0.2.0 ### API changes diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 1560df8f..61203e6d 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -138,6 +138,14 @@ function findMap(arr, f) { }; } +function fromOption(opt) { + if (opt !== undefined) { + return [Caml_option.valFromOption(opt)]; + } else { + return []; + } +} + export { make , fromInitializer , @@ -155,5 +163,6 @@ export { shuffle , shuffleInPlace , findMap , + fromOption , } /* No side effect */ diff --git a/src/Core__Array.res b/src/Core__Array.res index cd1bda48..cc895f1e 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -214,3 +214,9 @@ let findMap = (arr, f) => { } @send external at: (array<'a>, int) => option<'a> = "at" + +let fromOption = opt => + switch opt { + | None => [] + | Some(a) => [a] + } diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 253f0cf2..9e5d0c07 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -955,3 +955,16 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> */ @send external at: (array<'a>, int) => option<'a> = "at" + +/** +`fromOption` creates an empty array if the option is `None`, or creates an array of length 1 if the option is `Some`. + +## Examples + +```rescript +Some(3)->Array.fromOption // [3] +None->Array.fromOption // [] +Some([1,2,3])->Array.fromOption // [[1,2,3]] +``` +*/ +let fromOption: option<'a> => array<'a> diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 7cc4a4c6..4ab826eb 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -3,6 +3,7 @@ import * as Test from "./Test.mjs"; import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; import * as Core__List from "../src/Core__List.mjs"; +import * as Caml_option from "rescript/lib/es6/caml_option.js"; import * as Core__Array from "../src/Core__Array.mjs"; var eq = Caml_obj.equal; @@ -401,7 +402,44 @@ Test.run([ })), eq, undefined); +function fromOptionTest(wrap, title) { + Test.run([ + [ + "ArrayTests.res", + 103, + 22, + 45 + ], + "fromOption : " + title + "" + ], Core__Array.fromOption(Caml_option.some(wrap)), eq, [wrap]); +} + +fromOptionTest(3, "int"); + +fromOptionTest("abc", "string"); + +fromOptionTest(undefined, "undefined"); + +fromOptionTest([ + 1, + 2, + 3 + ], "array"); + +fromOptionTest([], "empty array"); + +Test.run([ + [ + "ArrayTests.res", + 111, + 20, + 42 + ], + "fromOption : if none" + ], Core__Array.fromOption(undefined), eq, []); + export { eq , + fromOptionTest , } /* Not a pure module */ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index 40c352f7..370f4bdf 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -98,3 +98,14 @@ Test.run( eq, None, ) + +let fromOptionTest = (wrap, title) => + Test.run(__POS_OF__(`fromOption : ${title}`), Some(wrap)->Array.fromOption, eq, [wrap]) + +3->fromOptionTest("int") +"abc"->fromOptionTest("string") +undefined->fromOptionTest("undefined") +[1, 2, 3]->fromOptionTest("array") +[]->fromOptionTest("empty array") + +Test.run(__POS_OF__("fromOption : if none"), None->Array.fromOption, eq, []) diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index c968bd8f..925ce4ca 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -26,6 +26,8 @@ var Concurrently = PromiseTest.Concurrently; var panicTest = ErrorTests.panicTest; +var fromOptionTest = ArrayTests.fromOptionTest; + var eq = IntTests.eq; var $$catch = IntTests.$$catch; @@ -41,6 +43,7 @@ export { Catching , Concurrently , panicTest , + fromOptionTest , eq , $$catch , }