Skip to content

Instantly share code, notes, and snippets.

@nurikk
Last active September 30, 2020 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurikk/00b55d6c3dc38ad45878a6d21fbeac88 to your computer and use it in GitHub Desktop.
Save nurikk/00b55d6c3dc38ad45878a6d21fbeac88 to your computer and use it in GitHub Desktop.
type LightFeatures = "state" | "brightness" | "color_temp" | "color_xy" | "color_hs";
interface Exposing {
endpoint?: string;
features: LightFeatures[] | string[];
}
class DeviceBuilder<T> {
endpoint: string;
features: Set<T>;
constructor(endpoint: string = undefined) {
this.endpoint = endpoint;
this.features = new Set<T>();
}
build(): Exposing {
const result: Exposing = {
features: Array.from(this.features) as unknown as string[]
};
if (this.endpoint !== undefined) {
result.endpoint = this.endpoint;
}
return result;
}
}
class LightBuilder extends DeviceBuilder<LightFeatures> {
withState() {
this.features.add("state");
return this;
}
withBrightness() {
this.features.add("brightness");
return this;
}
withColorTemp() {
this.features.add("color_temp");
return this;
}
withColorXY() {
this.features.add("color_xy");
return this;
}
withColorHS() {
this.features.add("color_hs");
return this;
}
}
new LightBuilder().withBrightness().withColorHS().withState().build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment