UNPKG

732 BPlain TextView Raw
1import Attribute, { AttributeConstructor } from './attributes/base';
2
3interface AttributeMapType {
4 [key: string]: any;
5}
6
7// 所有的 attribute map
8const ATTRIBUTE_MAP: AttributeMapType = {};
9
10/**
11 * 通过类型获得 Attribute 类
12 * @param type
13 */
14const getAttribute = (type: string) => {
15 return ATTRIBUTE_MAP[type.toLowerCase()];
16};
17
18const registerAttribute = (type: string, ctor: AttributeConstructor) => {
19 // 注册的时候,需要校验 type 重名,不区分大小写
20 if (getAttribute(type)) {
21 throw new Error(`Attribute type '${type}' existed.`);
22 }
23 // 存储到 map 中
24 ATTRIBUTE_MAP[type.toLowerCase()] = ctor;
25};
26
27export { getAttribute, registerAttribute, Attribute };
28export * from './interface';