diff --git a/CHANGELOG.md b/CHANGELOG.md index 5df9f6b0..fccfeb48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Next version +### API changes + +- `Array.of1` https://github.com/rescript-association/rescript-core/pull/109 + ## 0.4.0 ### API changes diff --git a/src/Core__Array.res b/src/Core__Array.res index 0f8be4e5..80b8e772 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -245,3 +245,5 @@ let findMap = (arr, f) => { } @send external at: (array<'a>, int) => option<'a> = "at" + +@val external of1: 'a => array<'a> = "Array.of" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index c4fbd938..a486c600 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -969,3 +969,24 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> */ @send external at: (array<'a>, int) => option<'a> = "at" + +/** +`of1` generates a new array from a single value. See [Array.of on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of) + +## Examples + +```rescript +3->Array.of1 // [3] +undefined->Array.of1 // [undefined] +Some("abc")->Array.of1 // [Some("abc")] +[1,2,3]->Array.of1 // [[1,2,3]] +{"x": 3, "y": 5}->Array.of1 // Some({"x": 3, "y": 5}) +``` + +## Specifications + +- [Array.of MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of) +- [Array.of ECMAScript Language Specification](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.of) +*/ +@val +external of1: 'a => array<'a> = "Array.of" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 891b9262..943964f3 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -401,7 +401,66 @@ Test.run([ })), eq, undefined); +function of1Test(a, title) { + Test.run([ + [ + "ArrayTests.res", + 103, + 22, + 38 + ], + "of1 : " + title + "" + ], Array.of(a), Caml_obj.equal, [a]); + Test.run([ + [ + "ArrayTests.res", + 104, + 22, + 38 + ], + "of1 : " + title + "" + ], Array.of(a).length, eq, 1); + Test.run([ + [ + "ArrayTests.res", + 105, + 22, + 38 + ], + "of1 : " + title + "" + ], Array.of(a)[0], (function (i, j) { + return i === j; + }), a); +} + +var m = Array.of({ + x: 3, + y: 5 + }); + +of1Test(3, "Single integer"); + +of1Test("abc", "Single string"); + +of1Test(undefined, "undefined"); + +of1Test(null, "null"); + +of1Test([ + 1, + 2, + 3 + ], "full array of integers"); + +of1Test([], "empty array"); + +of1Test(5, "Some"); + +of1Test(undefined, "None"); + export { eq , + of1Test , + m , } /* Not a pure module */ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index d22ff4a3..0da9ef74 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -98,3 +98,18 @@ Test.run( eq, None, ) + +let of1Test = (a, title) => { + Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a), (i, j) => i == j, [a]) + Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.length, eq, 1) + Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.getUnsafe(0), (i, j) => i === j, a) +} +let m = {"x": 3, "y": 5}->Array.of1 +3->of1Test("Single integer") +"abc"->of1Test("Single string") +undefined->of1Test("undefined") +null->of1Test("null") +[1, 2, 3]->of1Test("full array of integers") +[]->of1Test("empty array") +Some(5)->of1Test("Some") +None->of1Test("None") diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index 3d8e7128..eff6411b 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -28,6 +28,10 @@ var Concurrently = PromiseTest.Concurrently; var panicTest = ErrorTests.panicTest; +var of1Test = ArrayTests.of1Test; + +var m = ArrayTests.m; + var $$catch = IntTests.$$catch; var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction; @@ -61,6 +65,8 @@ export { Catching , Concurrently , panicTest , + of1Test , + m , $$catch , forEachIfOkCallFunction , forEachIfErrorDoNotCallFunction ,