caprese
Version:
Capped Log for Node.js
141 lines (122 loc) • 3.32 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var Query;
Query = (function() {
Query.prototype._limit = 0;
Query.prototype._order = 'asc';
Query.prototype._type = null;
Query.prototype._date = null;
function Query(log) {
this.log = log;
}
Query.prototype.asc = function() {
this._order = 'asc';
return this;
};
Query.prototype.desc = function() {
this._order = 'desc';
return this;
};
Query.prototype.filter = function(condition) {
if ((condition != null ? condition.type : void 0) != null) {
this._type = condition.type;
} else if ((condition != null ? condition.date : void 0) != null) {
this._date = condition.date.$gt;
}
return this;
};
Query.prototype.go = function(callback) {
var cb, cd, filter, index, item, re, _fn, _i, _len;
filter = this.log.index.slice(0);
if (this._type != null) {
filter = filter.filter((function(_this) {
return function(item) {
return item.type === _this._type;
};
})(this));
}
if (this._date != null) {
filter = filter.filter((function(_this) {
return function(item) {
return item.date > _this._date;
};
})(this));
}
if (this._order === 'desc') {
filter.reverse();
}
if (this._limit > 0) {
filter.splice(this._limit, filter.length - this._limit);
}
cd = filter.length;
cb = false;
re = [];
if (cd === 0) {
return callback(null, re);
}
_fn = (function(_this) {
return function(item, index) {
if (item.length === 0) {
re[index] = {
type: item.type,
message: ''
};
if (--cd === 0) {
cb = true;
return callback(null, re);
}
} else {
return _this.log.read(item.offset, item.length, function(err, buffer) {
if (cb) {
return;
}
if (err) {
cb = true;
return callback(err);
}
re[index] = {
type: item.type,
date: item.date,
message: buffer.toString()
};
if (--cd === 0) {
cb = true;
return callback(null, re);
}
});
}
};
})(this);
for (index = _i = 0, _len = filter.length; _i < _len; index = ++_i) {
item = filter[index];
if (cb) {
break;
}
_fn(item, index);
}
return this;
};
Query.prototype.limit = function(count) {
this._limit = count;
return this;
};
Query.prototype.newer = function(date) {
return this.filter({
date: {
$gt: date
}
});
};
Query.prototype.top = function(count) {
return this.limit(count);
};
Query.prototype.toArray = function(callback) {
return this.go(callback);
};
Query.prototype.where = function(condition) {
return this.filter(condition);
};
return Query;
})();
module.exports = Query;
}).call(this);