Merge branch 'v1.2-dev' as v1.2.6
[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 constructor(parameter) {
11 if (!parameter || typeof(parameter) !== 'string') {
12 throw new RangeError('parameter must be string');
13 }
14 if (parameters.has(parameter)) {
15 return parameters.get(parameter);
16 }
17 const pathParameter = Object.create(PathParameter.prototype);
18 pathParameter.parameter = parameter;
19 parameters.set(parameter, pathParameter);
20 Object.freeze(pathParameter);
21 return pathParameter;
22 }
23
24 /**
25 * @returns {String}
26 */
27 toString() {
28 return `{${this.constructor.name} ${this.parameter}}`;
29 }
30
31 /**
32 * Clear the de-duplication table, for tests.
33 */
34 static _flush() {
35 this.parameters.clear();
36 }
37 }
38 PathParameter.parameters = parameters;
39
40 module.exports = PathParameter;