Skip to content

Commit 8e8e4da

Browse files
committed
add map_to_valid_object/1, symbol_to_string/1
1 parent 2908051 commit 8e8e4da

6 files changed

Lines changed: 100 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [unreleased]
88
### Added
99
- Reimplement `String.split_at/2` to make sure Unicode library isn't compiled
10+
- Added `ElixirScript.JS.map_to_valid_object/1` to make sure keys are strings, values not symbols
11+
- Added `ElixirScript.JS.symbol_to_string/1` to turn Symbol (Elixir atom) into string
1012

1113
## [0.30.0] - 2017-08-15
1214

lib/elixir_script/lib/js.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,26 @@ defmodule ElixirScript.JS do
6262
```
6363
"""
6464
defexternal map_to_object(object)
65+
66+
@doc """
67+
Takes the given map and returns a valid object
68+
Throws an error if any key is not a
69+
number, binary, or atom
70+
71+
72+
```elixir
73+
ElixirScript.JS.map_to_valid_object(%{my: "map"})
74+
```
75+
"""
76+
defexternal map_to_valid_object(object)
77+
78+
@doc """
79+
Takes the given symbol and returns a string
80+
81+
82+
```elixir
83+
ElixirScript.JS.symbol_to_string(:js_symbol)
84+
```
85+
"""
86+
defexternal symbol_to_string(symbol)
6587
end

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,32 @@ defmodule ElixirScript.Translate.Forms.JS do
104104

105105
{ast, state}
106106
end
107+
108+
def compile({{:., _, [ElixirScript.JS, :map_to_valid_object]}, _, [object]}, state) do
109+
ast = Helpers.call(
110+
J.member_expression(
111+
Helpers.functions(),
112+
J.identifier("map_to_valid_object")
113+
),
114+
[
115+
Form.compile!(object, state)
116+
]
117+
)
118+
119+
{ast, state}
120+
end
121+
122+
def compile({{:., _, [ElixirScript.JS, :symbol_to_string]}, _, [symbol]}, state) do
123+
ast = Helpers.call(
124+
J.member_expression(
125+
Helpers.functions(),
126+
J.identifier("symbol_to_string")
127+
),
128+
[
129+
Form.compile!(symbol, state)
130+
]
131+
)
132+
133+
{ast, state}
134+
end
107135
end

0 commit comments

Comments
 (0)