How to use the asynciterator.TransformIterator.subclass function in asynciterator

To help you get started, we’ve selected a few asynciterator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LinkedDataFragments / Client.js / lib / triple-pattern-fragments / TurtleFragmentIterator.js View on Github external
// Convert Turtle into triples using the N3 parser
  this._parser = new N3.Parser({ documentURI: fragmentUrl });
  this._parser.parse({
    // Use dummy stream to capture `data` and `end` callbacks
    on: function (event, callback) {
      if (event === 'data') self._parseData = callback;
      else if (event === 'end') self._parseEnd = callback;
    },
  },
  // Process each triple and emit possible errors
  function (error, triple) {
    if (error) self.emit('error', error);
    else if (triple) self._processTriple(triple);
  });
}
TransformIterator.subclass(TurtleFragmentIterator);

// Sends a chunk of Turtle to the N3 parser to convert it to triples
TurtleFragmentIterator.prototype._transform = function (chunk, done) {
  this._parseData(chunk), done();
};

// Sends the given parsed triple to the data or metadata stream
TurtleFragmentIterator.prototype._processTriple = function (triple) {
  // This separation between data and metadata/controls is an approximation;
  // for a proper separation, use an RDF format with graph support (see TrigFragmentParser).
  if (triple.subject !== this._fragmentUrl && triple.predicate.indexOf(rdf.HYDRA) !== 0)
    this._push(triple);
  else
    this.metadataStream._push(triple);
};
github LinkedDataFragments / Client.js / lib / writers / SparqlResultWriter.js View on Github external
/*! @license MIT ©2014-2016 Miel Vander Sande, Ghent University - imec */
/* Serializing the output of a SparqlIterator */

var TransformIterator = require('asynciterator').TransformIterator;

function SparqlResultWriter(source) {
  TransformIterator.call(this, source);
  this._empty = true;
  this._variables = source.getProperty('variables') || [];
}
TransformIterator.subclass(SparqlResultWriter);

SparqlResultWriter.prototype._begin = function (done) {
  this._writeHead(this._variables.map(function (v) { return v.substring(1); }));
  done();
};

SparqlResultWriter.prototype._writeHead = function (variableNames) { };

SparqlResultWriter.prototype._transform = function (result, done) {
  if (typeof result === 'boolean')
    this._writeBoolean(result);
  else
    this._writeBindings(result);
  this._empty = false;
  done();
};
github LinkedDataFragments / Client.js / lib / sparql / SortIterator.js View on Github external
// Creates a new SortIterator with the given filter
function SortIterator(source, sort, options) {
  if (!(this instanceof SortIterator))
    return new SortIterator(source, sort, options);
  // Shift arguments if `sort` is omitted
  if (typeof sort !== 'function')
    options = sort, sort = null;
  TransformIterator.call(this, source, options);

  // The `window` parameter indicates the length of the sliding window to apply sorting
  var window = options && options.window;
  this._windowLength = isFinite(window) && window > 0 ? ~~window : Infinity;
  this._sort = sort || defaultSort;
  this._sorted = [];
}
TransformIterator.subclass(SortIterator);

// Reads the smallest item in the current sorting window
SortIterator.prototype._read = function (count, done) {
  var item, sorted = this._sorted, source = this._source, length = sorted.length;
  if (source) {
    // Try to read items until we reach the desired window length
    while (length !== this._windowLength && (item = source.read()) !== null) {
      // Insert the item in the sorted window (smallest last)
      var left = 0, right = length - 1, mid, order;
      while (left <= right) {
        order = this._sort(item, sorted[mid = (left + right) >> 1]);
        if      (order < 0) left  = mid + 1;
        else if (order > 0) right = mid - 1;
        else left = mid, right = -1;
      }
      sorted.splice(left, 0, item), length++;