Skip to content

Commit 8fb35f2

Browse files
wtkm7Matt Zumwalt
authored andcommitted
import a file
1 parent 6f73359 commit 8fb35f2

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
module.exports = Jawn
2-
function Jawn () {}
3-
Jawn.prototype.import = function (data) {
4-
return 'Hello World'
5-
}
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+
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
},
2222
"homepage": "https://github.com/CfABrigadePhiladelphia/jawn",
2323
"dependencies": {
24-
"hypercore": "^1.6.2"
24+
"hypercore": "~1.8.0",
25+
"fs": "0.0.2",
26+
"path": "~0.12.7",
27+
"tape": "~4.5.1",
28+
"level": "~1.4.0"
2529
},
2630
"devDependencies": {
2731
"standard": "^6.0.5",

test.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
c1,c2,c3,c4,c5
2+
c6,c7,c8,c9,c10
3+
c7,c8,c9,c10,c11
4+
new line

test/import.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
});

0 commit comments

Comments
 (0)