Skip to content

Commit 1c2b3a2

Browse files
committed
finalize list,proplists,binary additions
1 parent fcb50cd commit 1c2b3a2

File tree

8 files changed

+66
-35
lines changed

8 files changed

+66
-35
lines changed

src/javascript/lib/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import binary from './core/erlang_compat/binary';
1212
import unicode from './core/erlang_compat/unicode';
1313
import Store from './core/store';
1414
import math from './core/erlang_compat/math';
15+
import proplists from './core/erlang_compat/proplists';
1516

1617
class Integer {}
1718
class Float {}
@@ -54,4 +55,5 @@ export default {
5455
unicode,
5556
elixir_config,
5657
math,
58+
proplists
5759
};

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,34 @@ function list_to_bin(bytelist) {
2424
}
2525

2626
function part(subject, posOrTuple, len=null) {
27-
if (len === null) {
28-
var pos;
29-
[pos, len] = posOrTuple.values;
30-
return subject.substr(pos, len);
31-
} else {
32-
return subject.substr(posOrTuple, len);
33-
}
27+
if (len === null) {
28+
var pos;
29+
[pos, len] = posOrTuple.values;
30+
return subject.substr(pos, len);
31+
} else {
32+
return subject.substr(posOrTuple, len);
33+
}
3434
}
3535

3636
//TODO: Support more options
3737
//TODO: pattern cannot be list of strings
3838
function replace(subject, pattern, replacement, options=[]) {
39-
const opt_global = proplists.get_value(Symbol.for('global'), options);
39+
const opt_global = proplists.get_value(Symbol.for('global'), options);
4040

41-
var regex;
42-
if (opt_global !== Symbol.for('undefined')) {
43-
regex = new RegExp(pattern, 'g');
44-
} else {
45-
regex = new RegExp(pattern, '');
46-
}
47-
48-
return subject.replace(regex, replacement)
41+
var regex;
42+
if (opt_global !== Symbol.for('undefined')) {
43+
regex = new RegExp(pattern, 'g');
44+
} else {
45+
regex = new RegExp(pattern, '');
46+
}
47+
48+
return subject.replace(regex, replacement);
4949
}
5050

5151
//TODO: Support more options, global is implied
5252
//TODO: pattern cannot be list of strings
5353
function split(subject, pattern, options=[]) {
54-
return subject.split(pattern)
54+
return subject.split(pattern);
5555
}
5656

5757
export default {
@@ -63,4 +63,4 @@ export default {
6363
part,
6464
replace,
6565
split,
66-
};
66+
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import ErlangTypes from 'erlang-types';
12
import lists from './lists';
23

34
function get_value(key, list, defaultv = Symbol.for('undefined')) {
45
const tuple = lists.keyfind(key, 1, list);
56
if (tuple) {
67
const [, value] = tuple.values;
78
return value;
9+
} else if (lists.member(key, list)) {
10+
return true;
811
}
912
return defaultv;
1013
}

src/javascript/lib/core/functions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ function map_to_object(map, options = []) {
101101
if (opt_keys === Symbol.for('string') && typeof key === 'number') {
102102
key = key.toString();
103103
} else if (
104-
(opt_keys === Symbol.for('string') || opt_symbols !== Symbol.for('undefined'))
104+
(opt_keys === Symbol.for('string') || opt_symbols !== Symbol.for('undefined'))
105105
&& typeof key === 'symbol'
106106
) {
107107
key = erlang.atom_to_binary(key);
108108
}
109109

110110
if (value instanceof Map) {
111111
object[key] = map_to_object(value, options);
112-
} else if (opt_symbols !== Symbol.for('undefined') && typeof value === 'symbol') {
112+
} else if (opt_symbols !== Symbol.for('undefined') && typeof value === 'symbol') {
113113
object[key] = erlang.atom_to_binary(value);
114114
} else {
115115
object[key] = value;
@@ -171,4 +171,4 @@ export default {
171171
trampoline,
172172
Recurse,
173173
split_at,
174-
};
174+
};

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ import test from 'ava';
22
import Core from '../../../lib/core';
33

44
test('at/1', (t) => {
5-
let result = Core.binary.at('abc', 0);
5+
const result = Core.binary.at('abc', 0);
66
t.deepEqual(result, 'a');
77
});
88

99
test('copy/1', (t) => {
10-
let result = Core.binary.copy('h');
10+
const result = Core.binary.copy('h');
1111
t.deepEqual(result, 'h');
1212
});
1313

1414
test('copy/2', (t) => {
15-
let result = Core.binary.copy('h', 3);
15+
const result = Core.binary.copy('h', 3);
1616
t.deepEqual(result, 'hhh');
1717
});
1818

1919
test('first/1', (t) => {
20-
let result = Core.binary.first('abc');
20+
const result = Core.binary.first('abc');
2121
t.deepEqual(result, 'a');
2222
});
2323

2424
test('last/1', (t) => {
25-
let result = Core.binary.last('abc');
25+
const result = Core.binary.last('abc');
2626
t.deepEqual(result, 'c');
2727
});
2828

@@ -32,11 +32,11 @@ test('list_to_bin/1', (t) => {
3232
});
3333

3434
test('part/2', (t) => {
35-
let posLen = new Core.Tuple(1, 1)
35+
let posLen = new Core.Tuple(1, 1);
3636
let result = Core.binary.part('abcde', posLen);
3737
t.deepEqual(result, 'b');
3838

39-
posLen = new Core.Tuple(1, 3)
39+
posLen = new Core.Tuple(1, 3);
4040
result = Core.binary.part('abcde', posLen);
4141
t.deepEqual(result, 'bcd');
4242
});
@@ -50,16 +50,16 @@ test('part/3', (t) => {
5050
});
5151

5252
test('replace/3', (t) => {
53-
let result = Core.binary.replace('abcb', 'b', 'c');
53+
const result = Core.binary.replace('abcb', 'b', 'c');
5454
t.deepEqual(result, 'accb');
5555
});
5656

5757
test('replace/4', (t) => {
58-
let result = Core.binary.replace('abcb', 'b', 'c', [new Core.Tuple(Symbol.for('global'), true)]);
58+
const result = Core.binary.replace('abcb', 'b', 'c', [new Core.Tuple(Symbol.for('global'), true)]);
5959
t.deepEqual(result, 'accc');
6060
});
6161

6262
test('split/2', (t) => {
63-
let result = Core.binary.split('abcd', 'b');
63+
const result = Core.binary.split('abcd', 'b');
6464
t.deepEqual(result, ['a', 'cd']);
65-
});
65+
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ test('keyfind/3', (t) => {
3636
let result = Core.lists.keyfind('abc', 1, ['abc']);
3737
t.deepEqual(result, false);
3838

39-
result = Core.lists.keyfind('abc', 1, [{'abc'}]);
40-
t.deepEqual(result, true);
39+
result = Core.lists.keyfind('abc', 1, [new Core.Tuple('abc')]);
40+
t.deepEqual(result, new Core.Tuple('abc'));
4141
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('get_value/2', (t) => {
5+
let result = Core.proplists.get_value('abc', [new Core.Tuple('abc', '123')]);
6+
t.deepEqual(result, '123');
7+
8+
result = Core.proplists.get_value('abc', ['abc']);
9+
t.deepEqual(result, true);
10+
11+
result = Core.proplists.get_value('abcd', ['abc']);
12+
t.deepEqual(result, Symbol.for('undefined'));
13+
});
14+
15+
test('get_value/3', (t) => {
16+
let result = Core.proplists.get_value('abc', [new Core.Tuple('abc', '123')], "xyz");
17+
t.deepEqual(result, '123');
18+
19+
result = Core.proplists.get_value('abcd', [new Core.Tuple('abc', '123')], "xyz");
20+
t.deepEqual(result, "xyz");
21+
});
22+
23+
test('is_defined/2', (t) => {
24+
let result = Core.binary.at('abc', 0);
25+
t.deepEqual(result, 'a');
26+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ test('map_to_object/2', (t) => {
4747
result = Functions.map_to_object(map, []);
4848
4949
t.deepEqual(result, { s_key: 'value', s_anotherKey: 'value2' });
50-
});*/
50+
});*/

0 commit comments

Comments
 (0)