Coverage

100%
121
121
0

/home/bewest/src/sgvdata/index.js

100%
18
18
0
LineHitsSource
1
2
31var es = require('event-stream');
4
51var sync = {
6 text : require('./lib/text')( )
7, json : require('./lib/json')( )
8};
9
101function mapper(fn, strict) {
114 function map (item, next) {
124 var res = fn(item, strict);
134 next(null, res);
14 }
154 return es.map(map);
16}
17
181function format ( ) {
191 return mapper(sync.text.format);
20}
21
221function parse ( ) {
231 return es.pipeline(es.split('\n'), mapper(sync.text.parse), mapper(sync.json.echo));
24}
25
261function lint ( ) {
271 return mapper(sync.json.echo, {strict: true});
28}
29
301module.exports.sync = sync;
311module.exports.mapper = mapper;
321module.exports.format = format;
331module.exports.parse = parse;
341module.exports.lint = lint;
35

/home/bewest/src/sgvdata/lib/json.js

100%
9
9
0
LineHitsSource
1
21function echo (record, opts) {
35 var res = {
4 sgv: record.sgv.toString( )
5 , dateString: record.dateString || ''
6 , date: parseInt(record.date)
7 , device: record.device || ''
8 , direction: record.direction || ''
9 };
105 if (res.sgv && isFinite(res.date)) {
114 return res;
12 }
132 if (opts && opts.strict) { throw new Error(record); }
14}
15
16
171function configure ( ) {
184 return configure;
19}
20
211configure.echo = echo;
221module.exports = configure;
23

/home/bewest/src/sgvdata/lib/text.js

100%
19
19
0
LineHitsSource
1
21function parse (t) {
32 var sep = configure.opts.parse || configure.opts.sep;
42 var p = t.split(sep);
5
62 if (p.length < 4) { return; }
72 var rec = {
8 dateString: p[0]
9 , date: parseInt(p[1])
10 , sgv: p[2]
11 , direction: p[3]
12 , device: p[4] || ''
13 };
142 return rec;
15}
16
171function format (rec) {
182 var sep = configure.opts.format || configure.opts.sep;
192 var fields = [ rec.dateString
20 , rec.date
21 , rec.sgv
22 , rec.direction || ''
23 , rec.device || ''];
242 return fields.join(sep);
25}
26
271function configure (opts) {
281 if (opts) { configure.opts = opts; }
291 return configure;
30}
31
321var separate_text = /[\s,]/;
331var separate_format = '\t';
341configure.opts = { parse: separate_text, format: separate_format };
351configure.parse = parse;
361configure.format = format;
37
381module.exports = configure;
39

/home/bewest/src/sgvdata/tests/test_json.js

100%
20
20
0
LineHitsSource
1
21var should = require('should');
3
41describe('lib/json', function ( ) {
51 var fix = {
6 "sgv": "123",
7 "device": "test",
8 "direction": "",
9 "dateString": "2014-07-14T18:19:01-0700",
10 "date": 1405387141
11 };
12
131 var fixable = {
14 "sgv": 123,
15 "device": "test",
16 "direction": "",
17 "dateString": "2014-07-14T18:19:01-0700",
18 "date": "1405387141"
19 };
20
211 var broken = {
22 "sgv": "123",
23 "device": "test",
24 "direction": "",
25 "dateString": "2014-07-14T18:19:01-0700"
26 };
27
281 var json = require('../').sync.json;
291 it('should exist', function ( ) {
301 json.should.be.ok;
311 json.echo.should.be.ok;
32 });
33
341 it('echos records', function ( ) {
351 var result = json( ).echo(fix);
361 Object.keys(result).forEach(function (key) {
375 result[key].should.equal(fix[key]);
38 });
39
40 });
41
421 it('fixes records', function ( ) {
431 var result = json( ).echo(fixable);
441 Object.keys(result).forEach(function (key) {
455 result[key].should.equal(fix[key]);
46 });
47 });
48
491 it('has strict mode', function ( ) {
501 (function ( ) {
511 var result = json( ).echo(broken, {strict: true});
52 }).should.throw( );
53
54
55 });
56
57});
58
59

/home/bewest/src/sgvdata/tests/test_streams.js

100%
38
38
0
LineHitsSource
1
21var should = require('should');
31var es = require('event-stream');
4
51describe('sgvdata', function ( ) {
61 var fix = {
7 "sgv": "123",
8 "device": "test",
9 "direction": "",
10 "dateString": "2014-07-14T18:19:01-0700",
11 "date": 1405387141
12 };
13
141 var fixable = {
15 "sgv": 123,
16 "device": "test",
17 "direction": "",
18 "dateString": "2014-07-14T18:19:01-0700",
19 "date": "1405387141"
20 };
21
221 var tab = '2014-07-14T18:19:01-0700\t1405387141\t123\t\ttest';
231 var sgvdata = require('../');
24
25
261 it('should exist', function ( ) {
271 sgvdata.should.be.ok;
281 sgvdata.sync.should.be.ok;
291 sgvdata.lint.should.be.ok;
301 sgvdata.format.should.be.ok;
311 sgvdata.parse.should.be.ok;
32 });
33
341 function prove_known(result, fix) {
352 Object.keys(result).forEach(function (key) {
3610 result[key].should.equal(fix[key]);
37 });
38 }
39
401 it('should stream text to json', function (done) {
411 var input = es.readArray([tab]);
421 var proc = sgvdata.parse( )
431 function finish (err, results) {
441 results.length.should.equal(1);
451 prove_known(results[0], fix);
461 done( );
47 }
481 es.pipeline(input, proc, es.writeArray(finish));
49 });
50
511 it('should stream json to text', function (done) {
521 var input = es.readArray([fix]);
531 var proc = sgvdata.format( )
541 function finish (err, results) {
551 results[0].should.equal(tab);
561 done( );
57 }
581 es.pipeline(input, proc, es.writeArray(finish));
59 });
60
611 it('should stream valid json', function (done) {
621 var input = es.readArray([fixable]);
631 var proc = sgvdata.lint( )
641 function finish (err, results) {
651 prove_known(results[0], fix);
661 done( );
67 }
681 es.pipeline(input, proc, es.writeArray(finish));
69 });
70});
71

/home/bewest/src/sgvdata/tests/test_text.js

100%
17
17
0
LineHitsSource
1
21var should = require('should');
3
41describe('lib/text', function ( ) {
51 var fix = {
6 "sgv": "123",
7 "device": "test",
8 "direction": "",
9 "dateString": "2014-07-14T18:19:01-0700",
10 "date": 1405387141
11 };
121 var tab = '2014-07-14T18:19:01-0700\t1405387141\t123\t\ttest';
131 var text = require('../').sync.text;
141 it('should exist', function ( ) {
151 text.should.be.ok;
161 text.format.should.be.ok;
171 text.parse.should.be.ok;
181 text.opts.should.be.ok;
19 });
20
211 it('formats text', function ( ) {
221 var result = text.format(fix);
231 result.should.equal(tab);
24
25 });
26
271 it('parses text', function ( ) {
281 var result = text.parse(tab);
291 Object.keys(result).forEach(function (key) {
305 result[key].should.equal(fix[key]);
31 });
32
33 });
34
35});
36
37