Skip to content

Commit 6c3b64c

Browse files
author
Matt Zumwalt
committed
import from stream (incomplete)
refs #3
1 parent 8fb35f2 commit 6c3b64c

File tree

4 files changed

+57
-84
lines changed

4 files changed

+57
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
tmp
22
node_modules
3+
data.jawn

index.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
var level = require('level');
4-
var hypercore = require('hypercore');
5-
6-
module.exports = Jawn;
7-
8-
function Jawn() {}
9-
10-
Jawn.prototype.import = function (core, path) {
11-
12-
var ws = core.createWriteStream();
13-
14-
// read the file from disk and write it to the stream line by line
15-
var lines = fs.readFileSync(path)
16-
.toString()
17-
.split(/\r?\n/g)
18-
.filter(function (line) {
19-
return line !== '';
20-
});
21-
22-
lines.forEach(function (line) {
23-
ws.write(line);
24-
});
25-
26-
return ws;
27-
28-
};
1+
var level = require('level')
2+
var hypercore = require('hypercore')
3+
4+
module.exports = Jawn
5+
function Jawn () {
6+
var self = this
7+
self.db = level('data.jawn')
8+
self.core = hypercore(self.db)
9+
}
10+
11+
Jawn.prototype.import = function (stream) {
12+
var ws = this.core.createWriteStream()
13+
stream.on('data', function (line) {
14+
ws.write(line)
15+
})
16+
stream.on('end', function () {
17+
ws.end(function () {
18+
console.log(ws.id.toString('hex'))
19+
})
20+
})
21+
return ws
22+
}

test/import.js

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,29 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
var tape = require('tape');
4-
var level = require('level');
5-
var hypercore = require('hypercore');
6-
7-
var Jawn = require('../');
8-
9-
tape('test file ingest', function (t) {
10-
11-
var db = level('test.db');
12-
var core = hypercore(db);
13-
14-
var J = new Jawn();
15-
16-
var path = './test.csv';
17-
18-
var ws = J.import(core, path);
19-
20-
var filecontents = fs.readFileSync(path)
21-
.toString()
22-
.split('\n')
23-
.filter(function (line) {
24-
return line !== '';
25-
});
26-
27-
// get number of rows in test.csv
28-
var numrows = filecontents.length;
29-
30-
ws.end(function () {
31-
32-
var id = ws.id.toString('hex');
33-
34-
t.ok(ws.id, 'has id');
35-
36-
t.same(ws.blocks, numrows, 'there should be one row for each block');
37-
38-
// Now create a read stream with the same id and read the data
39-
var rs = core.createReadStream(id);
40-
41-
var feedcontents = [];
42-
43-
rs.on('data', function (data) {
44-
// gather feed contents
45-
feedcontents.push(data.toString());
46-
});
47-
48-
rs.on('end', function () {
49-
t.same(filecontents, feedcontents, 'contents of feed should match file contents');
50-
});
51-
52-
});
53-
54-
t.end();
55-
56-
});
1+
var test = require('tape')
2+
var Jawn = require('../')
3+
var fs = require('fs')
4+
5+
var j = new Jawn()
6+
7+
test('import csv to jawn', function (t) {
8+
var data = fs.createReadStream('./test/sample/sample.csv')
9+
var ws = j.import(data)
10+
var expected = [
11+
{'Type of Experience': 'Writing software in any programming language', 'Little/No Experience': 1, 'Some Experience': 5, 'Very Familiar': 4},
12+
{'Type of Experience': 'Frontend Web Development', 'Little/No Experience': 4, 'Some Experience': 3, 'Very Familiar': 3},
13+
{'Type of Experience': 'Server-side (backend) Web Development', 'Little/No Experience': 4, 'Some Experience': 4, 'Very Familiar': 2},
14+
{'Type of Experience': 'Using Git to track changes and share code (add, commit, push, pull)', 'Little/No Experience': 2, 'Some Experience': 5, 'Very Familiar': 3}
15+
]
16+
ws.on('end', function () {
17+
var feedId = ws.id.toString('hex')
18+
var rs = j.core.createReadStream(feedId)
19+
20+
rs.on('data', function (block) {
21+
t.same(block.toString(), expected.shift(), 'block matches imported line')
22+
})
23+
24+
var blocks = j.core.get(feedId).blocks
25+
26+
t.same(blocks, 4, 'correct number of blocks returned')
27+
t.end()
28+
})
29+
})

test/sample/sample.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Type of Experience,Little/No Experience,Some Experience,Very Familiar
2+
Writing software in any programming language,1,5,4
3+
Frontend Web Development,4,3,3
4+
Server-side (“backend”) Web Development,4,4,2
5+
"Using Git to track changes and share code (add, commit, push, pull)",2,5,3

0 commit comments

Comments
 (0)