var Transform = require('stream').Transform,
    util = require('util');


function MemoryStream(options) {
  options = options || {};
  Transform.call(this, options);
  // key contains the absissis variable
  this.key = null;
  // accumulator that will be sent to a writable stream
  this.memory = {
    xcoord: {},
    percentile: []
  };
}

util.inherits(MemoryStream, Transform);

MemoryStream.prototype._write = function _write(chunk, encoding, callback) {
  try {
    this._convert(JSON.parse(chunk));
    callback();
  } catch (e) {
    callback(e);
  }
};
MemoryStream.prototype._convert = function _convert(data) {
  var self = this,
      graph = {};
  if (!this.key) {
    this._getkey(data);
  }
  this.memory.xcoord[data[this.key]] = {
    l: data.l,
    m: data.m,
    s: data.s
  };
  Object.keys(data).filter(function (key) {
      return ['l', 'm', 's', 'stdev'].indexOf(key) < 0;
  }).forEach(function (key) {
    graph[key === self.key ? 'xcoord' : key] = data[key];
  });
  this.memory.percentile.push(graph);
};
MemoryStream.prototype._getkey = function _getkey(data) {
  var values = ['age', 'day', 'month', 'length', 'height'],
      keys = Object.keys(data);
  this.key = keys.reduce(function (accum, curr) {
    if (values.indexOf[curr] >= 0) {
      return curr;
    }
    return accum;
  });
};
MemoryStream.prototype.end = function end(buf) {
  this.push(JSON.stringify(this.memory));
  Transform.prototype.end.call(this, buf);
};

module.exports = function (options) { return new MemoryStream(options); };
module.exports.MemoryStream = MemoryStream;

@@ -218,12 +218,19 @@
 y = 
-null
+options.key
 ;%0A  
@@ -297,24 +297,96 @@
 .memory = %7B%0A
+    meta: %7B%0A      measure: options.measure,%0A      by: options.by%0A    %7D,%0A
     xcoord: 
@@ -737,55 +737,8 @@
 %7B%7D;%0A
-  if (!this.key) %7B%0A    this._getkey(data);%0A  %7D%0A
   th
@@ -1017,16 +1017,16 @@
 ;%0A  %7D);%0A
+
   this.m
@@ -1062,297 +1062,8 @@
 %0A%7D;%0A
-MemoryStream.prototype._getkey = function _getkey(data) %7B%0A  var values = %5B'age', 'day', 'month', 'length', 'height'%5D,%0A      keys = Object.keys(data);%0A  this.key = keys.reduce(function (accum, curr) %7B%0A    if (values.indexOf%5Bcurr%5D %3E= 0) %7B%0A      return curr;%0A    %7D%0A    return accum;%0A  %7D);%0A%7D;%0A
 Memo

Remove key method and accept meta info in options.