on ac5cd73 help sign in
elk/libraries/JavaScript/Experimental.elm
56 lines 1.6 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 Native.Json
13import Json
15data RawObject = RawObject
17{-| Turn arbitrary JavaScript objects into Elm records.
18Arrays are converted into lists, nested objects are allowed.
19No `null` values or non-homogeneous arrays.
21 -- OK objects
22 { student:"Steve", scores:[83,94,99,72] }
23 { errorLevel:10, critical:true }
25 -- BAD objects
26 { answer:null }
27 { info:[true,42,'what'] }
28-}
29toRecord : RawObject -> a
30toRecord = Native.JavaScript.toRecord
32{-| Turn arbitrary Elm records into JavaScript objects.
33Lists become arrays, nested records are allowed. No ADTs.
35 -- OK records
36 { student="Steve", scores=[83,94,99,72] }
37 { errorLevel=10, critical=True }
39 -- BAD records
40 { answer = Nothing }
41 { result = Left "An error occurred" }
42-}
43fromRecord : a -> RawObject
44fromRecord = Native.JavaScript.fromRecord
46{-| Turn a raw JavaScript object into a `Json.Value` as described in the
47`Json` library.
48-}
49toJson : RawObject -> Json.Value
50toJson = Native.Json.fromJS
52{-| Turn a `Json.Value` (as described in the `Json` library) into a raw
53JavaScript object.
54-}
55fromJson : Json.Value -> RawObject
56fromJson = Native.Json.toJS