Skip to content

Commit a4edc21

Browse files
committed
Add maps functions found in Map module
1 parent 34fc244 commit a4edc21

File tree

1 file changed

+134
-1
lines changed
  • src/javascript/lib/core/erlang_compat

1 file changed

+134
-1
lines changed

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

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// http://erlang.org/doc/man/maps.html
22
import ErlangTypes from 'erlang-types';
3+
import erlang from './erlang';
34

45
const OK = Symbol.for('ok');
56
const ERROR = Symbol.for('error');
67
const BADMAP = Symbol.for('badmap');
8+
const BADKEY = Symbol.for('badkey');
79

810
function find(key, map) {
9-
if (map instanceof Object === false) {
11+
if (erlang.is_map(map) === false) {
1012
return new ErlangTypes.Tuple(BADMAP, map);
1113
}
1214

@@ -29,7 +31,138 @@ function fold(fun, init, map) {
2931
return acc;
3032
}
3133

34+
function remove(key, map1) {
35+
if (erlang.is_map(map1) === false) {
36+
return new ErlangTypes.Tuple(BADMAP, map1);
37+
}
38+
39+
const map2 = { ...map1 };
40+
41+
delete map2[key];
42+
43+
return map2;
44+
}
45+
46+
function to_list(map) {
47+
if (erlang.is_map(map) === false) {
48+
return new ErlangTypes.Tuple(BADMAP, map);
49+
}
50+
51+
return Object.entries(map).map(entry => {
52+
return ErlangTypes.Tuple(...entry);
53+
});
54+
}
55+
56+
function from_list(list) {
57+
return list.reduce((acc, item) => {
58+
const [key, value] = item;
59+
acc[key] = value;
60+
61+
return acc;
62+
}, {});
63+
}
64+
65+
function keys(map) {
66+
if (erlang.is_map(map) === false) {
67+
return new ErlangTypes.Tuple(BADMAP, map);
68+
}
69+
70+
return Object.keys(map);
71+
}
72+
73+
function values(map) {
74+
if (erlang.is_map(map) === false) {
75+
return new ErlangTypes.Tuple(BADMAP, map);
76+
}
77+
78+
return Object.values(map);
79+
}
80+
81+
function is_key(key, map) {
82+
return map.hasOwnProperty(key);
83+
}
84+
85+
function put(key, value, map1) {
86+
if (erlang.is_map(map1) === false) {
87+
return new ErlangTypes.Tuple(BADMAP, map1);
88+
}
89+
90+
const map2 = { ...map1, [key]: value };
91+
92+
return map2;
93+
}
94+
95+
function merge(map1, map2) {
96+
if (erlang.is_map(map1) === false) {
97+
return new ErlangTypes.Tuple(BADMAP, map1);
98+
}
99+
100+
if (erlang.is_map(map2) === false) {
101+
return new ErlangTypes.Tuple(BADMAP, map2);
102+
}
103+
104+
return { ...map1, ...map2 };
105+
}
106+
107+
function update(key, value, map1) {
108+
if (erlang.is_map(map1) === false) {
109+
return new ErlangTypes.Tuple(BADMAP, map1);
110+
}
111+
112+
if (is_key(key) === false) {
113+
return new ErlangTypes.Tuple(BADKEY, key);
114+
}
115+
116+
return { ...map1, [key]: value };
117+
}
118+
119+
function get(...args) {
120+
const key = args[0];
121+
const map = args[1];
122+
123+
if (erlang.is_map(map) === false) {
124+
return new ErlangTypes.Tuple(BADMAP, map);
125+
}
126+
127+
if (is_key(key)) {
128+
return map[key];
129+
}
130+
131+
if (args.length === 3) {
132+
return args[2];
133+
}
134+
135+
return new ErlangTypes.Tuple(BADKEY, key);
136+
}
137+
138+
function take(key, map1) {
139+
if (erlang.is_map(map1) === false) {
140+
return new ErlangTypes.Tuple(BADMAP, map1);
141+
}
142+
143+
if (!is_key(key)) {
144+
return ERROR;
145+
}
146+
147+
const value = map1[key];
148+
const map2 = { ...map1 };
149+
delete map2[key];
150+
151+
return new ErlangTypes.Tuple(value, map2);
152+
}
153+
32154
export default {
33155
find,
34156
fold,
157+
remove,
158+
to_list,
159+
from_list,
160+
keys,
161+
values,
162+
is_key,
163+
put,
164+
merge,
165+
update,
166+
get,
167+
take,
35168
};

0 commit comments

Comments
 (0)