From dd854a1d1134a7ce8f0d35e5135563d0bb9d453f 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 --- lib/command/index.js | 8 ++++++-- test/command.js | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) 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