elm/libraries/JavaScript/Experimental.elm

41 lines
1.2 KiB
Elm
Raw Normal View History

module JavaScript.Experimental where
2013-09-12 21:00:29 +00:00
{-| A pair of very experimental functions for converting between
Elm records and JavaScript objects. It currently uses a best effort
conversion that can cause runtime errors. This API should change and
improve as we find its key failings.
2013-09-12 21:28:23 +00:00
# Converting between Elm records and JS Objects
@docs toRecord, fromRecord
2013-09-12 21:00:29 +00:00
-}
2013-07-27 11:11:18 +00:00
import JavaScript (JSObject)
import Native.JavaScript
2013-09-12 21:00:29 +00:00
{-| Turn arbitrary JavaScript objects into Elm records.
Arrays are converted into lists, nested objects are allowed.
No `null` values or non-homogeneous arrays.
-- OK objects
{ student:"Steve", scores:[83,94,99,72] }
{ errorLevel:10, critical:true }
-- BAD objects
{ answer:null }
{ info:[true,42,'what'] }
-}
toRecord : JSObject -> a
toRecord = Native.JavaScript.toRecord
2013-09-12 21:00:29 +00:00
{-| Turn arbitrary Elm records into JavaScript objects.
Lists become arrays, nested records are allowed. No ADTs.
-- OK records
{ student="Steve", scores=[83,94,99,72] }
{ errorLevel=10, critical=True }
-- BAD records
{ answer = Nothing }
{ result = Left "An error occurred" }
-}
fromRecord : a -> JSObject
fromRecord = Native.JavaScript.fromRecord