Skip to content

Commit e97c2a9

Browse files
jderochervlkillusionalsagacity
authored andcommitted
String.unsafeGet (rescript-lang#223)
1 parent 296ea59 commit e97c2a9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Core__String.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let compare = (a: string, b: string) =>
1313

1414
@get external length: string => int = "length"
1515
@get_index external get: (string, int) => option<string> = ""
16+
@get_index external getUnsafe: (string, int) => string = ""
1617
@send external charAt: (string, int) => string = "charAt"
1718

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

src/Core__String.resi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ String.get(`JS`, 4) == None
150150
@get_index
151151
external get: (string, int) => option<string> = ""
152152

153+
/**
154+
`getUnsafe(str, index)` returns an `string` at the given `index` number.
155+
156+
This is _unsafe_, meaning it will return `undefined` value if `index` does not exist in `string`.
157+
158+
Use `String.getUnsafe` only when you are sure the `index` exists.
159+
## Examples
160+
161+
```rescript
162+
String.getUnsafe("ReScript", 0) == "R"
163+
String.getUnsafe("Hello", 4) == "o"
164+
```
165+
*/
166+
@get_index
167+
external getUnsafe: (string, int) => string = ""
168+
153169
/**
154170
`charAt(str, index)` gets the character at `index` within string `str`. If
155171
`index` is negative or greater than the length of `str`, it returns the empty

0 commit comments

Comments
 (0)