refactoring
This commit is contained in:
		
							
								
								
									
										23
									
								
								lib/Types/PathType.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/Types/PathType.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| import fs from 'fs-extra'; | ||||
| import { TypeValidation } from './TypeValidation'; | ||||
|  | ||||
| export class PathType extends TypeValidation { | ||||
|   public get string_type (): 'string'|'number'|'boolean'|'array' { | ||||
|     return 'string'; | ||||
|   } | ||||
|  | ||||
|   public async to_type (value: unknown): Promise<unknown> { | ||||
|     if (typeof value !== 'string') | ||||
|       throw new Error (`invalid type for ${this.general_type}`); | ||||
|     if (!await fs.pathExists (value)) | ||||
|       throw new Error ('path does not exist'); | ||||
|     if (this.general_type === 'path') | ||||
|       return value; | ||||
|  | ||||
|     const stat = await fs.stat (value); | ||||
|     if (stat.isDirectory () === (this.general_type === 'folder')) | ||||
|       return value; | ||||
|  | ||||
|     throw new Error ('cannot assign folder to file'); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										44
									
								
								lib/Types/TypeValidation.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								lib/Types/TypeValidation.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| export class TypeValidation { | ||||
|   private readonly _general_type: string; | ||||
|  | ||||
|   public get general_type (): string { | ||||
|     return this._general_type; | ||||
|   } | ||||
|  | ||||
|   public get string_type (): 'string'|'number'|'boolean'|'array' { | ||||
|     return this._general_type as 'string'|'number'|'boolean'|'array'; | ||||
|   } | ||||
|  | ||||
|   public constructor (type: string) { | ||||
|     this._general_type = type; | ||||
|   } | ||||
|  | ||||
|   public validate_type (value: unknown): boolean { | ||||
|     return typeof value === this.general_type; | ||||
|   } | ||||
|  | ||||
|   public to_type (value: unknown): Promise<unknown> { | ||||
|     if (this.general_type === 'string') | ||||
|       return Promise.resolve (String (value)); | ||||
|  | ||||
|     if (this.general_type === 'number') { | ||||
|       const as_num = parseInt (String (value)); | ||||
|       if (isNaN (as_num)) | ||||
|         throw new Error ('value is not a number'); | ||||
|       return Promise.resolve (as_num); | ||||
|     } | ||||
|  | ||||
|     if (this.general_type === 'boolean') { | ||||
|       const as_num = parseInt (String (value)); | ||||
|       if ( | ||||
|         as_num !== 1 && as_num !== 0 | ||||
|         && !(/^(?:true|false)$/iu).test (String (value)) | ||||
|       ) | ||||
|         throw new Error ('value is not a boolean'); | ||||
|       return Promise.resolve ( | ||||
|         as_num === 1 || (/true/iu).test (String (value)) | ||||
|       ); | ||||
|     } | ||||
|     throw new Error ('unknown type'); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user