update devDependencies, add jsdoc lint, fix lint issues
[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 class PathParameter extends null {
8 static #parameters = parameters;
9
10 /**
11 *
12 * @param {string} parameter name
13 * @returns {PathParameter} frozen parameter object
14 */
15 constructor(parameter) {
16 if (!parameter || typeof(parameter) !== 'string') {
17 throw new RangeError('parameter must be string');
18 }
19
20 if (PathParameter.#parameters.has(parameter)) {
21 return PathParameter.#parameters.get(parameter); // NOSONAR
22 }
23
24 const pathParameter = Object.create(PathParameter.prototype);
25 pathParameter.parameter = parameter;
26 Object.freeze(pathParameter);
27 PathParameter.#parameters.set(parameter, pathParameter);
28 return pathParameter; // NOSONAR
29 }
30
31 /**
32 * @returns {string} parameter string
33 */
34 toString() {
35 return this.parameter;
36 }
37
38 /**
39 * @returns {string} parameter string
40 */
41 toJSON() {
42 return this.parameter;
43 }
44
45 /**
46 * Clear the de-duplication table, for tests.
47 */
48 static _flush() {
49 PathParameter.#parameters.clear();
50 }
51 }
52
53 module.exports = PathParameter;