Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ function findMap(arr, f) {
};
}

function fromOption(opt) {
if (opt !== undefined) {
return Array.of(Caml_option.valFromOption(opt));
} else {
return Array.of();
}
}

export {
make ,
fromInitializer ,
Expand All @@ -155,5 +163,6 @@ export {
shuffle ,
shuffleInPlace ,
findMap ,
fromOption ,
}
/* No side effect */
9 changes: 9 additions & 0 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,12 @@ let findMap = (arr, f) => {
}

@send external at: (array<'a>, int) => option<'a> = "at"

@val external of0: unit => array<'a> = "Array.of"
@val external of1: 'a => array<'a> = "Array.of"

let fromOption = opt =>
switch opt {
| None => of0()
| Some(a) => of1(a)
}
13 changes: 13 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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>
38 changes: 38 additions & 0 deletions test/ArrayTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
11 changes: 11 additions & 0 deletions test/ArrayTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
3 changes: 3 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var Concurrently = PromiseTest.Concurrently;

var panicTest = ErrorTests.panicTest;

var fromOptionTest = ArrayTests.fromOptionTest;

var eq = IntTests.eq;

var $$catch = IntTests.$$catch;
Expand All @@ -41,6 +43,7 @@ export {
Catching ,
Concurrently ,
panicTest ,
fromOptionTest ,
eq ,
$$catch ,
}
Expand Down