Skip to content

Commit 9411553

Browse files
authored
Merge branch 'rescript-association:main' into date-format-functions-with-options
2 parents a3c5f9e + f77964d commit 9411553

25 files changed

+3343
-525
lines changed

CHANGELOG.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
# @rescript/core Changelog
22

3-
## master
3+
## main
4+
5+
### API changes
6+
7+
- Change `Map.set` to not return self, to indicate that it's mutable. https://github.com/rescript-association/rescript-core/pull/34
8+
- Change `Set.add` to not return self, to indicate that it's mutable. https://github.com/rescript-association/rescript-core/pull/35
9+
- Change `Iterator` bindings to have the same shape as `AsyncIterator` for consistency. https://github.com/rescript-association/rescript-core/pull/34
10+
- Add `Iterator.toArray` binding for turning an iterator into an array. https://github.com/rescript-association/rescript-core/pull/34
11+
- Add `Array.at` binding for returning an array item by its index. https://github.com/rescript-association/rescript-core/pull/48
12+
- Fixed type signatures of `Array.fromArrayLikeWithMap` and `Array.fromIteratorWithMap`. https://github.com/rescript-association/rescript-core/pull/50
13+
- Remove internal async/await helpers that do not need to be exposed in `Core`.
14+
15+
### Documentation
16+
17+
- Docstrings for `Map` and `Iterator`. https://github.com/rescript-association/rescript-core/pull/34
18+
- Docstrings for `Global`. https://github.com/rescript-association/rescript-core/pull/39
19+
- Docstrings for `Set`. https://github.com/rescript-association/rescript-core/pull/35
20+
- Docstrings for `AsyncIterator`. https://github.com/rescript-association/rescript-core/pull/33
21+
- Docstrings for `Type`. https://github.com/rescript-association/rescript-core/pull/32
22+
- Docstrings for `Int`. https://github.com/rescript-association/rescript-core/pull/37
23+
- Docstrings for `Dict`. https://github.com/rescript-association/rescript-core/pull/40

src/Core__Array.res

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
66

77
@val external fromArrayLike: Js.Array2.array_like<'a> => array<'a> = "Array.from"
88
@val
9-
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'a> = "Array.from"
9+
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from"
1010

1111
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
12-
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'c) => array<'a> = "Array.from"
12+
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from"
1313

1414
@val external isArray: 'a => bool = "Array.isArray"
1515

@@ -196,3 +196,5 @@ let filterMap = (a, f) => filterMapU(a, (. a) => f(a))
196196

197197
// TODO: Change this implementation?
198198
let flatMap = (a, f) => []->concatMany(map(a, f))
199+
200+
@send external at: (array<'a>, int) => option<'a> = "at"

src/Core__Array.resi

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
@val external fromWithMap: ('a, 'b => 'c) => array<'c> = "Array.from"
33
@val external fromArrayLike: Js.Array2.array_like<'a> => array<'a> = "Array.from"
44
@val
5-
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'a> = "Array.from"
5+
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from"
66
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
7-
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'c) => array<'a> = "Array.from"
7+
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from"
88
@val external isArray: 'a => bool = "Array.isArray"
99
@get external length: array<'a> => int = "length"
1010
@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
@@ -103,3 +103,21 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b>
103103
let shuffle: array<'a> => array<'a>
104104
let shuffleInPlace: array<'a> => unit
105105
let flatMap: (array<'a>, 'a => array<'b>) => array<'b>
106+
107+
108+
/**
109+
`at(array, index)`
110+
111+
Get an element by its index. Negative indices count backwards from the last item.
112+
113+
## Examples
114+
```rescript
115+
["a", "b", "c"]->Array.at(0) // Some("a")
116+
["a", "b", "c"]->Array.at(2) // Some("c")
117+
["a", "b", "c"]->Array.at(3) // None
118+
["a", "b", "c"]->Array.at(-1) // Some("c")
119+
["a", "b", "c"]->Array.at(-3) // Some("a")
120+
["a", "b", "c"]->Array.at(-4) // None
121+
```
122+
*/
123+
@send external at: (array<'a>, int) => option<'a> = "at"

src/Core__AsyncIterator.resi

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***
2+
Bindings to async iterators, a way to do async iteration in JavaScript.
3+
4+
See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.*/
5+
6+
/**
7+
The type representing an async iterator.
8+
*/
9+
type t<'a>
10+
11+
type value<'a> = {
12+
/**
13+
Whether there are more values to iterate on before the iterator is done.
14+
*/
15+
done: bool,
16+
/**
17+
The value of this iteration, if any.
18+
*/
19+
value: option<'a>,
20+
}
21+
22+
/**
23+
`next(asyncIterator)`
24+
25+
Returns the next value of the iterator, if any.
26+
27+
See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.
28+
29+
## Examples
30+
- A simple example, getting the next value:
31+
```rescript
32+
let {done, value} = await someAsyncIterator->AsyncIterator.next
33+
```
34+
35+
- Complete example, including looping over all values:
36+
```rescript
37+
// Let's pretend we get an async iterator returning ints from somewhere.
38+
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
39+
40+
41+
let processMyAsyncIterator = async () => {
42+
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
43+
let break = ref(false)
44+
45+
while !break.contents {
46+
// Await the next iterator value
47+
let {value, done} = await asyncIterator->AsyncIterator.next
48+
49+
// Exit the while loop if the iterator says it's done
50+
break := done
51+
52+
// This will log the (int) value of the current async iteration, if a value was returned.
53+
Console.log(value)
54+
}
55+
}
56+
```
57+
*/
58+
@send
59+
external next: t<'a> => promise<value<'a>> = "next"

src/Core__Console.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424

2525
@val external trace: unit => unit = "console.trace"
2626

27-
@val external timeStart: string => unit = "console.timeStart"
27+
@val external time: string => unit = "console.time"
2828
@val external timeEnd: string => unit = "console.timeEnd"

0 commit comments

Comments
 (0)