From 07d0529288f27f1b42de5688ebf531d57eede5d0 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Fri, 17 Mar 2023 13:57:40 -0700 Subject: [PATCH 1/7] Array.fromSingleton --- src/Core__Array.res | 2 ++ src/Core__Array.resi | 20 +++++++++++++++ test/ArrayTests.mjs | 59 ++++++++++++++++++++++++++++++++++++++++++++ test/ArrayTests.res | 20 +++++++++++++++ test/TestSuite.mjs | 6 +++++ 5 files changed, 107 insertions(+) diff --git a/src/Core__Array.res b/src/Core__Array.res index 0f8be4e5..20a8c99c 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 fromSingleton: 'a => array<'a> = "Array.of" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index c4fbd938..9df18b54 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -969,3 +969,23 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> */ @send external at: (array<'a>, int) => option<'a> = "at" + +/** +`fromSingleton` generates a new array from a single value. + +## Examples + +```rescript +3->Array.fromSingleton // [3] +undefined->Array.fromSingleton // [undefined] +Some("abc")->Array.fromSingleton // [Some("abc")] +{"x": 3, "y": 5}->Array.fromSingleton // 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 fromSingleton: 'a => array<'a> = "Array.of" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 891b9262..019d8af3 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -401,7 +401,66 @@ Test.run([ })), eq, undefined); +function singletonTest(a, title) { + Test.run([ + [ + "ArrayTests.res", + 103, + 22, + 48 + ], + "fromSingleton : " + title + "" + ], Array.of(a), Caml_obj.equal, [a]); + Test.run([ + [ + "ArrayTests.res", + 104, + 22, + 48 + ], + "fromSingleton : " + title + "" + ], Array.of(a).length, eq, 1); + Test.run([ + [ + "ArrayTests.res", + 106, + 15, + 41 + ], + "fromSingleton : " + title + "" + ], Array.of(a)[0], (function (i, j) { + return i === j; + }), a); +} + +var m = Array.of({ + x: 3, + y: 5 + }); + +singletonTest(3, "Single integer"); + +singletonTest("abc", "Single string"); + +singletonTest(undefined, "undefined"); + +singletonTest(null, "null"); + +singletonTest([ + 1, + 2, + 3 + ], "full array of integers"); + +singletonTest([], "empty array"); + +singletonTest(5, "Some"); + +singletonTest(undefined, "None"); + export { eq , + singletonTest , + m , } /* Not a pure module */ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index d22ff4a3..b8dbb6a4 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -98,3 +98,23 @@ Test.run( eq, None, ) + +let singletonTest = (a, title) => { + Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a), (i, j) => i == j, [a]) + Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a)->Array.length, eq, 1) + Test.run( + __POS_OF__(`fromSingleton : ${title}`), + Array.fromSingleton(a)->Array.getUnsafe(0), + (i, j) => i === j, + a, + ) +} +let m = {"x": 3, "y": 5}->Array.fromSingleton +3->singletonTest("Single integer") +"abc"->singletonTest("Single string") +undefined->singletonTest("undefined") +null->singletonTest("null") +[1, 2, 3]->singletonTest("full array of integers") +[]->singletonTest("empty array") +Some(5)->singletonTest("Some") +None->singletonTest("None") diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index 3d8e7128..efd57647 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -28,6 +28,10 @@ var Concurrently = PromiseTest.Concurrently; var panicTest = ErrorTests.panicTest; +var singletonTest = ArrayTests.singletonTest; + +var m = ArrayTests.m; + var $$catch = IntTests.$$catch; var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction; @@ -61,6 +65,8 @@ export { Catching , Concurrently , panicTest , + singletonTest , + m , $$catch , forEachIfOkCallFunction , forEachIfErrorDoNotCallFunction , From 6840c82b28a09021ce194c6f9770ca5ef59f1ea5 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Fri, 17 Mar 2023 14:02:43 -0700 Subject: [PATCH 2/7] add doc example when pass an array in --- src/Core__Array.resi | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 9df18b54..82ccc140 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -979,6 +979,7 @@ external at: (array<'a>, int) => option<'a> = "at" 3->Array.fromSingleton // [3] undefined->Array.fromSingleton // [undefined] Some("abc")->Array.fromSingleton // [Some("abc")] +[1,2,3]->Array.fromSingleton // [[1,2,3]] {"x": 3, "y": 5}->Array.fromSingleton // Some({"x": 3, "y": 5}) ``` From b549b97f5c9d1b44425ae3aaa93e88e68297b148 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Mon, 20 Mar 2023 11:27:56 -0700 Subject: [PATCH 3/7] rename to fromOneItem --- src/Core__Array.res | 2 +- src/Core__Array.resi | 14 +++++++------- test/ArrayTests.mjs | 12 ++++++------ test/ArrayTests.res | 10 +++++----- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Core__Array.res b/src/Core__Array.res index 20a8c99c..6069af24 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -246,4 +246,4 @@ let findMap = (arr, f) => { @send external at: (array<'a>, int) => option<'a> = "at" -@val external fromSingleton: 'a => array<'a> = "Array.of" +@val external fromOneItem: 'a => array<'a> = "Array.of" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 82ccc140..c57cda8f 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -971,16 +971,16 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> external at: (array<'a>, int) => option<'a> = "at" /** -`fromSingleton` generates a new array from a single value. +`fromOneItem` generates a new array from a single value. ## Examples ```rescript -3->Array.fromSingleton // [3] -undefined->Array.fromSingleton // [undefined] -Some("abc")->Array.fromSingleton // [Some("abc")] -[1,2,3]->Array.fromSingleton // [[1,2,3]] -{"x": 3, "y": 5}->Array.fromSingleton // Some({"x": 3, "y": 5}) +3->Array.fromOneItem // [3] +undefined->Array.fromOneItem // [undefined] +Some("abc")->Array.fromOneItem // [Some("abc")] +[1,2,3]->Array.fromOneItem // [[1,2,3]] +{"x": 3, "y": 5}->Array.fromOneItem // Some({"x": 3, "y": 5}) ``` ## Specifications @@ -989,4 +989,4 @@ Some("abc")->Array.fromSingleton // [Some("abc")] - [Array.of ECMAScript Language Specification](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.of) */ @val -external fromSingleton: 'a => array<'a> = "Array.of" +external fromOneItem: 'a => array<'a> = "Array.of" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 019d8af3..7728ace7 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -407,27 +407,27 @@ function singletonTest(a, title) { "ArrayTests.res", 103, 22, - 48 + 46 ], - "fromSingleton : " + title + "" + "fromOneItem : " + title + "" ], Array.of(a), Caml_obj.equal, [a]); Test.run([ [ "ArrayTests.res", 104, 22, - 48 + 46 ], - "fromSingleton : " + title + "" + "fromOneItem : " + title + "" ], Array.of(a).length, eq, 1); Test.run([ [ "ArrayTests.res", 106, 15, - 41 + 39 ], - "fromSingleton : " + title + "" + "fromOneItem : " + title + "" ], Array.of(a)[0], (function (i, j) { return i === j; }), a); diff --git a/test/ArrayTests.res b/test/ArrayTests.res index b8dbb6a4..b1063b3e 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -100,16 +100,16 @@ Test.run( ) let singletonTest = (a, title) => { - Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a), (i, j) => i == j, [a]) - Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a)->Array.length, eq, 1) + Test.run(__POS_OF__(`fromOneItem : ${title}`), Array.fromOneItem(a), (i, j) => i == j, [a]) + Test.run(__POS_OF__(`fromOneItem : ${title}`), Array.fromOneItem(a)->Array.length, eq, 1) Test.run( - __POS_OF__(`fromSingleton : ${title}`), - Array.fromSingleton(a)->Array.getUnsafe(0), + __POS_OF__(`fromOneItem : ${title}`), + Array.fromOneItem(a)->Array.getUnsafe(0), (i, j) => i === j, a, ) } -let m = {"x": 3, "y": 5}->Array.fromSingleton +let m = {"x": 3, "y": 5}->Array.fromOneItem 3->singletonTest("Single integer") "abc"->singletonTest("Single string") undefined->singletonTest("undefined") From 7cae2f85a590fac1ffcc870c9f2307f53ffabc8f Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 10:49:10 -0700 Subject: [PATCH 4/7] remove Specifications section --- src/Core__Array.resi | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index c57cda8f..edf6bff1 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -971,7 +971,7 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> external at: (array<'a>, int) => option<'a> = "at" /** -`fromOneItem` generates a new array from a single value. +`fromOneItem` 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 @@ -982,11 +982,6 @@ Some("abc")->Array.fromOneItem // [Some("abc")] [1,2,3]->Array.fromOneItem // [[1,2,3]] {"x": 3, "y": 5}->Array.fromOneItem // 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 fromOneItem: 'a => array<'a> = "Array.of" From 7ba68b8a8e65cef0b728e4986a739473e59c94e7 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Fri, 17 Mar 2023 13:57:40 -0700 Subject: [PATCH 5/7] Array.fromSingleton --- src/Core__Array.res | 2 +- src/Core__Array.resi | 18 +++++++++++------- test/ArrayTests.mjs | 12 ++++++------ test/ArrayTests.res | 10 +++++----- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Core__Array.res b/src/Core__Array.res index 6069af24..20a8c99c 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -246,4 +246,4 @@ let findMap = (arr, f) => { @send external at: (array<'a>, int) => option<'a> = "at" -@val external fromOneItem: 'a => array<'a> = "Array.of" +@val external fromSingleton: 'a => array<'a> = "Array.of" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index edf6bff1..9df18b54 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -971,17 +971,21 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> external at: (array<'a>, int) => option<'a> = "at" /** -`fromOneItem` 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) +`fromSingleton` generates a new array from a single value. ## Examples ```rescript -3->Array.fromOneItem // [3] -undefined->Array.fromOneItem // [undefined] -Some("abc")->Array.fromOneItem // [Some("abc")] -[1,2,3]->Array.fromOneItem // [[1,2,3]] -{"x": 3, "y": 5}->Array.fromOneItem // Some({"x": 3, "y": 5}) +3->Array.fromSingleton // [3] +undefined->Array.fromSingleton // [undefined] +Some("abc")->Array.fromSingleton // [Some("abc")] +{"x": 3, "y": 5}->Array.fromSingleton // 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 fromOneItem: 'a => array<'a> = "Array.of" +external fromSingleton: 'a => array<'a> = "Array.of" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 7728ace7..019d8af3 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -407,27 +407,27 @@ function singletonTest(a, title) { "ArrayTests.res", 103, 22, - 46 + 48 ], - "fromOneItem : " + title + "" + "fromSingleton : " + title + "" ], Array.of(a), Caml_obj.equal, [a]); Test.run([ [ "ArrayTests.res", 104, 22, - 46 + 48 ], - "fromOneItem : " + title + "" + "fromSingleton : " + title + "" ], Array.of(a).length, eq, 1); Test.run([ [ "ArrayTests.res", 106, 15, - 39 + 41 ], - "fromOneItem : " + title + "" + "fromSingleton : " + title + "" ], Array.of(a)[0], (function (i, j) { return i === j; }), a); diff --git a/test/ArrayTests.res b/test/ArrayTests.res index b1063b3e..b8dbb6a4 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -100,16 +100,16 @@ Test.run( ) let singletonTest = (a, title) => { - Test.run(__POS_OF__(`fromOneItem : ${title}`), Array.fromOneItem(a), (i, j) => i == j, [a]) - Test.run(__POS_OF__(`fromOneItem : ${title}`), Array.fromOneItem(a)->Array.length, eq, 1) + Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a), (i, j) => i == j, [a]) + Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a)->Array.length, eq, 1) Test.run( - __POS_OF__(`fromOneItem : ${title}`), - Array.fromOneItem(a)->Array.getUnsafe(0), + __POS_OF__(`fromSingleton : ${title}`), + Array.fromSingleton(a)->Array.getUnsafe(0), (i, j) => i === j, a, ) } -let m = {"x": 3, "y": 5}->Array.fromOneItem +let m = {"x": 3, "y": 5}->Array.fromSingleton 3->singletonTest("Single integer") "abc"->singletonTest("Single string") undefined->singletonTest("undefined") From c271c9a8f0fcdac3302c88545043fad6980056aa Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 18 Jul 2023 11:47:02 -0700 Subject: [PATCH 6/7] rename to "of1" similar to Javascript name --- src/Core__Array.res | 2 +- src/Core__Array.resi | 13 +++++++------ test/ArrayTests.mjs | 36 ++++++++++++++++++------------------ test/ArrayTests.res | 31 +++++++++++++------------------ test/TestSuite.mjs | 4 ++-- 5 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/Core__Array.res b/src/Core__Array.res index 20a8c99c..80b8e772 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -246,4 +246,4 @@ let findMap = (arr, f) => { @send external at: (array<'a>, int) => option<'a> = "at" -@val external fromSingleton: 'a => array<'a> = "Array.of" +@val external of1: 'a => array<'a> = "Array.of" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 9df18b54..a486c600 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -971,15 +971,16 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b> external at: (array<'a>, int) => option<'a> = "at" /** -`fromSingleton` generates a new array from a single value. +`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.fromSingleton // [3] -undefined->Array.fromSingleton // [undefined] -Some("abc")->Array.fromSingleton // [Some("abc")] -{"x": 3, "y": 5}->Array.fromSingleton // Some({"x": 3, "y": 5}) +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 @@ -988,4 +989,4 @@ Some("abc")->Array.fromSingleton // [Some("abc")] - [Array.of ECMAScript Language Specification](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.of) */ @val -external fromSingleton: 'a => array<'a> = "Array.of" +external of1: 'a => array<'a> = "Array.of" diff --git a/test/ArrayTests.mjs b/test/ArrayTests.mjs index 019d8af3..943964f3 100644 --- a/test/ArrayTests.mjs +++ b/test/ArrayTests.mjs @@ -401,33 +401,33 @@ Test.run([ })), eq, undefined); -function singletonTest(a, title) { +function of1Test(a, title) { Test.run([ [ "ArrayTests.res", 103, 22, - 48 + 38 ], - "fromSingleton : " + title + "" + "of1 : " + title + "" ], Array.of(a), Caml_obj.equal, [a]); Test.run([ [ "ArrayTests.res", 104, 22, - 48 + 38 ], - "fromSingleton : " + title + "" + "of1 : " + title + "" ], Array.of(a).length, eq, 1); Test.run([ [ "ArrayTests.res", - 106, - 15, - 41 + 105, + 22, + 38 ], - "fromSingleton : " + title + "" + "of1 : " + title + "" ], Array.of(a)[0], (function (i, j) { return i === j; }), a); @@ -438,29 +438,29 @@ var m = Array.of({ y: 5 }); -singletonTest(3, "Single integer"); +of1Test(3, "Single integer"); -singletonTest("abc", "Single string"); +of1Test("abc", "Single string"); -singletonTest(undefined, "undefined"); +of1Test(undefined, "undefined"); -singletonTest(null, "null"); +of1Test(null, "null"); -singletonTest([ +of1Test([ 1, 2, 3 ], "full array of integers"); -singletonTest([], "empty array"); +of1Test([], "empty array"); -singletonTest(5, "Some"); +of1Test(5, "Some"); -singletonTest(undefined, "None"); +of1Test(undefined, "None"); export { eq , - singletonTest , + of1Test , m , } /* Not a pure module */ diff --git a/test/ArrayTests.res b/test/ArrayTests.res index b8dbb6a4..0da9ef74 100644 --- a/test/ArrayTests.res +++ b/test/ArrayTests.res @@ -99,22 +99,17 @@ Test.run( None, ) -let singletonTest = (a, title) => { - Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a), (i, j) => i == j, [a]) - Test.run(__POS_OF__(`fromSingleton : ${title}`), Array.fromSingleton(a)->Array.length, eq, 1) - Test.run( - __POS_OF__(`fromSingleton : ${title}`), - Array.fromSingleton(a)->Array.getUnsafe(0), - (i, j) => i === j, - a, - ) +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.fromSingleton -3->singletonTest("Single integer") -"abc"->singletonTest("Single string") -undefined->singletonTest("undefined") -null->singletonTest("null") -[1, 2, 3]->singletonTest("full array of integers") -[]->singletonTest("empty array") -Some(5)->singletonTest("Some") -None->singletonTest("None") +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 efd57647..eff6411b 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -28,7 +28,7 @@ var Concurrently = PromiseTest.Concurrently; var panicTest = ErrorTests.panicTest; -var singletonTest = ArrayTests.singletonTest; +var of1Test = ArrayTests.of1Test; var m = ArrayTests.m; @@ -65,7 +65,7 @@ export { Catching , Concurrently , panicTest , - singletonTest , + of1Test , m , $$catch , forEachIfOkCallFunction , From 363fb9ef28f4a8da6161c9f45dc9d97ed9415003 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 18 Jul 2023 11:52:26 -0700 Subject: [PATCH 7/7] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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