Skip to content

Commit 5bdbd78

Browse files
committed
Add map_to_object/2
Squashed commit of the following: commit 2c2198e Author: vans163 <[email protected]> Date: Wed Aug 30 13:00:58 2017 -0400 export default commit 1400a8b Merge: ce710b5 338e792 Author: vans163 <[email protected]> Date: Wed Aug 30 12:57:09 2017 -0400 Merge branch 'map_to_valid_object' of https://github.com/vans163/elixirscript into map_to_valid_object commit ce710b5 Author: vans163 <[email protected]> Date: Wed Aug 30 12:54:37 2017 -0400 fix whitespace commit 3833893 Author: vans163 <[email protected]> Date: Wed Aug 30 12:44:54 2017 -0400 changed to use map_to_object/2 commit a7ec702 Author: vans163 <[email protected]> Date: Fri Aug 25 13:29:38 2017 -0400 fix broken syntax on js_test.ex commit 0b95bc1 Author: vans163 <[email protected]> Date: Fri Aug 25 13:24:01 2017 -0400 add map_to_valid_object/1, symbol_to_string/1 commit 338e792 Author: vans163 <[email protected]> Date: Wed Aug 30 12:44:54 2017 -0400 changed to use map_to_object/2 commit e1cc332 Author: vans163 <[email protected]> Date: Fri Aug 25 13:29:38 2017 -0400 fix broken syntax on js_test.ex commit 8e8e4da Author: vans163 <[email protected]> Date: Fri Aug 25 13:24:01 2017 -0400 add map_to_valid_object/1, symbol_to_string/1
1 parent cc874f3 commit 5bdbd78

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +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_object/2` with options [keys: :string, symbols: false]
1011

1112
### Fixed
1213
- Make sure not to add underscores to erlang functions

lib/elixir_script/lib/js.ex

Lines changed: 11 additions & 1 deletion
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
@@ -62,4 +61,15 @@ defmodule ElixirScript.JS do
6261
```
6362
"""
6463
defexternal map_to_object(object)
64+
65+
@doc """
66+
Takes the given map and returns an object
67+
Throws an error if any key is not a
68+
number, binary, or atom
69+
70+
```elixir
71+
ElixirScript.JS.map_to_object(%{my: "map"}, keys: :string)
72+
```
73+
"""
74+
defexternal map_to_object(object, options)
6575
end

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

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

105105
{ast, state}
106106
end
107+
108+
def compile({{:., _, [ElixirScript.JS, :map_to_object]}, _, [object, options]}, state) do
109+
ast = Helpers.call(
110+
J.member_expression(
111+
Helpers.functions(),
112+
J.identifier("map_to_object")
113+
),
114+
[
115+
Form.compile!(object, state),
116+
Form.compile!(options, state)
117+
]
118+
)
119+
120+
{ast, state}
121+
end
107122
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
22+
23+
export default {
24+
get_value,
25+
is_defined
26+
};

src/javascript/lib/core/functions.js

Lines changed: 19 additions & 3 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,12 +94,26 @@ 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()) {
100+
var type_keys = proplists.get_value(Symbol("keys"), options);
101+
var symbols = proplists.get_value(Symbol("symbols"), options);
102+
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);
111+
}
112+
99113
if (value instanceof Map) {
100-
object[key] = map_to_object(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);
101117
} else {
102118
object[key] = value;
103119
}

src/javascript/tests/core/functions.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,16 @@ test('split_at', t => {
2424
t.deepEqual(Functions.split_at('abc', -1000).values, ['', 'abc']);
2525
t.deepEqual(Functions.split_at('😀abélkm', 4).values, ['😀abé', 'lkm']);
2626
});
27+
28+
test('map_to_object/2', t => {
29+
const map = new Map([
30+
[Symbol.for('key'), "value"],
31+
[Symbol.for('anotherKey'), "value2"],
32+
]);
33+
34+
const options = [new Core.Tuple(Symbol.for('keys'),Symbol.for('strings'))];
35+
36+
const result = Functions.map_to_object(map, options);
37+
38+
t.deepEqual(result, {key: "value", anotherKey: "value"});
39+
});

0 commit comments

Comments
 (0)