on master help sign in
Convert JavaScript and JSON docs.
1aef98dMax Goldstein
libraries/JavaScript.elm+20−4
@@ -1,6 +1,22 @@
1
1
2module JavaScript where
2module JavaScript where
3
3
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+
4import Native.JavaScript
20import Native.JavaScript
5import open Basics
21import open Basics
6
22
@@ -45,12 +61,12 @@ fromString : String -> JSString
45fromString = Native.JavaScript.fromString
61fromString = Native.JavaScript.fromString
46
62
47{--
63{--
48−-- Turn an `Element` into a plain old DOM node.
64+{-| Turn an `Element` into a DOM node. -}
49fromElement : Element -> JSDomNode
65fromElement : Element -> JSDomNode
50fromElement = Native.JavaScript.fromElement
66fromElement = Native.JavaScript.fromElement
51
67
52−-- Turn a DOM node into an `Element`. You can resize the node
68+{-| Turn a DOM node into an `Element`. You can resize the node
53−-- using the normal `width` and `height` functions.
69+using the normal `width` and `height` functions. -}
54toElement : Int -> Int -> JSDomNode -> Element
70toElement : Int -> Int -> JSDomNode -> Element
55toElement = Native.JavaScript.toElement
71toElement = Native.JavaScript.toElement
56−--}
72+--}
libraries/Json.elm+51−39
@@ -1,8 +1,19 @@
1
1
2−-- TOOD: Evan please review texts
3−
4module Json where
2module Json where
5
3
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+
6import open Basics
17import open Basics
7import Dict
18import Dict
8import Maybe (Maybe)
19import Maybe (Maybe)
@@ -10,9 +21,9 @@ import JavaScript as JS
10import Native.Json
21import Native.Json
11import JavaScript (JSObject, JSString)
22import JavaScript (JSObject, JSString)
12
23
13−-- This datatype can represent all valid values that can be held in a JSON
24+{-| 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)
25+object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
15−-- which is a mapping from strings to Json Values.
26+which is a mapping from strings to Json Values. -}
16data JsonValue
27data JsonValue
17 = String String
28 = String String
18 | Number Float
29 | Number Float
@@ -24,42 +35,43 @@ data JsonValue
24
35
25-- String Converters
36-- String Converters
26
37
27−-- Convert a `JsonValue` into a prettified string.
38+{-| Convert a `JsonValue` into a prettified string.
28−-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
39+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.
40+be used for indentation in the prettified string version of the JSON. -}
30toString : String -> JsonValue -> String
41toString : String -> JsonValue -> String
31toString sep v = JS.toString (Native.Json.toJSString sep v)
42toString sep v = JS.toString (Native.Json.toJSString sep v)
32
43
33−-- Convert a proper JSON object into a JavaScript string.
44+{-| 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
45+Note that the type JSString seen here is not the same as the type constructor
35−-- JsonString used elsewhere in this module.
46+JsonString used elsewhere in this module. -}
36toJSString : String -> JsonValue -> JSString
47toJSString : String -> JsonValue -> JSString
37toJSString = Native.Json.toJSString
48toJSString = Native.Json.toJSString
38
49
39−-- Parse a string representation of a proper JSON object into
50+{-| Parse a string representation of a proper JSON object into
40−-- its Elm representation.
51+its Elm representation. -}
41fromString : String -> Maybe JsonValue
52fromString : String -> Maybe JsonValue
42fromString s = Native.Json.fromJSString (JS.fromString s)
53fromString s = Native.Json.fromJSString (JS.fromString s)
43
54
44−-- Parse a JavaScript string representation of a proper JSON object into
55+{-| Parse a JavaScript string representation of a proper JSON object into
45−-- its Elm representation.
56+its Elm representation. -}
46fromJSString : JSString -> Maybe JsonValue
57fromJSString : JSString -> Maybe JsonValue
47fromJSString = Native.Json.fromJSString
58fromJSString = Native.Json.fromJSString
48
59
49−-- Convert a JS object into a `JsonValue`.
60+{-| Convert a JS object into a `JsonValue`. -}
50fromJSObject : JSObject -> JsonValue
61fromJSObject : JSObject -> JsonValue
51fromJSObject = Native.Json.fromJSObject
62fromJSObject = Native.Json.fromJSObject
52
63
53−-- Convert a `JsonValue` into a `JSObject`. Paired with the
64+{-| Convert a `JsonValue` into a `JSObject`. Paired with the
54−-- [`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
65+[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
55−-- This lets you convert strings into Elm records:
66+This lets you convert strings into Elm records:
56−--
67+
57−-- import JavaScript.Experimental as JS
68+ import JavaScript.Experimental as JS
58−--
69+
59−-- stringToRecord str =
70+ stringToRecord str =
60−-- case fromString str of
71+ case fromString str of
61−-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
72+ Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
62−-- Nothing -> Nothing
73+ Nothing -> Nothing
74+-}
63toJSObject : JsonValue -> JSObject
75toJSObject : JsonValue -> JSObject
64toJSObject = Native.Json.toJSObject
76toJSObject = Native.Json.toJSObject
65
77
@@ -83,8 +95,8 @@ toJSObject = Native.Json.toJSObject
83
95
84 -- Extract Elm values from dictionaries of Json values
96 -- Extract Elm values from dictionaries of Json values
85
97
86− -- Find a value in a Json Object using the passed get function. If the key is
98+ {-| 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.
99+ not found, this returns the given default/base value. -}
88 find get base =
100 find get base =
89 let f key dict =
101 let f key dict =
90 case Dict.lookup key dict of
102 case Dict.lookup key dict of
@@ -92,28 +104,28 @@ toJSObject = Native.Json.toJSObject
92 Just v -> get v
104 Just v -> get v
93 in f
105 in f
94
106
95− -- Find a string value in an Elm Json object. If the key is not found or the
107+ {-| 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.
108+ value found is not a string, this returns the empty string. -}
97 findString : String -> Object -> String
109 findString : String -> Object -> String
98 findString = find string ""
110 findString = find string ""
99
111
100− -- Find a number value in an Elm Json object. If the key is not found or the
112+ {-| 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
113+ value found is not a number, this returns 0 -}
102 findNumber : String -> Object -> Float
114 findNumber : String -> Object -> Float
103 findNumber = find number 0
115 findNumber = find number 0
104
116
105− -- Find a boolean value in an Elm Json object. If the key is not found or the
117+ {-| 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.
118+ value found is not a boolean, this returns the False. -}
107 findBoolean : String -> Object -> Bool
119 findBoolean : String -> Object -> Bool
108 findBoolean = find boolean False
120 findBoolean = find boolean False
109
121
110− -- Find an array value in an Elm Json object. If the key is not found or the
122+ {-| 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.
123+ value found is not an array, this returns an empty list. -}
112 findArray : String -> Object -> [JsonValue]
124 findArray : String -> Object -> [JsonValue]
113 findArray = find array []
125 findArray = find array []
114
126
115− -- Find an object value in an Elm Json object. If the key is not found or the
127+ {-| 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.
128+ value found is not an object, this returns an empty object. -}
117 findObject : String -> Object -> Object
129 findObject : String -> Object -> Object
118 findObject = find object Dict.empty
130 findObject = find object Dict.empty
119− --}
131+ --}