on master help sign in
Mark Char and Text values to distinguish them from Strings
2c080b6Evan Czaplicki
compiler/Generate/JavaScript.hs+1−1
@@ -62,7 +62,7 @@ internalImports name =
62
62
63literal lit =
63literal lit =
64 case lit of
64 case lit of
65− Chr c -> string [c]
65+ Chr c -> obj "_N.chr" <| string [c]
66 Str s -> string s
66 Str s -> string s
67 IntNum n -> IntLit () n
67 IntNum n -> IntLit () n
68 FloatNum n -> NumLit () n
68 FloatNum n -> NumLit () n
libraries/Native/List.js+4−1
@@ -53,7 +53,10 @@ Elm.Native.List.make = function(elm) {
53 }
53 }
54
54
55 function append(xs,ys) {
55 function append(xs,ys) {
56− if (typeof xs === "string") { return xs.concat(ys); }
56+ if (typeof xs === "string") {
57+ if (xs.isText) return Utils.txt(xs.concat(ys));
58+ return xs.concat(ys);
59+ }
57 if (xs.ctor === '[]') { return ys; }
60 if (xs.ctor === '[]') { return ys; }
58 var root = Cons(xs._0, Nil);
61 var root = Cons(xs._0, Nil);
59 var curr = root;
62 var curr = root;
libraries/Native/Show.js+3−2
@@ -21,9 +21,10 @@ Elm.Native.Show.make = function(elm) {
21 return v ? "True" : "False";
21 return v ? "True" : "False";
22 } else if (type === "number") {
22 } else if (type === "number") {
23 return v+"";
23 return v+"";
24+ } else if (v.isChar && v instanceof String) {
25+ return "'" + addSlashes(v) + "'";
24 } else if (type === "string") {
26 } else if (type === "string") {
25− var quote = v.length === 1 ? "'" : '"';
26− return quote + addSlashes(v) + quote;
27+ return '"' + addSlashes(v) + '"';
27 } else if (type === "object" && '_' in v) {
28 } else if (type === "object" && '_' in v) {
28 var output = [];
29 var output = [];
29 for (var k in v._) {
30 for (var k in v._) {
libraries/Native/String.js+6−14
@@ -9,7 +9,7 @@ Elm.Native.String.make = function(elm) {
9 var Char = Elm.Char.make(elm);
9 var Char = Elm.Char.make(elm);
10 var Maybe = Elm.Maybe.make(elm);
10 var Maybe = Elm.Maybe.make(elm);
11 var JS = Elm.JavaScript.make(elm);
11 var JS = Elm.JavaScript.make(elm);
12− var Tuple2 = Elm.Native.Utils.make(elm).Tuple2;
12+ var Utils = Elm.Native.Utils.make(elm);
13
13
14 function isEmpty(str) {
14 function isEmpty(str) {
15 return str.length === 0;
15 return str.length === 0;
@@ -18,8 +18,8 @@ Elm.Native.String.make = function(elm) {
18 return chr + str;
18 return chr + str;
19 }
19 }
20 function uncons(str) {
20 function uncons(str) {
21− var chr;
21+ var hd;
22− return (chr = str[0]) ? Maybe.Just(Tuple2(chr, str.slice(1)))
22+ return (hd = str[0]) ? Maybe.Just(Utils.Tuple2(Utils.chr(hd), str.slice(1)))
23 : Maybe.Nothing;
23 : Maybe.Nothing;
24 }
24 }
25 function length(str) {
25 function length(str) {
@@ -54,11 +54,11 @@ Elm.Native.String.make = function(elm) {
54 function join(sep, strs) {
54 function join(sep, strs) {
55 return JS.fromList(strs).join(sep);
55 return JS.fromList(strs).join(sep);
56 }
56 }
57− function repeat(n, chr) {
57+ function repeat(n, str) {
58 var result = '';
58 var result = '';
59 while (n > 0) {
59 while (n > 0) {
60− if (n & 1) result += chr;
60+ if (n & 1) result += str;
61− n >>= 1, chr += chr;
61+ n >>= 1, str += str;
62 }
62 }
63 return result;
63 return result;
64 }
64 }
@@ -103,15 +103,9 @@ Elm.Native.String.make = function(elm) {
103 function words(str) {
103 function words(str) {
104 return JS.toList(str.split(/\s+/g));
104 return JS.toList(str.split(/\s+/g));
105 }
105 }
106− function unwords(str) {
107− return JS.fromList(str).join(' ');
108− }
109 function lines(str) {
106 function lines(str) {
110 return JS.toList(str.split(/\r\n|\r|\n/g));
107 return JS.toList(str.split(/\r\n|\r|\n/g));
111 }
108 }
112− function unlines(str) {
113− return JS.fromList(str).join('\n');
114− }
115
109
116 function toUpper(str) {
110 function toUpper(str) {
117 return str.toUpperCase();
111 return str.toUpperCase();
@@ -219,9 +213,7 @@ Elm.Native.String.make = function(elm) {
219 trimRight: trimRight,
213 trimRight: trimRight,
220
214
221 words: words,
215 words: words,
222− unwords: unwords,
223 lines: lines,
216 lines: lines,
224− unlines: unlines,
225
217
226 toUpper: toUpper,
218 toUpper: toUpper,
227 toLower: toLower,
219 toLower: toLower,
libraries/Native/Text.js+11−11
@@ -5,7 +5,7 @@ Elm.Native.Text.make = function(elm) {
5 if (elm.Native.Text.values) return elm.Native.Text.values;
5 if (elm.Native.Text.values) return elm.Native.Text.values;
6
6
7 var JS = Elm.JavaScript.make(elm);
7 var JS = Elm.JavaScript.make(elm);
8− var htmlHeight = Elm.Native.Utils.make(elm).htmlHeight;
8+ var Utils = Elm.Native.Utils.make(elm);
9 var Color = Elm.Native.Color.make(elm);
9 var Color = Elm.Native.Color.make(elm);
10 var Element = Elm.Graphics.Element.make(elm);
10 var Element = Elm.Graphics.Element.make(elm);
11 var show = Elm.Native.Show.make(elm).show;
11 var show = Elm.Native.Show.make(elm).show;
@@ -51,15 +51,16 @@ Elm.Native.Text.make = function(elm) {
51 return arr.join('<br/>');
51 return arr.join('<br/>');
52 }
52 }
53
53
54− function toText(str) { return properEscape(JS.fromString(str)); }
54+ function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
55
55
56+ function addTag(tag) {
56− function addTag(tag) { return function(text) {
57+ return function(text) {
57− return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
58+ return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
59+ }
58 }
60 }
59− }
60
61
61 function addStyle(style, value, text) {
62 function addStyle(style, value, text) {
62− return "<span style='" + style + ":" + value + "'>" + text + "</span>";
63+ return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
63 }
64 }
64
65
65 function typeface(name, text) {
66 function typeface(name, text) {
@@ -75,8 +76,8 @@ Elm.Native.Text.make = function(elm) {
75 var bold = addTag('b');
76 var bold = addTag('b');
76
77
77 function extract(c) {
78 function extract(c) {
78− if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
79+ if (c._3 === 1) { return 'rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')'; }
79− return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
80+ return 'rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')';
80 }
81 }
81 function color(c, text) {
82 function color(c, text) {
82 return addStyle('color', extract(c), text);
83 return addStyle('color', extract(c), text);
@@ -87,7 +88,7 @@ Elm.Native.Text.make = function(elm) {
87 return addStyle('text-decoration', 'line-through', text);
88 return addStyle('text-decoration', 'line-through', text);
88 }
89 }
89 function link(href, text) {
90 function link(href, text) {
90− return "<a href='" + toText(href) + "'>" + text + "</a>";
91+ return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
91 }
92 }
92
93
93 function position(pos) {
94 function position(pos) {
@@ -96,7 +97,7 @@ Elm.Native.Text.make = function(elm) {
96 _0: '<div style="padding:0;margin:0;text-align:' +
97 _0: '<div style="padding:0;margin:0;text-align:' +
97 pos + '">' + text + '</div>'
98 pos + '">' + text + '</div>'
98 };
99 };
99− var p = A2(htmlHeight, 0, text);
100+ var p = A2(Utils.htmlHeight, 0, text);
100 return A3(Element.newElement, p._0, p._1, e);
101 return A3(Element.newElement, p._0, p._1, e);
101 }
102 }
102 }
103 }
@@ -132,5 +133,4 @@ Elm.Native.Text.make = function(elm) {
132
133
133 asText : asText
134 asText : asText
134 };
135 };
135−
136};
136};
libraries/Native/Utils.js+14
@@ -60,6 +60,18 @@ Elm.Native.Utils.make = function(elm) {
60 var Tuple0 = { ctor: "_Tuple0" };
60 var Tuple0 = { ctor: "_Tuple0" };
61 function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
61 function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
62
62
63+ function chr(c) {
64+ var x = new String(c);
65+ x.isChar = true;
66+ return x;
67+ }
68+
69+ function txt(str) {
70+ var t = new String(str);
71+ t.isText = true;
72+ return t;
73+ }
74+
63 var count = 0;
75 var count = 0;
64 function guid(_) { return count++ }
76 function guid(_) { return count++ }
65
77
@@ -149,6 +161,8 @@ Elm.Native.Utils.make = function(elm) {
149 compare:F2(compare),
161 compare:F2(compare),
150 Tuple0:Tuple0,
162 Tuple0:Tuple0,
151 Tuple2:Tuple2,
163 Tuple2:Tuple2,
164+ chr:chr,
165+ txt:txt,
152 copy: copy,
166 copy: copy,
153 remove: remove,
167 remove: remove,
154 replace: replace,
168 replace: replace,
libraries/String.elm+1−12
@@ -6,7 +6,7 @@ module String where
6 map, filter, foldl, foldr, any, all, repeat
6 map, filter, foldl, foldr, any, all, repeat
7
7
8# Split and Join
8# Split and Join
9−@docs split, join, words, unwords, lines, unlines
9+@docs split, join, words, lines
10
10
11# Get Substrings
11# Get Substrings
12@docs sub, left, right, dropLeft, dropRight
12@docs sub, left, right, dropLeft, dropRight
@@ -23,7 +23,6 @@ Cosmetic operations such as padding with extra characters or trimming whitespace
23@docs toUpper, toLower,
23@docs toUpper, toLower,
24 pad, padLeft, padRight,
24 pad, padLeft, padRight,
25 trim, trimLeft, trimRight
25 trim, trimLeft, trimRight
26−
27-}
26-}
28
27
29import Native.String
28import Native.String
@@ -147,21 +146,11 @@ trimRight = Native.String.trimRight
147words : String -> [String]
146words : String -> [String]
148words = Native.String.words
147words = Native.String.words
149
148
150−{-|
151−-}
152−unwords : [String] -> String
153−unwords = Native.String.unwords
154−
155{-|
149{-|
156-}
150-}
157lines : String -> [String]
151lines : String -> [String]
158lines = Native.String.lines
152lines = Native.String.lines
159
153
160−{-|
161−-}
162−unlines : [String] -> String
163−unlines = Native.String.unlines
164−
165{-|
154{-|
166-}
155-}
167toUpper : String -> String
156toUpper : String -> String