all files / lib/stream/ bufferedstream.js

100% Statements 17/17
100% Branches 2/2
100% Functions 6/6
100% Lines 17/17
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                              24× 24×       3043×       19×       15682×   15682× 10335×     15682×     24×      
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
 module.exports = (function() {
     
     'use strict';
     
     var stream = require('./stream.js');
     
/**
      * Buffered stream class
      */
     function BufferedStream(source) {
         this.source = source;
         this.cache = {};
     }
     
     BufferedStream.prototype = stream();
     
     BufferedStream.prototype.location = function(index) {
         return this.source.location(index);
     };
     
     // BufferedStream 'a => unit -> boolean
     BufferedStream.prototype.endOfStream = function(index) {
         return this.source.endOfStream(index);
     };
     
     // override, BufferedStream 'a => number -> Try 'a
     BufferedStream.prototype.get = function(index) {
         var self = this;
 
         if (!self.cache[index]) {
             self.cache[index] = self.source.get(index);
         }
         
         return self.cache[index];
     };
     
     return function(source) {
         return new BufferedStream(source);
     };
     
 }());