Skip to content

Commit cfdae14

Browse files
committed
Updated docs. Added JS.update/2
1 parent 01444a1 commit cfdae14

5 files changed

Lines changed: 23 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
* Bugfixes
1616
* Fixed optional parameter implementation
17+
18+
* Breaking
19+
* `JS.update(object, property, value)` has been removed and replaced with `JS.update(object, map)`
20+
This allows you to update multiple values on a javascript object at once.
1721

1822
# v0.15.2
1923
* Enhancements

lib/elixir_script/prelude/js.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ defmodule ElixirScript.JS do
2020
Updates an existing JavaScript object.
2121
2222
ex:
23-
JS.update elem, "width", 100
23+
JS.update elem, %{"width" => 100}
2424
"""
25-
defmacro update(object, property, value) do
25+
defmacro update(object, map) do
2626
end
2727

2828

lib/elixir_script/prelude/kernel.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ defmodule ElixirScript.Kernel do
193193
end
194194
end
195195

196+
@doc """
197+
Provides a convenient way to create a string-based map.
198+
199+
Elixirscript, by default turns the following, `%{a: "b"}` into `{[Symbol.for("a")]: "b"}` in JavaScript. In order to get string keys,
200+
one would have to do `%{"a" => "b"}` which turns into `{a: "b"}` in JavaScript. With `Kernel.object`, you can create string keyed maps
201+
conveniently, `object(a: "b")` which turns into `{a: "b"}`
202+
"""
196203
defmacro object(args) do
197204
args = Enum.map(args, fn
198205
{ k, v } when Kernel.is_atom(k) ->

lib/elixir_script/translator/js.ex

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ defmodule ElixirScript.Translator.JS do
4646
)
4747
end
4848

49-
defp do_translate({:update, _, [object, property, value]}, env) do
50-
Builder.assignment_expression(
51-
:=,
52-
Builder.member_expression(
53-
Translator.translate!(object, env),
54-
Translator.translate!(property, env),
55-
true
56-
),
57-
Translator.translate!(value, env)
58-
)
49+
defp do_translate({:update, _, [object, map]}, env) do
50+
quoted = quote do
51+
Object.assign(unquote(object), unquote(map))
52+
end
53+
54+
Translator.translate!(quoted, env)
5955
end
6056

6157
defp do_translate({:import, _, [module_names, from]}, env) when is_list(module_names) do

test/prelude/js_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ defmodule ElixirScript.Lib.JS.Test do
2626

2727
test "translate update" do
2828
ex_ast = quote do
29-
JS.update A, "b", [1, 2, 3]
29+
JS.update A, %{"b" => [1, 2, 3]}
3030
end
3131

3232
js_code = """
33-
A['b'] = Object.freeze([1, 2, 3])
33+
Object.assign(A, Object.freeze({
34+
b: Object.freeze([1, 2, 3])
35+
}))
3436
"""
3537

3638
assert_translation(ex_ast, js_code)

0 commit comments

Comments
 (0)