/** * A character buffer class that consumes characters and tracks previous and next characters * @class CharBuf * @param {string} str The string to use as the raw data for the buffer */ var CharBuf = module.exports = function CharBuf(str) { if(!(this instanceof CharBuf)) return new CharBuf(str); this.str = str || ''; this.charCur = null; this.idx = -1; } /** * Consumes a character from the string and increments the index * @function CharBuf#consume * @return {string} The character consumed * * @TODO Write unit test */ CharBuf.prototype.consume = function () { return this.charCur = this.str[++this.idx]; }; /** * @property {Array} The previous characters in reverse order (0 = the last character, 1 = the next to last ...) * @name CharBuf#charPre * * @TODO Write unit test */ Object.defineProperty(CharBuf.prototype, 'charPre', { get: function(){ return this.str.substr(0,this.idx).split('').reverse(); } }); /** * @property {Array} The next characters (0 = the next character, 1 = the next-next character ...) * @name CharBuf#charNxt * * @TODO Write unit test */ Object.defineProperty(CharBuf.prototype, 'charNxt', { get: function(){ return this.str.substr(this.idx+1).split(''); } });