File tree Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Expand file tree Collapse file tree 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
434434
435435Returns the numeric Unicode value of the character at the given index.
436436
437- ** Unsafe:** returns ` NaN ` if the index is out of bounds.
437+ ** Unsafe:** throws runtime exception if the index is out of bounds.
438438
439439#### ` charAt `
440440
@@ -444,7 +444,17 @@ charAt :: Number -> String -> Char
444444
445445Returns the character at the given index.
446446
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 ` .
448458
449459
450460
Original file line number Diff line number Diff line change 11-- | Unsafe string and character functions.
22module Data.String.Unsafe
3- ( charAt
3+ ( char
4+ , charAt
45 , charCodeAt
56 ) where
67
78 import Data.Char
89
910 -- | Returns the numeric Unicode value of the character at the given index.
1011 -- |
11- -- | **Unsafe:** returns `NaN` if the index is out of bounds.
12+ -- | **Unsafe:** throws runtime exception if the index is out of bounds.
1213 foreign import charCodeAt
1314 " " "
1415 function charCodeAt(i) {
1516 return function(s) {
17+ if (s.length <= i) {
18+ throw new Error(" Data.String.Unsafe. charCodeAt : Invalid index." );
19+ };
1620 return s.charCodeAt(i);
1721 };
1822 }
1923 " " " :: Number -> String -> Number
2024
2125 -- | Returns the character at the given index.
2226 -- |
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.
2428 foreign import charAt
2529 " " "
2630 function charAt(i) {
2731 return function(s) {
32+ if (s.length <= i) {
33+ throw new Error(" Data.String.Unsafe. charAt : Invalid index." );
34+ };
2835 return s.charAt(i);
2936 };
3037 }
3138 " " " :: 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