UNPKG

node-hue-api

Version:
40 lines (39 loc) 1.25 kB
import { ApiError } from '../../ApiError'; export class Placeholder { constructor(type, defaultName, name) { this._name = name || defaultName; this._type = type; } get name() { return this._name; } get typeDefinition() { return this._type; } set typeDefinition(type) { this._type = type; } inject(uri, parameters) { const placeholderText = `<${this.name}>`; if (uri.indexOf(placeholderText) > -1) { return uri.replace(placeholderText, this.getValue(parameters)); } return uri; } getValue(parameters) { const typeDefinition = this.typeDefinition; if (!typeDefinition) { throw new ApiError('No type definition has been specified for placeholder'); } const parameter = parameters ? parameters[this.name] : undefined; const value = this._getParameterValue(parameter); return typeDefinition.getValue(value); } _getParameterValue(parameter) { return parameter; } toString() { const type = this.typeDefinition; return `${this.name}: { type:${type.type}, optional:${type.optional}, defaultValue:${type.defaultValue} }`; } }