Skip to content

Commit 4b9c715

Browse files
authored
Have String.charCodeAt return option<int>; add charCodeAtUnsafe (#7877)
* Have `String.charCodeAt` return `option<int>`; add `charCodeAtUnsafe` * CHANGELOG
1 parent dee0465 commit 4b9c715

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Fix return type of `String.charCodeAt`. https://github.com/rescript-lang/rescript/pull/7864
1818
- Remove support of JSX children spread. https://github.com/rescript-lang/rescript/pull/7869
19+
- Have `String.charCodeAt` return `option<int>`; add `String.charCodeAtUnsafe`. https://github.com/rescript-lang/rescript/pull/7877
1920

2021
#### :eyeglasses: Spec Compliance
2122

packages/@rescript/runtime/Stdlib_String.res

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ external compare: (string, string) => Stdlib_Ordering.t = "%compare"
1717
@get_index external getUnsafe: (string, int) => string = ""
1818
@send external charAt: (string, int) => string = "charAt"
1919

20-
@send external charCodeAt: (string, int) => int = "charCodeAt"
20+
@send external charCodeAtUnsafe: (string, int) => int = "charCodeAt"
21+
22+
@val @scope("Number") external isNaN: float => bool = "isNaN"
23+
@send external charCodeAt: (string, int) => float = "charCodeAt"
24+
let charCodeAt = (s, i) => {
25+
let c = charCodeAt(s, i)
26+
isNaN(c) ? None : Some(c->Stdlib_Int.fromFloat)
27+
}
28+
2129
@send external codePointAt: (string, int) => option<int> = "codePointAt"
2230

2331
@send external concat: (string, string) => string = "concat"

packages/@rescript/runtime/Stdlib_String.resi

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,36 @@ external charAt: (string, int) => string = "charAt"
213213
`charCodeAt(str, index)` returns the character code at position `index` in
214214
string `str` the result is in the range 0-65535, unlike `codePointAt`, so it
215215
will not work correctly for characters with code points greater than or equal
216-
to 0x10000. The return type is `float` because this function returns NaN if
217-
`index` is less than zero or greater than the length of the string.
216+
to 0x10000.
218217
See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN.
219218
220219
## Examples
221220
222221
```rescript
223-
String.charCodeAt(`😺`, 0) == 0xd83d
222+
String.charCodeAt(`😺`, 0) == Some(0xd83d)
223+
String.charCodeAt("", 0) == None
224+
String.codePointAt(`😺`, 0) == Some(0x1f63a)
225+
```
226+
*/
227+
let charCodeAt: (string, int) => option<int>
228+
229+
/**
230+
`charCodeAtUnsafe(str, index)` returns the character code at position `index` in
231+
string `str` the result is in the range 0-65535, unlike `codePointAt`, so it
232+
will not work correctly for characters with code points greater than or equal
233+
to 0x10000.
234+
Beware: If the index is out of range, it will return `NaN` which is not actually a valid int.
235+
See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN.
236+
237+
## Examples
238+
239+
```rescript
240+
String.charCodeAtUnsafe(`😺`, 0) == 0xd83d
224241
String.codePointAt(`😺`, 0) == Some(0x1f63a)
225242
```
226243
*/
227244
@send
228-
external charCodeAt: (string, int) => int = "charCodeAt"
245+
external charCodeAtUnsafe: (string, int) => int = "charCodeAt"
229246

230247
/**
231248
`codePointAt(str, index)` returns the code point at position `index` within

packages/@rescript/runtime/lib/es6/Stdlib_String.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11

22

33

4+
function charCodeAt(s, i) {
5+
let c = s.charCodeAt(i);
6+
if (Number.isNaN(c)) {
7+
return;
8+
} else {
9+
return c | 0;
10+
}
11+
}
12+
413
function indexOfOpt(s, search) {
514
let index = s.indexOf(search);
615
if (index !== -1) {
@@ -38,6 +47,7 @@ function capitalize(s) {
3847
}
3948

4049
export {
50+
charCodeAt,
4151
indexOfOpt,
4252
lastIndexOfOpt,
4353
searchOpt,

packages/@rescript/runtime/lib/js/Stdlib_String.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict';
22

33

4+
function charCodeAt(s, i) {
5+
let c = s.charCodeAt(i);
6+
if (Number.isNaN(c)) {
7+
return;
8+
} else {
9+
return c | 0;
10+
}
11+
}
12+
413
function indexOfOpt(s, search) {
514
let index = s.indexOf(search);
615
if (index !== -1) {
@@ -37,6 +46,7 @@ function capitalize(s) {
3746
}
3847
}
3948

49+
exports.charCodeAt = charCodeAt;
4050
exports.indexOfOpt = indexOfOpt;
4151
exports.lastIndexOfOpt = lastIndexOfOpt;
4252
exports.searchOpt = searchOpt;

0 commit comments

Comments
 (0)