From 7ad999878afcc434f77616b46319f0be489c52a1 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sun, 19 Jul 2020 11:41:06 +0200 Subject: [PATCH] fix for number input, new integer input --- AppTest.js | 7 +++++-- CHANGELOG.md | 5 +++++ README.md | 5 +++-- lib/OptionType.ts | 1 + lib/Options/IntegerOption.ts | 8 ++++++++ lib/Sources/Interactive/NumberSubSource.ts | 19 +++++++++++++++++++ lib/Sources/Interactive/index.ts | 2 ++ lib/TypeValidation/TypeValidation.ts | 7 +++++++ lib/index.ts | 1 + package.json | 22 +++++++++++++++++----- test/TypeValidation.ts | 8 +++++++- 11 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 lib/Options/IntegerOption.ts create mode 100644 lib/Sources/Interactive/NumberSubSource.ts diff --git a/AppTest.js b/AppTest.js index 20a05d1..e75f8db 100644 --- a/AppTest.js +++ b/AppTest.js @@ -17,7 +17,8 @@ const { BooleanOption, NumberOption, ArrayOption, - FolderOption + FolderOption, + IntegerOption } = require ('./dist/lib/index.js'); (async () => { @@ -27,12 +28,14 @@ const { .parse (); const num = await new NumberOption ({ name: 'num' }) .parse (); + const int = await new IntegerOption ({ name: 'num' }) + .parse (); const arr = await new ArrayOption ({ name: 'arr' }) .parse (); const fld = await new FolderOption ({ name: 'fld' }) .parse (); - const data = { str, bool, num, arr, fld }; + const data = { str, bool, num, int, arr, fld }; console.log (data); }) (); diff --git a/CHANGELOG.md b/CHANGELOG.md index a588083..a577ca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.1.0 + +- Fix for NumberOption: do not cut off float values +- New type IntegerOption: only allows integer values + ## 2.0.0 Restructuring to split different Option types and keep specific parameters separate diff --git a/README.md b/README.md index 010b0e8..11676bb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # @sapphirecode/console-app -version: 2.0.x +version: 2.1.x read parameters from env, config files, console args or interactively @@ -22,7 +22,8 @@ const { BooleanOption, FileOption, // paths that exist and are a file FolderOption, // paths that exist and are a folder - NumberOption, + NumberOption, // integer and float values + IntegerOption, // only integer values PathOption, // paths that exist in the file system StringOption, } = require('@sapphirecode/console-app'); diff --git a/lib/OptionType.ts b/lib/OptionType.ts index 5eb678e..59a4210 100644 --- a/lib/OptionType.ts +++ b/lib/OptionType.ts @@ -8,6 +8,7 @@ export type OptionType = 'string' | 'number' + | 'int' | 'boolean' | 'file' | 'folder' diff --git a/lib/Options/IntegerOption.ts b/lib/Options/IntegerOption.ts new file mode 100644 index 0000000..7893b1d --- /dev/null +++ b/lib/Options/IntegerOption.ts @@ -0,0 +1,8 @@ +import { TypeValidation } from '../TypeValidation/TypeValidation'; +import { BaseOption } from './BaseOption'; + +export class IntegerOption extends BaseOption { + protected get validation ():TypeValidation { + return new TypeValidation ('int'); + } +} diff --git a/lib/Sources/Interactive/NumberSubSource.ts b/lib/Sources/Interactive/NumberSubSource.ts new file mode 100644 index 0000000..50b4b60 --- /dev/null +++ b/lib/Sources/Interactive/NumberSubSource.ts @@ -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 { + await this.val.assign_arg ( + this.opt, + await new NumberPrompt ({ + message: this.get_message (), + default: this.opt.default + }) + .run () + ); + } +} diff --git a/lib/Sources/Interactive/index.ts b/lib/Sources/Interactive/index.ts index dba59a3..5bae306 100644 --- a/lib/Sources/Interactive/index.ts +++ b/lib/Sources/Interactive/index.ts @@ -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 ]; diff --git a/lib/TypeValidation/TypeValidation.ts b/lib/TypeValidation/TypeValidation.ts index 5129764..f37b524 100644 --- a/lib/TypeValidation/TypeValidation.ts +++ b/lib/TypeValidation/TypeValidation.ts @@ -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'); diff --git a/lib/index.ts b/lib/index.ts index 6f22a89..73ec249 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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'; diff --git a/package.json b/package.json index 2cbc496..a2df311 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,17 @@ { "name": "@sapphirecode/console-app", - "version": "2.0.10", + "version": "2.1.0", "main": "dist/lib/index.js", - "author": "Timo Hocker ", + "author": { + "name": "Timo Hocker", + "email": "timo@scode.ovh" + }, "license": "MIT", "repository": { "type": "git", - "url": "git@git.scode.ovh:timo/console-app" + "url": "https://git.scode.ovh:timo/console-app.git" }, + "bugs": "https://redmine.scode.ovh/projects/console-app", "description": "read parameters from env, console args or interactively", "devDependencies": { "@ava/typescript": "^1.1.1", @@ -37,5 +41,13 @@ "fs-extra": "^9.0.0", "hjson": "^3.2.1", "yargs": "^15.3.1" - } -} \ No newline at end of file + }, + "keywords": [ + "interactive", + "console input", + "config", + "command line args", + "environment variables", + "parsing" + ] +} diff --git a/test/TypeValidation.ts b/test/TypeValidation.ts index 72a8aae..9171b36 100644 --- a/test/TypeValidation.ts +++ b/test/TypeValidation.ts @@ -24,7 +24,13 @@ test ('no number', (t) => { test ('number', async (t) => { const validator = new TypeValidation ('number'); - const res = await validator.to_type ('123'); + const res = await validator.to_type ('123.4'); + t.is (res, 123.4); +}); + +test ('int', async (t) => { + const validator = new TypeValidation ('int'); + const res = await validator.to_type ('123.4'); t.is (res, 123); });