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