Skip to content

Array.joinWith -> Array.join #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next version

- Add `Array.join` and deprecate `Array.joinWith`. https://github.com/rescript-association/rescript-core/pull/205
- BREAKING: Intl types: simplify bindings for constructors / functions with optional arguments. https://github.com/rescript-association/rescript-core/pull/198
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197
- Add `Array.flatMapWithIndex` https://github.com/rescript-association/rescript-core/pull/199
Expand Down
2 changes: 1 addition & 1 deletion scripts/DocTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async function testCode(id, code) {
"-I",
rescriptCoreCompiled,
"-w",
"-109",
"-3-109",
"-uncurried",
"-open",
"RescriptCore"
Expand Down
10 changes: 5 additions & 5 deletions scripts/DocTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let testCode = async (~id, ~code) => {
"-I",
rescriptCoreCompiled,
"-w",
"-109",
"-3-109",
"-uncurried",
"-open",
"RescriptCore",
Expand All @@ -184,7 +184,7 @@ let testCode = async (~id, ~code) => {
| true =>
promise
->Array.map(e => e->Buffer.toString)
->Array.joinWith("")
->Array.join("")
->Error
| false => Ok()
}
Expand Down Expand Up @@ -259,7 +259,7 @@ let getCodeBlocks = example => {
code
->List.reverse
->List.toArray
->Array.joinWith("\n"),
->Array.join("\n"),
...acc,
},
)
Expand Down Expand Up @@ -325,9 +325,9 @@ let main = async () => {
e
->String.split("\n")
->Array.filterWithIndex((_, i) => i !== 2)
->Array.joinWith("\n")
->Array.join("\n")
})
->Array.joinWith("\n")
->Array.join("\n")

let message = `${"error"->red}: failed to compile examples from ${kind} ${test.id->cyan}\n${errorMessage}`

Expand Down
10 changes: 8 additions & 2 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ let indexOfOpt = (arr, item) =>
}
@send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"

@send external joinWith: (array<string>, string) => string = "join"
@send external join: (array<string>, string) => string = "join"

@send external joinWithUnsafe: (array<'a>, string) => string = "join"
@deprecated("Use `join` instead") @send
external joinWith: (array<string>, string) => string = "join"

@send external joinUnsafe: (array<'a>, string) => string = "join"

@deprecated("Use `joinUnsafe` instead") @send
external joinWithUnsafe: (array<'a>, string) => string = "join"

@send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"
let lastIndexOfOpt = (arr, item) =>
Expand Down
32 changes: 32 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ Console.log([{"language": "ReScript"}]->Array.indexOfOpt({"language": "ReScript"
let indexOfOpt: (array<'a>, 'a) => option<int>
@send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"

/**
`join(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 `joinUnsafe`. Under the hood this will run JavaScript's `toString` on all the array items.

See [Array.join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

## Examples
```rescript
let array = ["One", "Two", "Three"]

Console.log(array->Array.join(" -- ")) // One -- Two -- Three
```
*/
@send
external join: (array<string>, string) => string = "join"

/**
`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.

Expand All @@ -407,9 +422,25 @@ let array = ["One", "Two", "Three"]
Console.log(array->Array.joinWith(" -- ")) // One -- Two -- Three
```
*/
@deprecated("Use `join` instead")
@send
external joinWith: (array<string>, string) => string = "join"

/**
`joinUnsafe(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.

See [Array.join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

## Examples
```rescript
let array = [1, 2, 3]

Console.log(array->Array.joinUnsafe(" -- ")) // 1 -- 2 -- 3
```
*/
@send
external joinUnsafe: (array<'a>, 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.

Expand All @@ -420,6 +451,7 @@ let array = [1, 2, 3]
Console.log(array->Array.joinWithUnsafe(" -- ")) // 1 -- 2 -- 3
```
*/
@deprecated("Use `joinUnsafe` instead")
@send
external joinWithUnsafe: (array<'a>, string) => string = "join"
@send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"
Expand Down
Loading