/
class_properties.ts
39 lines (35 loc) · 1.13 KB
/
class_properties.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Constructor } from "./constructor"
import { Controller } from "./controller"
import { readInheritableStaticArrayValues } from "./inheritable_statics"
import { capitalize } from "./string_helpers"
export function ClassPropertiesBlessing<T>(constructor: Constructor<T>) {
const classes = readInheritableStaticArrayValues(constructor, "classes")
return classes.reduce((properties, classDefinition) => {
return Object.assign(properties, propertiesForClassDefinition(classDefinition))
}, {} as PropertyDescriptorMap)
}
function propertiesForClassDefinition(key: string) {
return {
[`${key}Class`]: {
get(this: Controller) {
const { classes } = this
if (classes.has(key)) {
return classes.get(key)
} else {
const attribute = classes.getAttributeName(key)
throw new Error(`Missing attribute "${attribute}"`)
}
},
},
[`${key}Classes`]: {
get(this: Controller) {
return this.classes.getAll(key)
},
},
[`has${capitalize(key)}Class`]: {
get(this: Controller) {
return this.classes.has(key)
},
},
}
}