Cursor.js 860 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. /**
  3. * This class is a simplified implementation of the cursors used in MongoDB for reading from an Array of documents.
  4. * @param {Array} items The array source of the data
  5. **/
  6. var klass = module.exports = function Cursor(items){
  7. if (!(items instanceof Array)) throw new Error("arg `items` must be an Array");
  8. this.cachedData = items.slice(0); // keep a copy
  9. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  10. proto.ok = function ok(){
  11. return this.cachedData.length > 0 || this.hasOwnProperty("curr");
  12. };
  13. proto.advance = function advance(){
  14. if (this.cachedData.length === 0){
  15. delete this.curr;
  16. return false;
  17. }
  18. this.curr = this.cachedData.shift();
  19. return this.curr;
  20. };
  21. proto.current = function current(){
  22. if (!this.hasOwnProperty("curr")) this.advance();
  23. return this.curr;
  24. };