Code coverage report for ac/index.js

Statements: 100% (27 / 27)      Branches: 100% (0 / 0)      Functions: 100% (9 / 9)      Lines: 100% (27 / 27)      Ignored: none     

All files » ac/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551 1 1 1   1 1       1   1       1 1   235886     1 1 1     1 1 1   235886       1       1 1 1 1 1   4     1         1  
var ac = {};
var fs = require('fs');
var level = require('level');
var db = level(__dirname + '/db');
 
ac.auto = function() {
    return 'autoc...';
};
 
// Import the list of words from words.txt into LevelDB
ac.import = function(callback) {
  // read file and split into an array of lines
  var lines = fs.readFileSync(__dirname + '/words2.txt', 'utf8')
    .split('\n');
 
    // uses batch chained: https://github.com/rvagg/node-levelup#batch_chained
    var batch = db.batch();
    lines.forEach(function (word) {
      // if(word.length > 0){
        batch.put(word, 0); // number of times word was searched for
      // }
    });
    batch.write();
    var words = 'imported';
    callback(null, words);
};
 
ac.count = function (callback) {
  var count = 0;
  db.createReadStream()
    .on('data', function(data){
      count++;
    })
    .on('end', function(){
      // console.log(' - - - -',count);
      callback(null, count);
    }); // no error handling is *deliberate*
};
 
ac.findWords = function(word, callback) {
  var words = [];
  var key = word.trim();
  var i = 0;
  db.createReadStream({ start: key, end: key + '\xff' })
    .on('data', function (data) {
      words.push(data);
    })
    .on('end', function () {
      callback(null, words);
    });
};
 
 
module.exports = ac;