Skip to content

Commit 6267f97

Browse files
committed
Clean up buffer_to and add tests
1 parent ea5b761 commit 6267f97

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

buffer_to.coffee.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55
R = require 'ramda'
66
7+
{add, compose, converge, curry, equals, identity, invoker, last, nth, pipe, replace, slice} = R
8+
79

810
## Helper functions
911

10-
buffer_to_string = R.invoker 1, 'toString'
12+
buffer_to_string = invoker 1, 'toString'
1113
12-
last_is_0 = R.pipe R.last, R.equals 0
14+
last_is_0 = pipe last, equals 0
1315
14-
strip_last_0 = R.when last_is_0, R.slice(0, -1)
16+
strip_last_0 = R.when last_is_0, slice(0, -1)
1517
16-
buffer_to_ascii = R.pipe strip_last_0, buffer_to_string 'ascii'
18+
buffer_to_ascii = pipe strip_last_0, buffer_to_string 'ascii'
1719
18-
swap_slashes = R.replace /\\/g, '/'
20+
swap_slashes = replace /\\/g, '/'
1921
2022

2123
## Exports
@@ -24,21 +26,22 @@
2426
ascii: buffer_to_ascii
2527
2628
#char: R.compose String.fromCharCode, R.nth
27-
char: R.curry (index, buffer)->
28-
String.fromCharCode R.nth index, buffer
29+
char: curry (index, buffer)->
30+
String.fromCharCode nth index, buffer
2931
30-
file_path: R.pipe buffer_to_ascii, swap_slashes
32+
file_path: pipe buffer_to_ascii, swap_slashes
3133
32-
float: R.invoker 1, 'readFloatLE'
34+
float: invoker 1, 'readFloatLE'
3335
34-
int16: R.invoker 1, 'readInt16LE'
36+
int16: invoker 1, 'readInt16LE'
3537
36-
label: (index)->
37-
#R.compose buffer_to.ascii, R.apply(R.slice, R.juxt([R.identity, R.add(4)])(index))
38-
R.compose buffer_to_ascii, R.slice(index, index + 4)
38+
label: curry (index, buffer)->
39+
compose(buffer_to_ascii, slice(index, index + 4))(buffer)
40+
# label: pipe(converge(slice, [identity, add(4)]), buffer_to_ascii)
41+
# label: compose(buffer_to_ascii, converge(slice, [identity, add(4)]))
3942
40-
uint8: R.invoker 1, 'readUInt8'
43+
uint8: invoker 1, 'readUInt8'
4144
42-
uint16: R.invoker 1, 'readUInt16LE'
45+
uint16: invoker 1, 'readUInt16LE'
4346
44-
uint32: R.invoker 1, 'readUInt32LE'
47+
uint32: invoker 1, 'readUInt32LE'

test/buffer_to.coffee.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Test buffer-to
2+
3+
## Library imports
4+
5+
tape = require 'tape'
6+
7+
8+
## Relative imports
9+
10+
buffer_to = require '../buffer_to'
11+
12+
13+
## Run tests
14+
15+
tape 'buffer_to.ascii', (t)->
16+
t.plan 1
17+
actual = buffer_to.ascii(new Buffer [0x31, 0x41, 0x61, 0x0])
18+
t.deepEqual actual, '1Aa'
19+
20+
tape 'buffer_to.int16 -1', (t)->
21+
t.plan 1
22+
actual = buffer_to.int16(0, new Buffer [0xFF, 0xFF])
23+
t.deepEqual actual, -1
24+
25+
tape 'buffer_to.label', (t)->
26+
t.plan 1
27+
actual = buffer_to.label(0, new Buffer [0x47, 0x52, 0x55, 0x50])
28+
t.deepEqual actual, 'GRUP'
29+
30+
tape 'buffer_to.uint8', (t)->
31+
t.plan 1
32+
actual = buffer_to.uint8(0, new Buffer [9])
33+
t.deepEqual actual, 9
34+
35+
tape 'buffer_to.uint16', (t)->
36+
t.plan 1
37+
actual = buffer_to.uint16(0, new Buffer [1, 2])
38+
t.deepEqual actual, 513

0 commit comments

Comments
 (0)