2013-04-28 12:01:05 +00:00
|
|
|
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-04-28 12:01:05 +00:00
|
|
|
|
2013-07-27 11:11:18 +00:00
|
|
|
import JavaScript (JSObject)
|
2013-07-29 21:23:04 +00:00
|
|
|
import Native.JavaScript
|
2013-04-28 12:01:05 +00:00
|
|
|
|
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'] }
|
|
|
|
-}
|
2013-04-28 12:01:05 +00:00
|
|
|
toRecord : JSObject -> a
|
2013-07-29 21:23:04 +00:00
|
|
|
toRecord = Native.JavaScript.toRecord
|
2013-04-28 12:01:05 +00:00
|
|
|
|
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" }
|
|
|
|
-}
|
2013-04-28 12:01:05 +00:00
|
|
|
fromRecord : a -> JSObject
|
2013-07-29 21:23:04 +00:00
|
|
|
fromRecord = Native.JavaScript.fromRecord
|