From cd9d12b0a58d233b16d306e4814e16df0ce6b036 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 27 Oct 2023 18:24:29 +0200 Subject: [PATCH 1/4] Only allow to join actual strings with Array.joinWith --- src/Core__Array.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Array.res b/src/Core__Array.res index 0f8be4e5..f1f53317 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -123,7 +123,7 @@ let indexOfOpt = (arr, item) => } @send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf" -@send external joinWith: (array<'a>, string) => string = "join" +@send external joinWith: (array, string) => string = "join" @send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf" let lastIndexOfOpt = (arr, item) => From 5576dab1f4d0c7a9c8f988888253961836c4cd7e Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 27 Oct 2023 18:25:20 +0200 Subject: [PATCH 2/4] Add Array.joinWithUnsafe for when you want to do weird stuff --- src/Core__Array.res | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Core__Array.res b/src/Core__Array.res index f1f53317..954cb99b 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -125,6 +125,8 @@ let indexOfOpt = (arr, item) => @send external joinWith: (array, string) => string = "join" +@send external joinWithUnsafe: (array<'a>, string) => string = "join" + @send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf" let lastIndexOfOpt = (arr, item) => switch arr->lastIndexOf(item) { From d5b20310fb827ef1f63524a1f0c817c28a2cc9f7 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 27 Oct 2023 18:42:47 +0200 Subject: [PATCH 3/4] Add docs for Arra.joinWith / joinWithUnsafe --- src/Core__Array.resi | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index c4fbd938..998b9178 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -384,17 +384,30 @@ let indexOfOpt: (array<'a>, 'a) => option @send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf" /** -`joinWith(array, separator)` produces a string where all items of `array` are printed, separated by `separator`. Under the hood this will run JavaScript's `toString` on all the array items. +`joinWith(array, separator)` produces a string where all items of `array` are printed, separated by `separator`. Array items must be strings, to join number or other arrays, use `joinWithUnsafe`. Under the hood this will run JavaScript's `toString` on all the array items. + +## Examples +```rescript +let array = ["One", "Two", "Three"] + +Console.log(array->Array.joinWith(" -- ")) // One -- Two -- Three +``` +*/ +@send +external joinWith: (array, string) => string = "join" + +/** +`joinWithUnsafe(array, separator)` produces a string where all items of `array` are printed, separated by `separator`. Under the hood this will run JavaScript's `toString` on all the array items. ## Examples ```rescript let array = [1, 2, 3] -Console.log(array->Array.joinWith(" -- ")) // 1 -- 2 -- 3 +Console.log(array->Array.joinWithUnsafe(" -- ")) // 1 -- 2 -- 3 ``` */ @send -external joinWith: (array<'a>, string) => string = "join" +external joinWithUnsafe: (array<'a>, string) => string = "join" @send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf" let lastIndexOfOpt: (array<'a>, 'a) => option @send external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf" From d444b4b4e7d6a18f0b178f387bbb0296a204e4a0 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 3 Nov 2023 14:23:02 +0100 Subject: [PATCH 4/4] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23dc8533..626ee70c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### API changes - Add `Math.Int.floor` and `Math.Int.random`, https://github.com/rescript-association/rescript-core/pull/156 +- Change `Array.joinWith`s signature to accept only string arrays and add `Array.joinWithUnsafe` with the polymorphic signature of the former https://github.com/rescript-association/rescript-core/pull/157 ### Documentation