on 8a2fc7c help sign in
elk/libraries/JavaScript/Experimental.elm
55 lines 1.5 kB history raw
1module JavaScript.Experimental where
2{-| A pair of very experimental functions for converting between
3Elm records and JavaScript objects. It currently uses a best effort
4conversion that can cause runtime errors. This API should change and
5improve as we find its key failings.
7# Converting between Elm records and JS Objects
8@docs toRecord, fromRecord
9-}
11import Native.JavaScript
12import Json
14data RawObject = RawObject
16{-| Turn arbitrary JavaScript objects into Elm records.
17Arrays are converted into lists, nested objects are allowed.
18No `null` values or non-homogeneous arrays.
20 -- OK objects
21 { student:"Steve", scores:[83,94,99,72] }
22 { errorLevel:10, critical:true }
24 -- BAD objects
25 { answer:null }
26 { info:[true,42,'what'] }
27-}
28toRecord : RawObject -> a
29toRecord = Native.JavaScript.toRecord
31{-| Turn arbitrary Elm records into JavaScript objects.
32Lists become arrays, nested records are allowed. No ADTs.
34 -- OK records
35 { student="Steve", scores=[83,94,99,72] }
36 { errorLevel=10, critical=True }
38 -- BAD records
39 { answer = Nothing }
40 { result = Left "An error occurred" }
41-}
42fromRecord : a -> RawObject
43fromRecord = Native.JavaScript.fromRecord
45{-| Turn a raw JavaScript object into a `Json.Value` as described in the
46`Json` library.
47-}
48toJson : RawObject -> Json.Value
49toJson = Native.Json.fromJS
51{-| Turn a `Json.Value` (as described in the `Json` library) into a raw
52JavaScript object.
53-}
54fromJson : Json.Value -> RawObject
55fromJson = Native.Json.toJS