-
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.
- Loading branch information
1 parent
5d441bd
commit 23f2cb0
Showing
3 changed files
with
39 additions
and
58 deletions.
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
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 |
---|---|---|
@@ -1,68 +1,48 @@ | ||
export class TypeChecker { | ||
constructor(private value: any) {} | ||
|
||
isString(): boolean { | ||
return typeof this.value === 'string'; | ||
} | ||
|
||
isNumber(): boolean { | ||
return typeof this.value === 'number'; | ||
} | ||
|
||
isBoolean(): boolean { | ||
return typeof this.value === 'boolean'; | ||
} | ||
import { Type } from 'ts-morph'; | ||
|
||
isDate(): boolean { | ||
return this.value instanceof Date; | ||
} | ||
|
||
isArray(): boolean { | ||
return Array.isArray(this.value); | ||
} | ||
|
||
isObject(): boolean { | ||
return this.value !== null && typeof this.value === 'object' && !Array.isArray(this.value) && !(this.value instanceof Date); | ||
} | ||
export class TypeChecker { | ||
private readonly type: Type; | ||
|
||
isBuffer(): boolean { | ||
return Buffer.isBuffer(this.value); | ||
private constructor(type: Type) { | ||
this.type = type; | ||
} | ||
|
||
isFile(): boolean { | ||
return this.value instanceof File; | ||
static new(type: Type): TypeChecker { | ||
return new TypeChecker(type); | ||
} | ||
|
||
|
||
getTypeScriptType(): string { | ||
if (this.value.isString()) { | ||
if (this.type.isString()) { | ||
return 'string'; | ||
} | ||
if (this.value.isNumber()) { | ||
} else if (this.type.isNumber()) { | ||
return 'number'; | ||
} | ||
if (this.value.isBoolean()) { | ||
} else if (this.type.isBoolean()) { | ||
return 'boolean'; | ||
} else if (this.type.isArray()) { | ||
const elementType = this.type.getArrayElementTypeOrThrow(); | ||
return `${this.getTypeScriptTypeFromElement(elementType)}[]`; | ||
} else if (this.type.isClassOrInterface() || this.type.isObject()) { | ||
return 'object'; | ||
} else if (this.type.isUndefined()) { | ||
return 'undefined'; | ||
} else if (this.type.isNull()) { | ||
return 'null'; | ||
} else if (this.type.isEnum() || this.type.isEnumLiteral()) { | ||
return 'enum'; | ||
} else { | ||
return 'any'; | ||
} | ||
} | ||
|
||
private getTypeScriptTypeFromElement(type: Type): string { | ||
if (type.isString()) { | ||
return 'string'; | ||
} else if (type.isNumber()) { | ||
return 'number'; | ||
} else if (type.isBoolean()) { | ||
return 'boolean'; | ||
} else { | ||
return 'unknown'; | ||
} | ||
if (this.value.isDate()) { | ||
return 'Date'; | ||
} | ||
if (this.value.isArray()) { | ||
return 'Array'; | ||
} | ||
if (this.value.isObject()) { | ||
return 'Object'; | ||
} | ||
if (this.value.isBuffer()) { | ||
return 'Buffer'; | ||
} | ||
if (this.value.isFile()) { | ||
return 'File'; | ||
} | ||
return 'any'; | ||
} | ||
|
||
static new(value: any): TypeChecker { | ||
return new TypeChecker(value); | ||
} | ||
} | ||
} |