Skip to content
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 @@ -16,6 +16,7 @@

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

#### :eyeglasses: Spec Compliance

Expand Down
10 changes: 9 additions & 1 deletion packages/@rescript/runtime/Stdlib_String.res
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ external compare: (string, string) => Stdlib_Ordering.t = "%compare"
@get_index external getUnsafe: (string, int) => string = ""
@send external charAt: (string, int) => string = "charAt"

@send external charCodeAt: (string, int) => int = "charCodeAt"
@send external charCodeAtUnsafe: (string, int) => int = "charCodeAt"

@val @scope("Number") external isNaN: float => bool = "isNaN"
@send external charCodeAt: (string, int) => float = "charCodeAt"
let charCodeAt = (s, i) => {
let c = charCodeAt(s, i)
isNaN(c) ? None : Some(c->Stdlib_Int.fromFloat)
}

@send external codePointAt: (string, int) => option<int> = "codePointAt"

@send external concat: (string, string) => string = "concat"
Expand Down
25 changes: 21 additions & 4 deletions packages/@rescript/runtime/Stdlib_String.resi
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,36 @@ external charAt: (string, int) => string = "charAt"
`charCodeAt(str, index)` returns the character code at position `index` in
string `str` the result is in the range 0-65535, unlike `codePointAt`, so it
will not work correctly for characters with code points greater than or equal
to 0x10000. The return type is `float` because this function returns NaN if
`index` is less than zero or greater than the length of the string.
to 0x10000.
See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN.

## Examples

```rescript
String.charCodeAt(`😺`, 0) == 0xd83d
String.charCodeAt(`😺`, 0) == Some(0xd83d)
String.charCodeAt("", 0) == None
String.codePointAt(`😺`, 0) == Some(0x1f63a)
```
*/
let charCodeAt: (string, int) => option<int>

/**
`charCodeAtUnsafe(str, index)` returns the character code at position `index` in
string `str` the result is in the range 0-65535, unlike `codePointAt`, so it
will not work correctly for characters with code points greater than or equal
to 0x10000.
Beware: If the index is out of range, it will return `NaN` which is not actually a valid int.
See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN.

## Examples

```rescript
String.charCodeAtUnsafe(`😺`, 0) == 0xd83d
String.codePointAt(`😺`, 0) == Some(0x1f63a)
```
*/
@send
external charCodeAt: (string, int) => int = "charCodeAt"
external charCodeAtUnsafe: (string, int) => int = "charCodeAt"

/**
`codePointAt(str, index)` returns the code point at position `index` within
Expand Down
10 changes: 10 additions & 0 deletions packages/@rescript/runtime/lib/es6/Stdlib_String.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@



function charCodeAt(s, i) {
let c = s.charCodeAt(i);
if (Number.isNaN(c)) {
return;
} else {
return c | 0;
}
}

function indexOfOpt(s, search) {
let index = s.indexOf(search);
if (index !== -1) {
Expand Down Expand Up @@ -38,6 +47,7 @@ function capitalize(s) {
}

export {
charCodeAt,
indexOfOpt,
lastIndexOfOpt,
searchOpt,
Expand Down
10 changes: 10 additions & 0 deletions packages/@rescript/runtime/lib/js/Stdlib_String.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
'use strict';


function charCodeAt(s, i) {
let c = s.charCodeAt(i);
if (Number.isNaN(c)) {
return;
} else {
return c | 0;
}
}

function indexOfOpt(s, search) {
let index = s.indexOf(search);
if (index !== -1) {
Expand Down Expand Up @@ -37,6 +46,7 @@ function capitalize(s) {
}
}

exports.charCodeAt = charCodeAt;
exports.indexOfOpt = indexOfOpt;
exports.lastIndexOfOpt = lastIndexOfOpt;
exports.searchOpt = searchOpt;
Expand Down
Loading