minor cleanup of router parameter class
[squeep-api-dingus] / lib / router / path-parameter.js
1 'use strict';
2
3 const parameters = new Map();
4 /**
5 * De-duplicating factory of minimal-objects to track the named-parameter parts of path definitions.
6 *
7 * @property {String} parameter
8 */
9 class PathParameter extends null {
10 static #parameters = parameters;
11
12 constructor(parameter) {
13 if (!parameter || typeof(parameter) !== 'string') {
14 throw new RangeError('parameter must be string');
15 }
16
17 if (PathParameter.#parameters.has(parameter)) {
18 return PathParameter.#parameters.get(parameter); // NOSONAR
19 }
20
21 const pathParameter = Object.create(PathParameter.prototype);
22 pathParameter.parameter = parameter;
23 Object.freeze(pathParameter);
24 PathParameter.#parameters.set(parameter, pathParameter);
25 return pathParameter; // NOSONAR
26 }
27
28 /**
29 * @returns {String}
30 */
31 toString() {
32 return this.parameter;
33 }
34
35 /**
36 * @returns {String}
37 */
38 toJSON() {
39 return this.parameter;
40 }
41
42 /**
43 * Clear the de-duplication table, for tests.
44 */
45 static _flush() {
46 PathParameter.#parameters.clear();
47 }
48 }
49
50 module.exports = PathParameter;