Skip to content

Commit 01bdf02

Browse files
committed
added more :erlang.binary functions
1 parent a4774c6 commit 01bdf02

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
import erlang from './erlang';
2+
import proplists from './proplists';
3+
4+
function at(subject, pos) {
5+
return subject.charAt(pos);
6+
}
27

38
function copy(subject, n = 1) {
49
return subject.repeat(n);
510
}
611

12+
function first(subject) {
13+
//TODO: raise badarg on size 0
14+
return at(subject, 0);
15+
}
16+
17+
function last(subject) {
18+
//TODO: raise badarg on size 0
19+
return subject.slice(-1);
20+
}
21+
722
function list_to_bin(bytelist) {
823
return erlang.list_to_binary(bytelist);
924
}
1025

26+
//TODO: How to create a tuple on JS side from part/3?
27+
//function part(subject, posOrTuple, len=null) {
28+
// if (len == null) "/2 called" else "/3 called"
29+
//}
30+
31+
function part(subject, pos, len) {
32+
return subject.substr(pos, len);
33+
}
34+
35+
//TODO: Support more options
36+
//TODO: pattern cannot be list of strings
37+
function replace(subject, pattern, replacement, options=[]) {
38+
const opt_global = proplists.get_value(Symbol('global'), options);
39+
40+
var regex;
41+
if (opt_global.toString() != Symbol('undefined').toString()) {
42+
regex = new RegExp(pattern, 'g');
43+
} else {
44+
regex = new RegExp(pattern, '');
45+
}
46+
47+
return subject.replace(regex, replacement)
48+
}
49+
50+
//TODO: Support more options, global is implied
51+
//TODO: pattern cannot be list of strings
52+
function split(subject, pattern, options=[]) {
53+
return subject.split(pattern)
54+
}
55+
1156
export default {
57+
at,
1258
copy,
59+
first,
60+
last,
1361
list_to_bin,
62+
part,
63+
replace,
64+
split,
1465
};

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

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

4+
test('at', (t) => {
5+
let result = Core.binary.at('abc', 0);
6+
t.deepEqual(result, 'a');
7+
});
8+
49
test('copy', (t) => {
510
let result = Core.binary.copy('h', 3);
611
t.deepEqual(result, 'hhh');
@@ -9,7 +14,39 @@ test('copy', (t) => {
914
t.deepEqual(result, 'h');
1015
});
1116

17+
test('first', (t) => {
18+
let result = Core.binary.first('abc');
19+
t.deepEqual(result, 'a');
20+
});
21+
22+
test('last', (t) => {
23+
let result = Core.binary.last('abc');
24+
t.deepEqual(result, 'c');
25+
});
26+
1227
test('list_to_bin', (t) => {
1328
const result = Core.binary.list_to_bin([104, 101, 108, 108, 111]);
1429
t.deepEqual(result, 'hello');
1530
});
31+
32+
test('part', (t) => {
33+
let result = Core.binary.part('abcde', 1, 1);
34+
t.deepEqual(result, 'b');
35+
36+
result = Core.binary.part('abcde', 1, 3);
37+
t.deepEqual(result, 'bcd');
38+
});
39+
40+
test('replace', (t) => {
41+
let result = Core.binary.replace('abcb', 'b', 'c');
42+
t.deepEqual(result, 'accb');
43+
44+
//TODO: How to make a proplist here?
45+
//result = Core.binary.replace('abcb', 'b', 'c', [global: true]);
46+
//t.deepEqual(result, 'accc');
47+
});
48+
49+
test('split', (t) => {
50+
let result = Core.binary.split('abcd', 'b');
51+
t.deepEqual(result, ['a', 'cd']);
52+
});

0 commit comments

Comments
 (0)