Skip to content

Commit 07d0529

Browse files
committed
Array.fromSingleton
1 parent ad03448 commit 07d0529

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

src/Core__Array.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,5 @@ let findMap = (arr, f) => {
245245
}
246246

247247
@send external at: (array<'a>, int) => option<'a> = "at"
248+
249+
@val external fromSingleton: 'a => array<'a> = "Array.of"

src/Core__Array.resi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,3 +969,23 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b>
969969
*/
970970
@send
971971
external at: (array<'a>, int) => option<'a> = "at"
972+
973+
/**
974+
`fromSingleton` generates a new array from a single value.
975+
976+
## Examples
977+
978+
```rescript
979+
3->Array.fromSingleton // [3]
980+
undefined->Array.fromSingleton // [undefined]
981+
Some("abc")->Array.fromSingleton // [Some("abc")]
982+
{"x": 3, "y": 5}->Array.fromSingleton // Some({"x": 3, "y": 5})
983+
```
984+
985+
## Specifications
986+
987+
- [Array.of MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
988+
- [Array.of ECMAScript Language Specification](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.of)
989+
*/
990+
@val
991+
external fromSingleton: 'a => array<'a> = "Array.of"

test/ArrayTests.mjs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,66 @@ Test.run([
401401

402402
})), eq, undefined);
403403

404+
function singletonTest(a, title) {
405+
Test.run([
406+
[
407+
"ArrayTests.res",
408+
103,
409+
22,
410+
48
411+
],
412+
"fromSingleton : " + title + ""
413+
], Array.of(a), Caml_obj.equal, [a]);
414+
Test.run([
415+
[
416+
"ArrayTests.res",
417+
104,
418+
22,
419+
48
420+
],
421+
"fromSingleton : " + title + ""
422+
], Array.of(a).length, eq, 1);
423+
Test.run([
424+
[
425+
"ArrayTests.res",
426+
106,
427+
15,
428+
41
429+
],
430+
"fromSingleton : " + title + ""
431+
], Array.of(a)[0], (function (i, j) {
432+
return i === j;
433+
}), a);
434+
}
435+
436+
var m = Array.of({
437+
x: 3,
438+
y: 5
439+
});
440+
441+
singletonTest(3, "Single integer");
442+
443+
singletonTest("abc", "Single string");
444+
445+
singletonTest(undefined, "undefined");
446+
447+
singletonTest(null, "null");
448+
449+
singletonTest([
450+
1,
451+
2,
452+
3
453+
], "full array of integers");
454+
455+
singletonTest([], "empty array");
456+
457+
singletonTest(5, "Some");
458+
459+
singletonTest(undefined, "None");
460+
404461
export {
405462
eq ,
463+
singletonTest ,
464+
m ,
406465
}
407466
/* Not a pure module */

test/ArrayTests.res

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,23 @@ Test.run(
9898
eq,
9999
None,
100100
)
101+
102+
let singletonTest = (a, title) => {
103+
Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a), (i, j) => i == j, [a])
104+
Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a)->Array.length, eq, 1)
105+
Test.run(
106+
__POS_OF__(`fromSingleton : ${title}`),
107+
Array.fromSingleton(a)->Array.getUnsafe(0),
108+
(i, j) => i === j,
109+
a,
110+
)
111+
}
112+
let m = {"x": 3, "y": 5}->Array.fromSingleton
113+
3->singletonTest("Single integer")
114+
"abc"->singletonTest("Single string")
115+
undefined->singletonTest("undefined")
116+
null->singletonTest("null")
117+
[1, 2, 3]->singletonTest("full array of integers")
118+
[]->singletonTest("empty array")
119+
Some(5)->singletonTest("Some")
120+
None->singletonTest("None")

test/TestSuite.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ var Concurrently = PromiseTest.Concurrently;
2828

2929
var panicTest = ErrorTests.panicTest;
3030

31+
var singletonTest = ArrayTests.singletonTest;
32+
33+
var m = ArrayTests.m;
34+
3135
var $$catch = IntTests.$$catch;
3236

3337
var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
@@ -61,6 +65,8 @@ export {
6165
Catching ,
6266
Concurrently ,
6367
panicTest ,
68+
singletonTest ,
69+
m ,
6470
$$catch ,
6571
forEachIfOkCallFunction ,
6672
forEachIfErrorDoNotCallFunction ,

0 commit comments

Comments
 (0)