From c19f9a34ec7ae311e025118e51705842e31e7066 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Tue, 18 Jul 2023 13:56:28 -0700 Subject: [PATCH] minor cleanup of router parameter class --- lib/router/path-parameter.js | 26 +++++++++++++------ test/lib/router/path-parameter.js | 42 +++++++++++++++---------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/lib/router/path-parameter.js b/lib/router/path-parameter.js index 3c6f1cb..8140594 100644 --- a/lib/router/path-parameter.js +++ b/lib/router/path-parameter.js @@ -7,34 +7,44 @@ const parameters = new Map(); * @property {String} parameter */ class PathParameter extends null { + static #parameters = parameters; + constructor(parameter) { if (!parameter || typeof(parameter) !== 'string') { throw new RangeError('parameter must be string'); } - if (parameters.has(parameter)) { - return parameters.get(parameter); + + if (PathParameter.#parameters.has(parameter)) { + return PathParameter.#parameters.get(parameter); // NOSONAR } + const pathParameter = Object.create(PathParameter.prototype); pathParameter.parameter = parameter; - parameters.set(parameter, pathParameter); Object.freeze(pathParameter); - return pathParameter; + PathParameter.#parameters.set(parameter, pathParameter); + return pathParameter; // NOSONAR } /** * @returns {String} */ toString() { - return `{${this.constructor.name} ${this.parameter}}`; + return this.parameter; + } + + /** + * @returns {String} + */ + toJSON() { + return this.parameter; } /** * Clear the de-duplication table, for tests. */ static _flush() { - this.parameters.clear(); + PathParameter.#parameters.clear(); } } -PathParameter.parameters = parameters; -module.exports = PathParameter; \ No newline at end of file +module.exports = PathParameter; diff --git a/test/lib/router/path-parameter.js b/test/lib/router/path-parameter.js index 9b2fb1e..d4c1c84 100644 --- a/test/lib/router/path-parameter.js +++ b/test/lib/router/path-parameter.js @@ -4,48 +4,46 @@ const assert = require('assert'); const PathParameter = require('../../../lib/router/path-parameter'); -const noExpectedException = 'did not receive expected exception'; describe('PathParameter', function () { beforeEach(function () { PathParameter._flush(); }); + it('requires a parameter', function () { - try { - new PathParameter(); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.throws(() => new PathParameter(), RangeError); }); + it('requires parameter be a string', function () { - try { - new PathParameter({}); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.throws(() => new PathParameter({}), RangeError); }); + it('creates a parameter object', function () { const p = new PathParameter('foo'); assert(p instanceof PathParameter); assert.strictEqual(p.parameter, 'foo'); }); + it('duplicate parameters are the same object', function () { const p1 = new PathParameter('foo'); const p2 = new PathParameter('foo'); assert.strictEqual(p1, p2); + assert(Object.is(p1, p2)); }); + it('shows itself', function () { const p = new PathParameter('foo'); - assert(p.toString().includes('foo')); + assert.strictEqual(`${p}`, 'foo'); }); + + it('serializes', function () { + const p = new PathParameter(':foo'); + const result = JSON.stringify(p); + assert.strictEqual(result, '":foo"'); + }); + it('parameters are immutable', function () { const p = new PathParameter('foo'); - try { - p[PathParameter.kPathParameter] = 'bar'; - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof TypeError, noExpectedException); - } - }); -}); // PathParameter \ No newline at end of file + assert.throws(() => p.parameter = 'bar', TypeError); + }); + +}); // PathParameter -- 2.43.2