Last active
September 30, 2020 02:03
-
-
Save nurikk/00b55d6c3dc38ad45878a6d21fbeac88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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