fix for number input, new integer input
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
export type OptionType =
|
||||
'string'
|
||||
| 'number'
|
||||
| 'int'
|
||||
| 'boolean'
|
||||
| 'file'
|
||||
| 'folder'
|
||||
|
8
lib/Options/IntegerOption.ts
Normal file
8
lib/Options/IntegerOption.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||
import { BaseOption } from './BaseOption';
|
||||
|
||||
export class IntegerOption extends BaseOption<number> {
|
||||
protected get validation ():TypeValidation {
|
||||
return new TypeValidation ('int');
|
||||
}
|
||||
}
|
19
lib/Sources/Interactive/NumberSubSource.ts
Normal file
19
lib/Sources/Interactive/NumberSubSource.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { NumberPrompt } from 'enquirer';
|
||||
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||
|
||||
export class NumberSubSource extends InteractiveSubSource {
|
||||
protected condition ():boolean {
|
||||
return this.val.type_validation.option_type === 'number';
|
||||
}
|
||||
|
||||
protected async run ():Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new NumberPrompt ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
@ -2,10 +2,12 @@ import { ArraySubSource } from './ArraySubSource';
|
||||
import { BooleanSubSource } from './BooleanSubSource';
|
||||
import { PresetSubSource } from './PresetSubSource';
|
||||
import { StringSubSource } from './StringSubSource';
|
||||
import { NumberSubSource } from './NumberSubSource';
|
||||
|
||||
export const sources = [
|
||||
ArraySubSource,
|
||||
BooleanSubSource,
|
||||
PresetSubSource,
|
||||
NumberSubSource,
|
||||
StringSubSource
|
||||
];
|
||||
|
@ -40,6 +40,13 @@ export class TypeValidation {
|
||||
return Promise.resolve (String (value));
|
||||
|
||||
if (this.option_type === 'number') {
|
||||
const as_num = parseFloat (String (value));
|
||||
if (isNaN (as_num))
|
||||
throw new Error ('value is not a number');
|
||||
return Promise.resolve (as_num);
|
||||
}
|
||||
|
||||
if (this.option_type === 'int') {
|
||||
const as_num = parseInt (String (value));
|
||||
if (isNaN (as_num))
|
||||
throw new Error ('value is not a number');
|
||||
|
@ -12,3 +12,4 @@ export { FolderOption } from './Options/FolderOption';
|
||||
export { NumberOption } from './Options/NumberOption';
|
||||
export { PathOption } from './Options/PathOption';
|
||||
export { StringOption } from './Options/StringOption';
|
||||
export { IntegerOption } from './Options/IntegerOption';
|
||||
|
Reference in New Issue
Block a user