/
EnumTypeFormatter.ts
27 lines (25 loc) · 1.14 KB
/
EnumTypeFormatter.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
import { Definition } from "../Schema/Definition.js";
import { SubTypeFormatter } from "../SubTypeFormatter.js";
import { BaseType } from "../Type/BaseType.js";
import { EnumType } from "../Type/EnumType.js";
import { typeName } from "../Utils/typeName.js";
import { uniqueArray } from "../Utils/uniqueArray.js";
export class EnumTypeFormatter implements SubTypeFormatter {
public supportsType(type: BaseType): boolean {
return type instanceof EnumType;
}
public getDefinition(type: EnumType): Definition {
const values = uniqueArray(type.getValues());
const types = uniqueArray(values.map(typeName));
// NOTE: We want to use "const" when referencing an enum member.
// However, this formatter is used both for enum members and enum types,
// so the side effect is that an enum type that contains just a single
// value is represented as "const" too.
return values.length === 1
? { type: types[0], const: values[0] }
: { type: types.length === 1 ? types[0] : types, enum: values };
}
public getChildren(type: EnumType): BaseType[] {
return [];
}
}