on master help sign in
Add the toList and fromList functions
519484dEvan Czaplicki
libraries/Native/String.js+11−4
@@ -157,8 +157,7 @@ Elm.Native.String.make = function(elm) {
157 return JS.toList(is);
157 return JS.toList(is);
158 }
158 }
159
159
160− function toInt(str) {
160+ function toInt(s) {
161− var s = JS.fromString(str);
162 var len = s.length;
161 var len = s.length;
163 if (len === 0) { return Maybe.Nothing; }
162 if (len === 0) { return Maybe.Nothing; }
164 var start = 0;
163 var start = 0;
@@ -172,8 +171,7 @@ Elm.Native.String.make = function(elm) {
172 return Maybe.Just(parseInt(s, 10));
171 return Maybe.Just(parseInt(s, 10));
173 }
172 }
174
173
175− function toFloat(str) {
174+ function toFloat(s) {
176− var s = JS.fromString(str);
177 var len = s.length;
175 var len = s.length;
178 if (len === 0) { return Maybe.Nothing; }
176 if (len === 0) { return Maybe.Nothing; }
179 var start = 0;
177 var start = 0;
@@ -193,6 +191,13 @@ Elm.Native.String.make = function(elm) {
193 return Maybe.Just(parseFloat(s));
191 return Maybe.Just(parseFloat(s));
194 }
192 }
195
193
194+ function toList(str) {
195+ return JS.toList(str.split('').map(Utils.chr));
196+ }
197+ function fromList(chars) {
198+ return JS.fromList(chars).join('');
199+ }
200+
196 return Elm.Native.String.values = {
201 return Elm.Native.String.values = {
197 isEmpty: isEmpty,
202 isEmpty: isEmpty,
198 cons: F2(cons),
203 cons: F2(cons),
@@ -240,5 +245,7 @@ Elm.Native.String.make = function(elm) {
240
245
241 toInt: toInt,
246 toInt: toInt,
242 toFloat: toFloat,
247 toFloat: toFloat,
248+ toList: toList,
249+ fromList: fromList,
243 };
250 };
244};
251};
libraries/String.elm+24−4
@@ -14,8 +14,8 @@ are enclosed in `"double quotes"`. Strings are *not* lists of characters.
14# Check for Substrings
14# Check for Substrings
15@docs contains, startsWith, endsWith, indexes, indices
15@docs contains, startsWith, endsWith, indexes, indices
16
16
17−# Conversion To Numbers
17+# Conversions
18−@docs toInt, toFloat
18+@docs toInt, toFloat, toList, fromList
19
19
20# Formatting
20# Formatting
21Cosmetic operations such as padding with extra characters or trimming whitespace.
21Cosmetic operations such as padding with extra characters or trimming whitespace.
@@ -85,12 +85,16 @@ filter = Native.String.filter
85reverse : String -> String
85reverse : String -> String
86reverse = Native.String.reverse
86reverse = Native.String.reverse
87
87
88−{-|
88+{-| Reduce a string from the left:
89+
90+ foldl cons "" "time" == "emit"
89-}
91-}
90foldl : (Char -> b -> b) -> b -> String -> b
92foldl : (Char -> b -> b) -> b -> String -> b
91foldl = Native.String.foldl
93foldl = Native.String.foldl
92
94
93−{-|
95+{-| Reduce a string from the right:
96+
97+ foldr cons "" "time" == "time"
94-}
98-}
95foldr : (Char -> b -> b) -> b -> String -> b
99foldr : (Char -> b -> b) -> b -> String -> b
96foldr = Native.String.foldr
100foldr = Native.String.foldr
@@ -289,3 +293,19 @@ toInt = Native.String.toInt
289-}
293-}
290toFloat : String -> Maybe Float
294toFloat : String -> Maybe Float
291toFloat = Native.String.toFloat
295toFloat = Native.String.toFloat
296+
297+{-| Convert a string to a list of characters.
298+
299+ toList "abc" == ['a','b','c']
300+-}
301+toList : String -> [Char]
302+toList = Native.String.toList
303+
304+{-| Convert a list of characters into a String. Can be useful if you
305+want to create a string primarly by consing, perhaps for decoding
306+something.
307+
308+ fromList ['a','b','c'] == "abc"
309+-}
310+fromList : String -> [Char]
311+fromList = Native.String.fromList