on 09c91a9 help sign in
elk/libraries/JavaScript/Experimental.elm
42 lines 1.2 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
13data RawObject = RawObject
15{-| Turn arbitrary JavaScript objects into Elm records.
16Arrays are converted into lists, nested objects are allowed.
17No `null` values or non-homogeneous arrays.
19 -- OK objects
20 { student:"Steve", scores:[83,94,99,72] }
21 { errorLevel:10, critical:true }
23 -- BAD objects
24 { answer:null }
25 { info:[true,42,'what'] }
26-}
27toRecord : RawObject -> a
28toRecord = Native.JavaScript.toRecord
30{-| Turn arbitrary Elm records into JavaScript objects.
31Lists become arrays, nested records are allowed. No ADTs.
33 -- OK records
34 { student="Steve", scores=[83,94,99,72] }
35 { errorLevel=10, critical=True }
37 -- BAD records
38 { answer = Nothing }
39 { result = Left "An error occurred" }
40-}
41fromRecord : a -> RawObject
42fromRecord = Native.JavaScript.fromRecord