Skip to content

Commit 58de0e0

Browse files
committed
introduce Char type
1 parent 4bd42a1 commit 58de0e0

File tree

3 files changed

+188
-117
lines changed

3 files changed

+188
-117
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22

33
## Module Data.String
44

5+
### Types
6+
7+
newtype Char
8+
9+
510
### Values
611

712
charAt :: Number -> String -> String
813

14+
charAt' :: Number -> String -> Maybe Char
15+
916
charCodeAt :: Number -> String -> Number
1017

18+
charCodeOf :: Char -> Number
19+
1120
drop :: Number -> String -> String
1221

22+
fromArray :: [Char] -> String
23+
1324
fromCharCode :: Number -> String
1425

26+
fromCharCode' :: Number -> Char
27+
1528
indexOf :: String -> String -> Number
1629

1730
indexOf' :: String -> Number -> String -> Number
@@ -32,6 +45,8 @@
3245

3346
take :: Number -> String -> String
3447

48+
toArray :: String -> [Char]
49+
3550
toLower :: String -> String
3651

3752
toUpper :: String -> String

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
"bower.json",
1717
"Gruntfile.js",
1818
"package.json"
19-
]
19+
],
20+
"dependencies": {
21+
"purescript-maybe": "*"
22+
}
2023
}

src/Data/String.purs

Lines changed: 169 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,169 @@
1-
module Data.String where
2-
3-
foreign import charAt
4-
"function charAt(i) {\
5-
\ return function(s) {\
6-
\ return s.charAt(i); \
7-
\ };\
8-
\}" :: Number -> String -> String
9-
10-
foreign import charCodeAt
11-
"function charCodeAt(i) {\
12-
\ return function(s) {\
13-
\ return s.charCodeAt(i); \
14-
\ };\
15-
\}" :: Number -> String -> Number
16-
17-
foreign import fromCharCode
18-
"function fromCharCode(n) {\
19-
\ return String.fromCharCode(n);\
20-
\}" :: Number -> String
21-
22-
foreign import indexOf
23-
"function indexOf(x) {\
24-
\ return function(s) {\
25-
\ return s.indexOf(x);\
26-
\ }; \
27-
\}" :: String -> String -> Number
28-
29-
foreign import indexOf'
30-
"function indexOf$prime(x) {\
31-
\ return function(startAt) {\
32-
\ return function(s) {\
33-
\ return s.indexOf(x, startAt);\
34-
\ }; \
35-
\ }; \
36-
\}" :: String -> Number -> String -> Number
37-
38-
foreign import lastIndexOf
39-
"function lastIndexOf(x) {\
40-
\ return function(s) {\
41-
\ return s.lastIndexOf(x);\
42-
\ };\
43-
\}" :: String -> String -> Number
44-
45-
foreign import lastIndexOf'
46-
"function lastIndexOf$prime(x) {\
47-
\ return function(startAt) {\
48-
\ return function(s) {\
49-
\ return s.lastIndexOf(x, startAt);\
50-
\ }; \
51-
\ }; \
52-
\}" :: String -> Number -> String -> Number
53-
54-
foreign import length
55-
"function length(s) {\
56-
\ return s.length;\
57-
\}" :: String -> Number
58-
59-
foreign import localeCompare
60-
"function localeCompare(s1) {\
61-
\ return function(s2) {\
62-
\ return s1.localeCompare(s2);\
63-
\ };\
64-
\}" :: String -> String -> Number
65-
66-
foreign import replace
67-
"function replace(s1) {\
68-
\ return function(s2) {\
69-
\ return function(s3) {\
70-
\ return s3.replace(s1, s2);\
71-
\ };\
72-
\ };\
73-
\}" :: String -> String -> String -> String
74-
75-
foreign import take
76-
"function take(n) {\
77-
\ return function(s) {\
78-
\ return s.substr(0, n);\
79-
\ };\
80-
\}" :: Number -> String -> String
81-
82-
foreign import drop
83-
"function drop(n) {\
84-
\ return function(s) {\
85-
\ return s.substr(n);\
86-
\ };\
87-
\}" :: Number -> String -> String
88-
89-
foreign import split
90-
"function split(sep) {\
91-
\ return function(s) {\
92-
\ return s.split(sep);\
93-
\ };\
94-
\}" :: String -> String -> [String]
95-
96-
foreign import toLower
97-
"function toLower(s) {\
98-
\ return s.toLowerCase();\
99-
\}" :: String -> String
100-
101-
foreign import toUpper
102-
"function toUpper(s) {\
103-
\ return s.toUpperCase();\
104-
\}" :: String -> String
105-
106-
foreign import trim
107-
"function trim(s) {\
108-
\ return s.trim();\
109-
\}" :: String -> String
110-
111-
foreign import joinWith
112-
"function joinWith (s) {\
113-
\ return function (xs) {\
114-
\ return xs.join(s);\
115-
\ };\
116-
\}" :: String -> [String] -> String
1+
module Data.String
2+
(
3+
Char(),
4+
charAt,
5+
charAt',
6+
charCodeAt,
7+
charCodeOf,
8+
fromArray,
9+
fromCharCode,
10+
fromCharCode',
11+
indexOf,
12+
indexOf',
13+
lastIndexOf,
14+
lastIndexOf',
15+
length,
16+
localeCompare,
17+
replace,
18+
take,
19+
drop,
20+
split,
21+
toArray,
22+
toLower,
23+
toUpper,
24+
trim,
25+
joinWith
26+
) where
27+
28+
import Data.Maybe
29+
30+
newtype Char = Char String
31+
32+
-- | Deprecated
33+
foreign import charAt
34+
"function charAt(i) {\
35+
\ return function(s) {\
36+
\ return s.charAt(i); \
37+
\ };\
38+
\}" :: Number -> String -> String
39+
40+
charAt' :: Number -> String -> Maybe Char
41+
charAt' n s =
42+
case charAt n s of
43+
"" -> Nothing
44+
c -> Just $ Char c
45+
46+
charCodeOf :: Char -> Number
47+
charCodeOf (Char s) = charCodeAt 0 s
48+
49+
foreign import charCodeAt
50+
"function charCodeAt(i) {\
51+
\ return function(s) {\
52+
\ return s.charCodeAt(i); \
53+
\ };\
54+
\}" :: Number -> String -> Number
55+
56+
foreign import fromArray
57+
"function fromArray(a) {\
58+
\ return a.join(''); \
59+
\" :: [Char] -> String
60+
61+
-- | Deprecated
62+
foreign import fromCharCode
63+
"function fromCharCode(n) {\
64+
\ return String.fromCharCode(n);\
65+
\}" :: Number -> String
66+
67+
fromCharCode' :: Number -> Char
68+
fromCharCode' c = Char $ fromCharCode c
69+
70+
foreign import indexOf
71+
"function indexOf(x) {\
72+
\ return function(s) {\
73+
\ return s.indexOf(x);\
74+
\ }; \
75+
\}" :: String -> String -> Number
76+
77+
foreign import indexOf'
78+
"function indexOf$prime(x) {\
79+
\ return function(startAt) {\
80+
\ return function(s) {\
81+
\ return s.indexOf(x, startAt);\
82+
\ }; \
83+
\ }; \
84+
\}" :: String -> Number -> String -> Number
85+
86+
foreign import lastIndexOf
87+
"function lastIndexOf(x) {\
88+
\ return function(s) {\
89+
\ return s.lastIndexOf(x);\
90+
\ };\
91+
\}" :: String -> String -> Number
92+
93+
foreign import lastIndexOf'
94+
"function lastIndexOf$prime(x) {\
95+
\ return function(startAt) {\
96+
\ return function(s) {\
97+
\ return s.lastIndexOf(x, startAt);\
98+
\ }; \
99+
\ }; \
100+
\}" :: String -> Number -> String -> Number
101+
102+
foreign import length
103+
"function length(s) {\
104+
\ return s.length;\
105+
\}" :: String -> Number
106+
107+
foreign import localeCompare
108+
"function localeCompare(s1) {\
109+
\ return function(s2) {\
110+
\ return s1.localeCompare(s2);\
111+
\ };\
112+
\}" :: String -> String -> Number
113+
114+
foreign import replace
115+
"function replace(s1) {\
116+
\ return function(s2) {\
117+
\ return function(s3) {\
118+
\ return s3.replace(s1, s2);\
119+
\ };\
120+
\ };\
121+
\}" :: String -> String -> String -> String
122+
123+
foreign import take
124+
"function take(n) {\
125+
\ return function(s) {\
126+
\ return s.substr(0, n);\
127+
\ };\
128+
\}" :: Number -> String -> String
129+
130+
foreign import drop
131+
"function drop(n) {\
132+
\ return function(s) {\
133+
\ return s.substr(n);\
134+
\ };\
135+
\}" :: Number -> String -> String
136+
137+
foreign import split
138+
"function split(sep) {\
139+
\ return function(s) {\
140+
\ return s.split(sep);\
141+
\ };\
142+
\}" :: String -> String -> [String]
143+
144+
foreign import toArray
145+
"function toArray(s) {\
146+
\ return s.split('');\
147+
\}" :: String -> [Char]
148+
149+
foreign import toLower
150+
"function toLower(s) {\
151+
\ return s.toLowerCase();\
152+
\}" :: String -> String
153+
154+
foreign import toUpper
155+
"function toUpper(s) {\
156+
\ return s.toUpperCase();\
157+
\}" :: String -> String
158+
159+
foreign import trim
160+
"function trim(s) {\
161+
\ return s.trim();\
162+
\}" :: String -> String
163+
164+
foreign import joinWith
165+
"function joinWith (s) {\
166+
\ return function (xs) {\
167+
\ return xs.join(s);\
168+
\ };\
169+
\}" :: String -> [String] -> String

0 commit comments

Comments
 (0)