on master help sign in
Convert JavaScript and JSON docs.
commit1aef98dbf8dba61fa5a942e5b4eb7e94d2519f17 authorMax Goldstein <maxgoldstein1@gmail.com> dateTue 10 Sep 2013, 09:46 (13 years ago) parentc69ff2a
2 files changed+71−43
libraries/JavaScript.elm+20−4 libraries/Json.elm+51−39
libraries/JavaScript.elm+20−4
@@ -1,6 +1,22 @@
11
22module JavaScript where
33
4+{-|This library does basic conversions between Elm and JS values. This allows
5+the internal data structures of Elm to change and improve with no impact on JS
6+integration.
7+
8+# Javascript to Elm
9+@docs toString, toInt, toFloat, toBool, toList
10+
11+# JavaScript from Elm
12+@docs fromString, fromInt, fromFloat, fromBool fromList
13+
14+-}
15+{-
16+# DOM Nodes and Elements
17+@docs toElement, fromElement
18+-}
19+
420import Native.JavaScript
521import open Basics
622
@@ -45,12 +61,12 @@ fromString : String -> JSString
4561fromString = Native.JavaScript.fromString
4662
4763{--
48−-- Turn an `Element` into a plain old DOM node.
64+{-| Turn an `Element` into a DOM node. -}
4965fromElement : Element -> JSDomNode
5066fromElement = Native.JavaScript.fromElement
5167
52−-- Turn a DOM node into an `Element`. You can resize the node
53−-- using the normal `width` and `height` functions.
68+{-| Turn a DOM node into an `Element`. You can resize the node
69+using the normal `width` and `height` functions. -}
5470toElement : Int -> Int -> JSDomNode -> Element
5571toElement = Native.JavaScript.toElement
56−--}
72+--}
libraries/Json.elm+51−39
@@ -1,8 +1,19 @@
11
2−-- TOOD: Evan please review texts
3−
42module Json where
53
4+{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
5+
6+# Strings
7+@docs fromString, toString
8+
9+# JS Strings
10+@docs fromJSString, toJSString
11+
12+# JS Objects
13+@docs fromJSObject, toJSObject
14+
15+-}
16+
617import open Basics
718import Dict
819import Maybe (Maybe)
@@ -10,9 +21,9 @@ import JavaScript as JS
1021import Native.Json
1122import JavaScript (JSObject, JSString)
1223
13−-- This datatype can represent all valid values that can be held in a JSON
14−-- object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
15−-- which is a mapping from strings to Json Values.
24+{-| This datatype can represent all valid values that can be held in a JSON
25+object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
26+which is a mapping from strings to Json Values. -}
1627data JsonValue
1728 = String String
1829 | Number Float
@@ -24,42 +35,43 @@ data JsonValue
2435
2536-- String Converters
2637
27−-- Convert a `JsonValue` into a prettified string.
28−-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
29−-- be used for indentation in the prettified string version of the JSON.
38+{-| Convert a `JsonValue` into a prettified string.
39+The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
40+be used for indentation in the prettified string version of the JSON. -}
3041toString : String -> JsonValue -> String
3142toString sep v = JS.toString (Native.Json.toJSString sep v)
3243
33−-- Convert a proper JSON object into a JavaScript string.
34−-- Note that the type JSString seen here is not the same as the type constructor
35−-- JsonString used elsewhere in this module.
44+{-| Convert a proper JSON object into a JavaScript string.
45+Note that the type JSString seen here is not the same as the type constructor
46+JsonString used elsewhere in this module. -}
3647toJSString : String -> JsonValue -> JSString
3748toJSString = Native.Json.toJSString
3849
39−-- Parse a string representation of a proper JSON object into
40−-- its Elm representation.
50+{-| Parse a string representation of a proper JSON object into
51+its Elm representation. -}
4152fromString : String -> Maybe JsonValue
4253fromString s = Native.Json.fromJSString (JS.fromString s)
4354
44−-- Parse a JavaScript string representation of a proper JSON object into
45−-- its Elm representation.
55+{-| Parse a JavaScript string representation of a proper JSON object into
56+its Elm representation. -}
4657fromJSString : JSString -> Maybe JsonValue
4758fromJSString = Native.Json.fromJSString
4859
49−-- Convert a JS object into a `JsonValue`.
60+{-| Convert a JS object into a `JsonValue`. -}
5061fromJSObject : JSObject -> JsonValue
5162fromJSObject = Native.Json.fromJSObject
5263
53−-- Convert a `JsonValue` into a `JSObject`. Paired with the
54−-- [`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
55−-- This lets you convert strings into Elm records:
56−--
57−-- import JavaScript.Experimental as JS
58−--
59−-- stringToRecord str =
60−-- case fromString str of
61−-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
62−-- Nothing -> Nothing
64+{-| Convert a `JsonValue` into a `JSObject`. Paired with the
65+[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
66+This lets you convert strings into Elm records:
67+
68+ import JavaScript.Experimental as JS
69+
70+ stringToRecord str =
71+ case fromString str of
72+ Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
73+ Nothing -> Nothing
74+-}
6375toJSObject : JsonValue -> JSObject
6476toJSObject = Native.Json.toJSObject
6577
@@ -83,8 +95,8 @@ toJSObject = Native.Json.toJSObject
8395
8496 -- Extract Elm values from dictionaries of Json values
8597
86− -- Find a value in a Json Object using the passed get function. If the key is
87− -- not found, this returns the given default/base value.
98+ {-| Find a value in a Json Object using the passed get function. If the key is
99+ not found, this returns the given default/base value. -}
88100 find get base =
89101 let f key dict =
90102 case Dict.lookup key dict of
@@ -92,28 +104,28 @@ toJSObject = Native.Json.toJSObject
92104 Just v -> get v
93105 in f
94106
95− -- Find a string value in an Elm Json object. If the key is not found or the
96− -- value found is not a string, this returns the empty string.
107+ {-| Find a string value in an Elm Json object. If the key is not found or the
108+ value found is not a string, this returns the empty string. -}
97109 findString : String -> Object -> String
98110 findString = find string ""
99111
100− -- Find a number value in an Elm Json object. If the key is not found or the
101− -- value found is not a number, this returns 0
112+ {-| Find a number value in an Elm Json object. If the key is not found or the
113+ value found is not a number, this returns 0 -}
102114 findNumber : String -> Object -> Float
103115 findNumber = find number 0
104116
105− -- Find a boolean value in an Elm Json object. If the key is not found or the
106− -- value found is not a boolean, this returns the False.
117+ {-| Find a boolean value in an Elm Json object. If the key is not found or the
118+ value found is not a boolean, this returns the False. -}
107119 findBoolean : String -> Object -> Bool
108120 findBoolean = find boolean False
109121
110− -- Find an array value in an Elm Json object. If the key is not found or the
111− -- value found is not an array, this returns an empty list.
122+ {-| Find an array value in an Elm Json object. If the key is not found or the
123+ value found is not an array, this returns an empty list. -}
112124 findArray : String -> Object -> [JsonValue]
113125 findArray = find array []
114126
115− -- Find an object value in an Elm Json object. If the key is not found or the
116− -- value found is not an object, this returns an empty object.
127+ {-| Find an object value in an Elm Json object. If the key is not found or the
128+ value found is not an object, this returns an empty object. -}
117129 findObject : String -> Object -> Object
118130 findObject = find object Dict.empty
119− --}
131+ --}