Skip to content

Commit 3833893

Browse files
committed
changed to use map_to_object/2
1 parent a7ec702 commit 3833893

6 files changed

Lines changed: 46 additions & 82 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ 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
10+
- Added `ElixirScript.JS.map_to_object/2` with options [keys: :string, symbols: false]
1211

1312
### Fixed
1413
- Make sure not to add underscores to erlang functions

lib/elixir_script/lib/js.ex

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ defmodule ElixirScript.JS do
5050
"""
5151
defexternal mutate(object, key, value)
5252

53-
5453
@doc """
5554
Takes the given map and returns an object
5655
Throws an error if any key is not a
@@ -64,24 +63,13 @@ defmodule ElixirScript.JS do
6463
defexternal map_to_object(object)
6564

6665
@doc """
67-
Takes the given map and returns a valid object
66+
Takes the given map and returns an object
6867
Throws an error if any key is not a
6968
number, binary, or atom
7069
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-
8270
```elixir
83-
ElixirScript.JS.symbol_to_string(:js_symbol)
71+
ElixirScript.JS.map_to_object(%{my: "map"}, keys: :string)
8472
```
8573
"""
86-
defexternal symbol_to_string(symbol)
74+
defexternal map_to_object(object, options)
8775
end

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,15 @@ defmodule ElixirScript.Translate.Forms.JS do
105105
{ast, state}
106106
end
107107

108-
def compile({{:., _, [ElixirScript.JS, :map_to_valid_object]}, _, [object]}, state) do
108+
def compile({{:., _, [ElixirScript.JS, :map_to_object]}, _, [object, options]}, state) do
109109
ast = Helpers.call(
110110
J.member_expression(
111111
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")
112+
J.identifier("map_to_object")
127113
),
128114
[
129-
Form.compile!(symbol, state)
115+
Form.compile!(object, state),
116+
Form.compile!(options, state)
130117
]
131118
)
132119

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import ErlangTypes from 'erlang-types';
2+
import lists from './lists';
3+
4+
function get_value(key, list, defaultv = Symbol("undefined")) {
5+
var tuple = lists.keyfind(key, 1, list)
6+
if (tuple) {
7+
[_symbol, value] = keys.values;
8+
return value;
9+
} else {
10+
return defaultv;
11+
}
12+
}
13+
14+
function is_defined(key, list) {
15+
var tuple = lists.keyfind(key, 1, list)
16+
if (tuple) {
17+
return true;
18+
} else {
19+
return false;
20+
}
21+
}

src/javascript/lib/core/functions.js

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Protocol from './protocol';
22
import Core from '../core';
3+
import proplists from './erlang_compat/proplists';
4+
import erlang from './erlang_compat/erlang';
35

46
function call_property(item, property) {
57
if (!property) {
@@ -92,43 +94,28 @@ function build_namespace(ns, ns_string) {
9294
return parent;
9395
}
9496

95-
function map_to_object(map) {
97+
function map_to_object(map, options = []) {
9698
const object = {};
9799

98-
for (const [key, value] of map.entries()) {
99-
if (value instanceof Map) {
100-
object[key] = map_to_object(value);
101-
} else {
102-
object[key] = value;
103-
}
104-
}
105-
106-
return object;
107-
}
108-
109-
function symbol_to_string(symbol) {
110-
const REGEX = /^Symbol\((.*)\)$/;
111-
const string = symbol.toString();
112-
return REGEX.exec(string)[1];
113-
}
100+
var type_keys = proplists.get_value(Symbol("keys"), options);
101+
var symbols = proplists.get_value(Symbol("symbols"), options);
114102

115-
function map_to_valid_object(map) {
116-
const object = {};
117-
118-
for (const [key, value] of map.entries()) {
119-
var key2 = key;
120-
if (typeof key == 'number') {
121-
key2 = key.toString()
122-
} else if (typeof key == 'symbol') {
123-
key2 = symbol_to_string(key)
103+
for (var [key, value] of map.entries()) {
104+
if (type_keys == Symbol("string") && typeof key == 'number') {
105+
key = key.toString();
106+
} else if (
107+
(type_keys == Symbol("string") || symbols != Symbol("undefined"))
108+
&& typeof key == 'symbol'
109+
) {
110+
key = erlang.atom_to_binary(key);
124111
}
125112

126113
if (value instanceof Map) {
127-
object[key2] = map_to_object(value);
128-
} else if (typeof value == 'symbol') {
129-
object[key2] = symbol_to_string(value);
114+
object[key] = map_to_object(value, options);
115+
} else if (symbols != Symbol("undefined") && typeof value == 'symbol') {
116+
object[key] = erlang.atom_to_binary(value);
130117
} else {
131-
object[key2] = value;
118+
object[key] = value;
132119
}
133120
}
134121

@@ -184,8 +171,6 @@ export default {
184171
defimpl,
185172
build_namespace,
186173
map_to_object,
187-
symbol_to_string,
188-
map_to_valid_object,
189174
trampoline,
190175
Recurse,
191176
split_at

test/passes/translate/forms/js_test.exs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,4 @@ defmodule ElixirScript.Translate.Forms.JS.Test do
9696
]
9797
)
9898
end
99-
100-
test "map_to_valid_object/1" do
101-
ast = {{:., [], [ElixirScript.JS, :map_to_valid_object]}, [], [{:entry, [], nil}]}
102-
state = %{function: {:each, nil}, module: Enum, vars: %{:_ => 0, "entry" => 0, "enumerable" => 0, "fun" => 0}}
103-
104-
{js_ast, _} = Form.compile(ast, state)
105-
assert js_ast == J.call_expression(
106-
J.member_expression(
107-
Helpers.functions(),
108-
J.identifier("map_to_valid_object")
109-
),
110-
[
111-
J.identifier("entry0")
112-
]
113-
)
114-
end
11599
end

0 commit comments

Comments
 (0)