Code coverage report for levelgraph-n3/index.js

Statements: 95.74% (45 / 47)      Branches: 66.67% (8 / 12)      Functions: 100% (10 / 10)      Lines: 95.74% (45 / 47)      Ignored: none     

All files » levelgraph-n3/ » 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98  1     1   13       13   13   13 7     13   13 5   5 5   5     13 6     6 6 6   6   6     13 2   2 2     2   2 2     2     13 1   1 1   1 1     1   1           13     1 13 5       5     5   5   5       1  
 
var n3 = require("n3")
  , concat = require("concat-stream");
 
function levelgraphN3(db) {
  
  Iif (db.n3) {
    return db;
  }
 
  var graphdb = Object.create(db);
 
  graphdb.n3 = {};
 
  graphdb.n3.getStream = function(pattern, options) {
    return graphdb.getStream(pattern, options).pipe(new n3.StreamWriter());
  };
 
  graphdb.n3.get = wrapCallback('getStream');
 
  graphdb.n3.put = function(data, done) {
    var stream = this.putStream();
    
    stream.on("close", done)
    stream.end(data)
 
    return this;
  };
 
  graphdb.n3.putStream = function() {
    var parser = new n3.StreamParser()
      , putStream = graphdb.putStream();
 
    putStream.on("close", parser.emit.bind(parser, "close"));
    putStream.on("finish", parser.emit.bind(parser, "finish"));
    putStream.on("error", parser.emit.bind(parser, "error"));
 
    parser.pipe(putStream);
 
    return parser;
  };
 
  graphdb.searchStream = function(conditions, options) {
    var stream;
 
    Eif (options && options.n3) {
      options.materialized = options.n3;
    }
 
    stream = db.searchStream(conditions, options);
 
    Eif (options && options.n3) {
      stream = stream.pipe(new n3.StreamWriter());
    }
 
    return stream;
  };
 
  graphdb.search = function(conditions, options, callback) {
    var stream;
 
    Eif (options.n3) {
      stream = this.searchStream(conditions, options)
 
      stream.pipe(concat({ encoding: 'string' }, function(result) {
        callback(null, result);
      }));
 
      stream.on("error", callback);
 
      return this;
    }
 
    return db.search(conditions, options, callback);
  };
 
  return graphdb;
}
 
function wrapCallback(method) {
  return function() {
    var args = Array.prototype.slice.call(arguments, 0)
      , callback = args.pop()
      , stream = null
      , wrapped = function(result) {
                    callback(null, result);
                  };
 
    stream = this[method].apply(this, args);
 
    stream.on("error", callback);
 
    stream.pipe(concat({ encoding: 'string' }, wrapped));
  };
}
 
module.exports = levelgraphN3;