Skip to content

Commit cf00817

Browse files
committed
Add ElixirScript.JS.map_to_object/1
1 parent 049d1b4 commit cf00817

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

lib/elixir_script/lib/js.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ defmodule ElixirScript.JS do
4444
ElixirScript.JS.mutate elem, "width", 100
4545
"""
4646
defmacro mutate(object, key, value)
47+
48+
49+
@doc """
50+
Takes the given map and returns an object
51+
Throws an error if any key is not a
52+
number, binary, or atom
53+
ex:
54+
ElixirScript.JS.map_to_object(%{my: "map"})
55+
"""
56+
defmacro map_to_object(object)
4757
end

lib/elixir_script/passes/translate/forms/js.ex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,26 @@ defmodule ElixirScript.Translate.Forms.JS do
9191
Form.compile!(key, state),
9292
true
9393
),
94+
Form.compile!(value, state)
95+
)
96+
97+
{ast, state}
98+
end
99+
100+
def compile({{:., _, [ElixirScript.JS, :map_to_object]}, _, [object]}, state) do
101+
ast = J.call_expression(
102+
J.member_expression(
103+
J.member_expression(
104+
J.identifier("ElixirScript"),
105+
J.member_expression(
106+
J.identifier("Core"),
107+
J.identifier("Functions")
108+
)
109+
),
110+
J.identifier("map_to_object")
111+
),
94112
[
95-
Form.compile!(value, state)
113+
Form.compile!(object, state)
96114
]
97115
)
98116

0 commit comments

Comments
 (0)