X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Frouter%2Fpath-parameter.js;fp=lib%2Frouter%2Fpath-parameter.js;h=585aaec7500a3ccf3fd6c9529fdb288f817f14e8;hb=9fe1e9b22b8e753c44dec77d1dee3d0061b2e991;hp=0000000000000000000000000000000000000000;hpb=91197ffbd90707333da06a714aaec0b8b8143577;p=squeep-api-dingus diff --git a/lib/router/path-parameter.js b/lib/router/path-parameter.js new file mode 100644 index 0000000..585aaec --- /dev/null +++ b/lib/router/path-parameter.js @@ -0,0 +1,49 @@ +'use strict'; + +const kPathParameter = Symbol('kSqueepDingusRouterPathParameter'); +const parameters = new Map(); +/** + * De-duplicating factory of minimal-objects to track the named-parameter parts of path definitions. + * + * @property {String} parameter + */ +class PathParameter extends null { + constructor(parameter) { + if (!parameter || typeof(parameter) !== 'string') { + throw new RangeError('parameter must be string'); + } + if (parameters.has(parameter)) { + return parameters.get(parameter); + } + const pathParameter = Object.create(PathParameter.prototype); + pathParameter[kPathParameter] = parameter; // eslint-disable-line security/detect-object-injection + parameters.set(parameter, pathParameter); + Object.freeze(pathParameter); + return pathParameter; + } + + /** + * Return the parameter name. + */ + get parameter() { + return this[kPathParameter];// eslint-disable-line security/detect-object-injection + } + + /** + * @returns {String} + */ + toString() { + return `{${this.constructor.name} ${this.parameter}}`; // eslint-disable-line security/detect-object-injection + } + + /** + * Clear the de-duplication table, for tests. + */ + static _flush() { + this.parameters.clear(); + } +} +PathParameter.kPathParameter = kPathParameter; +PathParameter.parameters = parameters; + +module.exports = PathParameter; \ No newline at end of file