From 5981f77d018aa5c2b8a1da51e4847aa5c0ea9b01 Mon Sep 17 00:00:00 2001 From: Eric Satterwhite Date: Fri, 29 Dec 2023 14:38:37 -0600 Subject: [PATCH] feat(command): add support for input filter for non-interactive the input filter, previously supported for interactive mode via inquirer allow transformation of the input before validation happens. This allows for a great deal of functionality and flexibility for end users being able to fix typos, enter short hands, etc. This is being extended into the non-interactive commands as well --- README.md | 58 ++++++++++++++++++++++---------------------- lib/command/index.js | 8 ++++-- test/command.js | 5 +++- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 16cc7d2..db4a125 100644 --- a/README.md +++ b/README.md @@ -114,34 +114,34 @@ node ./cli world --name=Mark --name=Sally --no-excited * [API](#api) - * [Seeli.run( )](#seelirun-) - * [Seeli.list``](#seelilistarray) - * [Seeli.use( name ``, cmd `` )](#seeliuse-name-string-cmd-command-) - * [Seeli.bold( text ``)](#seelibold-text-string) - * [Seeli.green( text ``)](#seeligreen-text-string) - * [Seeli.blue( text ``)](#seeliblue-text-string) - * [Seeli.red( text ``)](#seelired-text-string) - * [Seeli.yellow( text ``)](#seeliyellow-text-string) - * [Seeli.cyan( text ``)](#seelicyan-text-string) - * [Seeli.magenta( text ``)](#seelimagenta-text-string) - * [Seeli.redBright( text ``)](#seeliredbright-text-string) - * [Seeli.blueBright( text ``)](#seelibluebright-text-string) - * [Seeli.greenBright( text ``)](#seeligreenbright-text-string) - * [Seeli.yellowBright( text ``)](#seeliyellowbright-text-string) - * [Seeli.cyanBright( text ``)](#seelicyanbright-text-string) - * [Seeli.config( key ``, value `` )](#seeliconfig-key-string-value-object-) - * [Seeli.config( opts `` )](#seeliconfig-opts-object-) - * [Seeli.config( key `` )](#seeliconfig-key-string-) - * [Supported Confgurations](#supported-confgurations) - * [Package Configuration](#package-configuration) - * [Command( options `` )](#command-options-object-) - * [Options](#options) - * [Flag Options](#flag-options) - * [Nested Flags](#nested-flags) - * [Auto Help](#auto-help) - * [Asyncronous](#asyncronous) - * [Progress](#progress) - * [Events](#events) + * [Seeli.run( )](#seelirun-) + * [Seeli.list``](#seelilistarray) + * [Seeli.use( name ``, cmd `` )](#seeliuse-name-string-cmd-command-) + * [Seeli.bold( text ``)](#seelibold-text-string) + * [Seeli.green( text ``)](#seeligreen-text-string) + * [Seeli.blue( text ``)](#seeliblue-text-string) + * [Seeli.red( text ``)](#seelired-text-string) + * [Seeli.yellow( text ``)](#seeliyellow-text-string) + * [Seeli.cyan( text ``)](#seelicyan-text-string) + * [Seeli.magenta( text ``)](#seelimagenta-text-string) + * [Seeli.redBright( text ``)](#seeliredbright-text-string) + * [Seeli.blueBright( text ``)](#seelibluebright-text-string) + * [Seeli.greenBright( text ``)](#seeligreenbright-text-string) + * [Seeli.yellowBright( text ``)](#seeliyellowbright-text-string) + * [Seeli.cyanBright( text ``)](#seelicyanbright-text-string) + * [Seeli.config( key ``, value `` )](#seeliconfig-key-string-value-object-) + * [Seeli.config( opts `` )](#seeliconfig-opts-object-) + * [Seeli.config( key `` )](#seeliconfig-key-string-) + * [Supported Confgurations](#supported-confgurations) + * [Package Configuration](#package-configuration) + * [Command( options `` )](#command-options-object-) + * [Options](#options) + * [Flag Options](#flag-options) + * [Nested Flags](#nested-flags) + * [Auto Help](#auto-help) + * [Asyncronous](#asyncronous) + * [Progress](#progress) + * [Events](#events) # API @@ -287,7 +287,7 @@ name | required | type | description **event** | `false` | `boolean` | if set to `true` the command will emit an event withe the same name as the flag with **the** value that was captured for that flag | **when** | `false` | `function` | **interactive mode only** Receives the current user answers hash and should return true or **false** depending on whether or not this question should be asked. **validate** | `false` | `function` | receives user input and should return true if the value is **valid**, and an error message (String) otherwise. If false is returned, a default error message is provided. -**filter** | `false` | `function` | **interactive mode only** Receives the user input and return the filtered value to be used **inside** the program. The value returned will be added to the Answers hash. +**filter** | `false` | `function` | Receives the user input and return the filtered value to be used **inside** the program. The value returned will be added to the Answers hash. #### Nested Flags diff --git a/lib/command/index.js b/lib/command/index.js index f6afd0d..eb5a851 100644 --- a/lib/command/index.js +++ b/lib/command/index.js @@ -237,7 +237,11 @@ class Command extends Registry { return this.parsed } const value = hasOwn(this.parsed, key) ? this.parsed[key] : flag.default - object.set(this.parsed, key, value) + object.set( + this.parsed + , key + , typeof flag.filter === 'function' ? flag.filter(value, this.parsed) : value + ) } return this.parsed } @@ -648,7 +652,7 @@ function toQuestion(flag, opts, answers) { // can return them arg.when = opts.when ? opts.when.bind(null, answers) : undefined arg.validate = opts.validate ? opts.validate.bind(null, answers) : undefined - arg.filter = opts.filter ? opts.filter.bind(null, answers) : undefined + arg.filter = opts.filter ? opts.filter.bind(null) : undefined arg.transformer = opts.transformer ? opts.transformer : transform.bind(arg) return arg } diff --git a/test/command.js b/test/command.js index 3a45c89..3d4f7d6 100644 --- a/test/command.js +++ b/test/command.js @@ -58,6 +58,9 @@ test('command', async (t) => { , 'foo:bar:baz': { type: Number , required: true + , filter(input) { + return input + 100 + } } , 'nested:array': { type: [Number, Array] @@ -71,7 +74,7 @@ test('command', async (t) => { t.match(data, { foo: { bar: { - baz: Number + baz: 112 } } , test: Boolean