File tree 2 files changed +35
-5
lines changed
2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -434,7 +434,7 @@ charCodeAt :: Number -> String -> Number
434
434
435
435
Returns the numeric Unicode value of the character at the given index.
436
436
437
- ** Unsafe:** returns ` NaN ` if the index is out of bounds.
437
+ ** Unsafe:** throws runtime exception if the index is out of bounds.
438
438
439
439
#### ` charAt `
440
440
@@ -444,7 +444,17 @@ charAt :: Number -> String -> Char
444
444
445
445
Returns the character at the given index.
446
446
447
- ** Unsafe:** returns an illegal value if the index is out of bounds.
447
+ ** Unsafe:** throws runtime exception if the index is out of bounds.
448
+
449
+ #### ` char `
450
+
451
+ ``` purescript
452
+ char :: String -> Char
453
+ ```
454
+
455
+ Converts a string of length ` 1 ` to a character..
456
+
457
+ ** Unsafe:** throws runtime exception if length is not ` 1 ` .
448
458
449
459
450
460
Original file line number Diff line number Diff line change 1
1
-- | Unsafe string and character functions.
2
2
module Data.String.Unsafe
3
- ( charAt
3
+ ( char
4
+ , charAt
4
5
, charCodeAt
5
6
) where
6
7
7
8
import Data.Char
8
9
9
10
-- | Returns the numeric Unicode value of the character at the given index.
10
11
-- |
11
- -- | **Unsafe:** returns `NaN` if the index is out of bounds.
12
+ -- | **Unsafe:** throws runtime exception if the index is out of bounds.
12
13
foreign import charCodeAt
13
14
" " "
14
15
function charCodeAt(i) {
15
16
return function(s) {
17
+ if (s.length <= i) {
18
+ throw new Error(" Data.String.Unsafe. charCodeAt : Invalid index." );
19
+ };
16
20
return s.charCodeAt(i);
17
21
};
18
22
}
19
23
" " " :: Number -> String -> Number
20
24
21
25
-- | Returns the character at the given index.
22
26
-- |
23
- -- | **Unsafe:** returns an illegal value if the index is out of bounds.
27
+ -- | **Unsafe:** throws runtime exception if the index is out of bounds.
24
28
foreign import charAt
25
29
" " "
26
30
function charAt(i) {
27
31
return function(s) {
32
+ if (s.length <= i) {
33
+ throw new Error(" Data.String.Unsafe. charAt : Invalid index." );
34
+ };
28
35
return s.charAt(i);
29
36
};
30
37
}
31
38
" " " :: Number -> String -> Char
39
+
40
+ -- | Converts a string of length `1` to a character..
41
+ -- |
42
+ -- | **Unsafe:** throws runtime exception if length is not `1`.
43
+ foreign import char
44
+ " " "
45
+ function $$char(s) {
46
+ if (s.length != 1) {
47
+ throw new Error(" Data.String.Unsafe. char: Expected string of length 1." );
48
+ };
49
+ return s.charAt(0);
50
+ }
51
+ " " " :: String -> Char
You can’t perform that action at this time.
0 commit comments