Skip to content

Commit d9874df

Browse files
committed
Fix index off by one issues. Add tuple functions
1 parent 7bd6254 commit d9874df

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,54 @@ function is_binary(value) {
197197
return typeof value === 'string' || value instanceof String;
198198
}
199199

200+
function element(n, tuple) {
201+
return tuple.get(n - 1);
202+
}
203+
204+
function setelement(index, tuple1, value) {
205+
const tupleData = [...tuple1.data];
206+
207+
tupleData[index - 1] = value;
208+
209+
return new ErlangTypes.Tuple(...tupleData);
210+
}
211+
212+
function make_tuple(arity, initialValue) {
213+
const list = [];
214+
215+
for (let i = 0; i < arity; i++) {
216+
list.push(initialValue);
217+
}
218+
219+
return new ErlangTypes.Tuple(...list);
220+
}
221+
222+
function insert_element(index, tuple, term) {
223+
const list = [...tuple.data];
224+
list.splice(index - 1, 0, term);
225+
226+
return new ErlangTypes.Tuple(...list);
227+
}
228+
229+
function append_element(tuple, term) {
230+
const list = [...tuple.data];
231+
list.push(term);
232+
233+
return new ErlangTypes.Tuple(...list);
234+
}
235+
236+
function delete_element(index, tuple) {
237+
const list = [...tuple.data];
238+
list.splice(index - 1, 1);
239+
240+
return new ErlangTypes.Tuple(...list);
241+
}
242+
243+
function tuple_to_list(tuple) {
244+
const list = [...tuple.data];
245+
return list;
246+
}
247+
200248
export default {
201249
atom_to_binary,
202250
binary_to_atom,
@@ -239,4 +287,11 @@ export default {
239287
is_tuple,
240288
is_atom,
241289
is_binary,
290+
element,
291+
setelement,
292+
make_tuple,
293+
insert_element,
294+
append_element,
295+
delete_element,
296+
tuple_to_list,
242297
};

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function foldr(fun, acc0, list) {
4545

4646
function keyfind(key, n, tupleList) {
4747
for (const tuple of tupleList) {
48-
if (tuple.get(n) === key) {
48+
if (tuple.get(n - 1) === key) {
4949
return tuple;
5050
}
5151
}
@@ -65,7 +65,7 @@ function keyreplace(key, n, tupleList, newTuple) {
6565
const newTupleList = [...tupleList];
6666

6767
for (let index = 0; index < newTupleList.length; index++) {
68-
if (newTupleList[index].get(n) === key) {
68+
if (newTupleList[index].get(n - 1) === key) {
6969
newTupleList[index] = newTuple;
7070
return newTupleList;
7171
}
@@ -78,9 +78,9 @@ function keysort(n, tupleList) {
7878
const newTupleList = [...tupleList];
7979

8080
return newTupleList.sort((a, b) => {
81-
if (a.get(n) < b.get(n)) {
81+
if (a.get(n - 1) < b.get(n - 1)) {
8282
return -1;
83-
} else if (a.get(n) > b.get(n)) {
83+
} else if (a.get(n - 1) > b.get(n - 1)) {
8484
return 1;
8585
}
8686

@@ -92,7 +92,7 @@ function keystore(key, n, tupleList, newTuple) {
9292
const newTupleList = [...tupleList];
9393

9494
for (let index = 0; index < newTupleList.length; index++) {
95-
if (newTupleList[index].get(n) === key) {
95+
if (newTupleList[index].get(n - 1) === key) {
9696
newTupleList[index] = newTuple;
9797
return newTupleList;
9898
}
@@ -106,7 +106,7 @@ function keydelete(key, n, tupleList) {
106106
let deleted = false;
107107

108108
for (let index = 0; index < tupleList.length; index++) {
109-
if (deleted === false && tupleList[index].get(n) === key) {
109+
if (deleted === false && tupleList[index].get(n - 1) === key) {
110110
deleted = true;
111111
} else {
112112
newTupleList.push(tupleList[index]);
@@ -121,7 +121,7 @@ function keytake(key, n, tupleList) {
121121

122122
if (result !== false) {
123123
return new ErlangTypes.Tuple(
124-
result.get(n),
124+
result.get(n - 1),
125125
result,
126126
keydelete(key, n, tupleList)
127127
);

0 commit comments

Comments
 (0)