-
Notifications
You must be signed in to change notification settings - Fork 424
/
class_properties.ts
36 lines (32 loc) · 1.05 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
import { Constructor } from "./constructor"
import { Controller } from "./controller"
import { readInheritableStaticArrayValues } from "./inheritable_statics"
import { capitalize } from "./string_helpers"
/** @hidden */
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) {
const name = `${key}Class`
return {
[name]: {
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}"`)
}
}
},
[`has${capitalize(name)}`]: {
get(this: Controller) {
return this.classes.has(key)
}
}
}
}