Skip to content

Commit 77e46e4

Browse files
committed
fixed maps:get, maps:put, Map.put, Map.get broken
1 parent 796a0ad commit 77e46e4

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/javascript/lib/core/erlang_compat/maps.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ function is_non_primitive(key) {
1818
);
1919
}
2020

21+
function __put(map, key, value) {
22+
const map2 = new Map(map);
23+
24+
if (is_non_primitive(key)) {
25+
for (const map_key of map.keys()) {
26+
if (erlang.equals(map_key, key)) {
27+
map2.set(map_key, value);
28+
return map2;
29+
}
30+
}
31+
}
32+
33+
map2.set(key, value);
34+
return map2;
35+
}
36+
2137
function __has(map, key) {
2238
if (is_non_primitive(key)) {
2339
for (const map_key of map.keys()) {
@@ -142,10 +158,7 @@ function put(key, value, map1) {
142158
return new ErlangTypes.Tuple(BADMAP, map1);
143159
}
144160

145-
const map2 = new Map(map1);
146-
map2.set(key, value);
147-
148-
return map2;
161+
return __put(map1, key, value);
149162
}
150163

151164
function merge(map1, map2) {
@@ -180,7 +193,7 @@ function get(...args) {
180193
return new ErlangTypes.Tuple(BADMAP, map);
181194
}
182195

183-
if (is_key(key)) {
196+
if (is_key(key, map)) {
184197
return __get(map, key);
185198
}
186199

@@ -196,7 +209,7 @@ function take(key, map1) {
196209
return new ErlangTypes.Tuple(BADMAP, map1);
197210
}
198211

199-
if (!is_key(key)) {
212+
if (!is_key(key, map1)) {
200213
return ERROR;
201214
}
202215

src/javascript/tests/core/erlang_compat/maps_spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ test('is_key', (t) => {
3838
t.is(result, false);
3939
});
4040

41+
test('get/2', (t) => {
42+
const myMap = new Map([['a', 1], ['b', 2]]);
43+
const result = Core.maps.get('a', myMap);
44+
t.is(result, 1);
45+
});
46+
47+
test('get/3', (t) => {
48+
let myMap = new Map([['a', 1], ['b', 2]]);
49+
let result = Core.maps.get('a', myMap);
50+
t.is(result, 1);
51+
52+
myMap = new Map([['a', 1], ['b', 2]]);
53+
result = Core.maps.get('c', myMap, "undefined");
54+
t.is(result, "undefined");
55+
});
56+
57+
test('put/3', (t) => {
58+
const keyMap = new Map([['a', 5]]);
59+
60+
let myMap = new Map([]);
61+
myMap = Core.maps.put(keyMap, 5, myMap);
62+
myMap = Core.maps.put(new Map([['a', 5]]), 6, myMap);
63+
64+
const result = Core.maps.get(new Map([['a', 5]]), myMap);
65+
t.is(result, 6);
66+
});
67+
68+
test('take/2', (t) => {
69+
const myMap = new Map([['a', 1], ['b', 2]]);
70+
var a, result;
71+
[a, result] = Core.maps.take('a', myMap);
72+
t.is(a, 1);
73+
t.is(result.has('a'), false);
74+
});
75+
4176
test('remove', (t) => {
4277
let myMap = new Map([['a', 1], ['b', 2]]);
4378
let result = Core.maps.remove('a', myMap);

0 commit comments

Comments
 (0)