-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a utils method to get all enums in a type recursivly
- Loading branch information
1 parent
cb455f9
commit 1aa4be6
Showing
4 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from "./models" | ||
export { Parser } from "./parser" | ||
export * from "./rewrite" | ||
export * from "./utils/prettify" | ||
|
||
export * from "./models" | ||
export * from "./writer" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./utils/getAllEnumsInside" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ArrayType, EnumType, IntersectionType, TypeLiteral, UnionType } from "../../models" | ||
import { Type } from "../../models/Type" | ||
import { L } from "../../utils/logger" | ||
|
||
export function getAllEnumsInside(type: Type): Set<EnumType> { | ||
const s = new Set<EnumType>() | ||
|
||
function append(set: Set<EnumType>) { | ||
set.forEach((e) => s.add(e)) | ||
} | ||
|
||
L.d("<getAllEnumsInside>") | ||
if (type instanceof UnionType) { | ||
L.d("isUnionType") | ||
type.types.map(getAllEnumsInside).forEach(append) | ||
} else if (type instanceof IntersectionType) { | ||
L.d("isIntersection") | ||
type.types.map(getAllEnumsInside).forEach(append) | ||
} else if (type instanceof ArrayType) { | ||
L.d("isArray") | ||
append(getAllEnumsInside(type.arrayType)) | ||
} else if (type instanceof TypeLiteral) { | ||
L.d("isTypeLiteral") | ||
Array.from(type.properties.values()).map(getAllEnumsInside).forEach(append) | ||
} else if (type instanceof EnumType) { | ||
s.add(type) | ||
} | ||
|
||
return s | ||
} |