From a321d0151f2ca02106e5c1d1fcdd7d08cce328c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Mon, 27 Jun 2022 20:52:02 +0200 Subject: [PATCH 1/2] replace depricated istanbul \w c8 --- package.json | 6 +++--- test/index.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 94b59d1..8851ce4 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,11 @@ "description": "Take an array of token and produce a more useful API to give to a parser", "dependencies": {}, "devDependencies": { - "istanbul": "*" + "c8": "*" }, "scripts": { - "test": "node test && npm run coverage", - "coverage": "istanbul cover test" + "test": "node test/index.js && npm run coverage", + "coverage": "c8 node test/index.js" }, "repository": { "type": "git", diff --git a/test/index.js b/test/index.js index 8a127a2..60075cd 100644 --- a/test/index.js +++ b/test/index.js @@ -1,7 +1,7 @@ 'use strict'; var assert = require('assert'); -var TokenStream = require('../'); +var TokenStream = require('../index.js'); assert.throws(function () { new TokenStream('foo,bar'); From c2901f3d3a618a461bb62c07bc2dee5cd7d3793d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Mon, 27 Jun 2022 20:52:11 +0200 Subject: [PATCH 2/2] classify --- index.js | 59 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 70641bb..b7daec2 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,41 @@ 'use strict'; -module.exports = TokenStream; -function TokenStream(tokens) { - if (!Array.isArray(tokens)) { - throw new TypeError('tokens must be passed to TokenStream as an array.'); +/** @template T */ +module.exports = class TokenStream { + #tokens + + /** @param {T[]} tokens */ + constructor (tokens) { + if (!Array.isArray(tokens)) { + throw new TypeError('tokens must be passed to TokenStream as an array.'); + } + this.#tokens = tokens; } - this._tokens = tokens; -} -TokenStream.prototype.lookahead = function (index) { - if (this._tokens.length <= index) { - throw new Error('Cannot read past the end of a stream'); + + /** @param {number} index */ + lookahead (index) { + if (this.#tokens.length <= index) { + throw new Error('Cannot read past the end of a stream'); + } + return this.#tokens[index]; + } + + peek () { + if (this.#tokens.length === 0) { + throw new Error('Cannot read past the end of a stream'); + } + return this.#tokens[0]; } - return this._tokens[index]; -}; -TokenStream.prototype.peek = function () { - if (this._tokens.length === 0) { - throw new Error('Cannot read past the end of a stream'); + + advance () { + if (this.#tokens.length === 0) { + throw new Error('Cannot read past the end of a stream'); + } + return this.#tokens.shift(); } - return this._tokens[0]; -}; -TokenStream.prototype.advance = function () { - if (this._tokens.length === 0) { - throw new Error('Cannot read past the end of a stream'); + + /** @param {T} token */ + defer (token) { + this.#tokens.unshift(token); } - return this._tokens.shift(); -}; -TokenStream.prototype.defer = function (token) { - this._tokens.unshift(token); -}; +}