diff --git a/.yarn/cache/lodash.get-npm-4.4.2-7bda64ed87-2a4925f6e8.zip b/.yarn/cache/lodash.get-npm-4.4.2-7bda64ed87-2a4925f6e8.zip new file mode 100644 index 000000000..c41105a28 Binary files /dev/null and b/.yarn/cache/lodash.get-npm-4.4.2-7bda64ed87-2a4925f6e8.zip differ diff --git a/.yarn/cache/lodash.set-npm-4.3.2-7586c942c2-f0968109bc.zip b/.yarn/cache/lodash.set-npm-4.3.2-7586c942c2-f0968109bc.zip new file mode 100644 index 000000000..cce491ca4 Binary files /dev/null and b/.yarn/cache/lodash.set-npm-4.3.2-7586c942c2-f0968109bc.zip differ diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 6a2c5fe91..3bc305a77 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -1,6 +1,4 @@ import assert from 'node:assert'; -import filter from 'lodash/filter.js'; -import map from 'lodash/map.js'; import Separator from './separator.js'; import Choice from './choice.js'; @@ -78,11 +76,19 @@ export default class Choices { /** * Match the valid choices against a where clause - * @param {Object} whereClause Lodash `where` clause + * @param {Function|Object} whereClause filter function or key-value object to match against * @return {Array} Matching choices or empty array */ where(whereClause) { - return filter(this.realChoices, whereClause); + let filterFn; + if (typeof whereClause === 'function') { + filterFn = whereClause; + } else { + const [key, value] = Object.entries(whereClause)[0]; + filterFn = (choice) => choice[key] === value; + } + + return this.realChoices.filter(filterFn); } /** @@ -91,7 +97,7 @@ export default class Choices { * @return {Array} Selected properties */ pluck(propertyName) { - return map(this.realChoices, propertyName); + return this.realChoices.map((choice) => choice[propertyName]); } // Expose usual Array methods diff --git a/packages/inquirer/lib/prompts/base.js b/packages/inquirer/lib/prompts/base.js index 68b580423..c3bcda705 100644 --- a/packages/inquirer/lib/prompts/base.js +++ b/packages/inquirer/lib/prompts/base.js @@ -1,14 +1,7 @@ -import defaults from 'lodash/defaults.js'; -import clone from 'lodash/clone.js'; /** * Base prompt implementation * Should be extended by prompt types. */ -const _ = { - defaults, - clone, -}; - import pc from 'picocolors'; import runAsync from 'run-async'; import { filter, flatMap, share, take, takeUntil } from 'rxjs'; @@ -24,7 +17,7 @@ export default class Prompt { }); // Set defaults prompt options - this.opt = _.defaults(_.clone(question), { + this.opt = { validate: () => true, validatingText: '', filter: (val) => val, @@ -33,7 +26,8 @@ export default class Prompt { suffix: '', prefix: pc.green('?'), transformer: (val) => val, - }); + ...question, + }; // Make sure name is present if (!this.opt.name) { diff --git a/packages/inquirer/lib/ui/prompt.js b/packages/inquirer/lib/ui/prompt.js index 13b838908..29b1cbbb3 100644 --- a/packages/inquirer/lib/ui/prompt.js +++ b/packages/inquirer/lib/ui/prompt.js @@ -1,13 +1,21 @@ -import isPlainObject from 'lodash/isPlainObject.js'; -import get from 'lodash/get.js'; -import set from 'lodash/set.js'; +import get from 'lodash.get'; +import set from 'lodash.set'; const _ = { - isPlainObject, set, get, }; -import { defer, empty, from, of, concatMap, filter, publish, reduce } from 'rxjs'; +import { + defer, + empty, + from, + of, + concatMap, + filter, + publish, + reduce, + isObservable, +} from 'rxjs'; import runAsync from 'run-async'; import * as utils from '../utils/utils.js'; import Base from './baseUI.js'; @@ -23,23 +31,33 @@ export default class PromptUI extends Base { run(questions, answers) { // Keep global reference to the answers - this.answers = _.isPlainObject(answers) ? { ...answers } : {}; - - // Make sure questions is an array. - if (_.isPlainObject(questions)) { - // It's either an object of questions or a single question - questions = Object.values(questions).every( - (v) => _.isPlainObject(v) && v.name === undefined, + this.answers = typeof answers === 'object' ? { ...answers } : {}; + + let obs; + if (Array.isArray(questions)) { + obs = from(questions); + } else if (isObservable(questions)) { + obs = questions; + } else if ( + Object.values(questions).every( + (maybeQuestion) => + typeof maybeQuestion === 'object' && + !Array.isArray(maybeQuestion) && + maybeQuestion != null, ) - ? Object.entries(questions).map(([name, question]) => ({ name, ...question })) - : [questions]; + ) { + // Case: Called with a set of { name: question } + obs = from( + Object.entries(questions).map(([name, question]) => ({ + name, + ...question, + })), + ); + } else { + // Case: Called with a single question config + obs = from([questions]); } - // Create an observable, unless we received one as parameter. - // Note: As this is a public interface, we cannot do an instanceof check as we won't - // be using the exact same object in memory. - const obs = Array.isArray(questions) ? from(questions) : questions; - this.process = obs.pipe( concatMap(this.processQuestion.bind(this)), publish(), // Creates a hot Observable. It prevents duplicating prompts. diff --git a/packages/inquirer/package.json b/packages/inquirer/package.json index c400e1bca..9958bda3f 100644 --- a/packages/inquirer/package.json +++ b/packages/inquirer/package.json @@ -63,7 +63,8 @@ "ansi-escapes": "^4.3.2", "cli-width": "^4.1.0", "external-editor": "^3.1.0", - "lodash": "^4.17.21", + "lodash.get": "^4.4.2", + "lodash.set": "^4.3.2", "mute-stream": "1.0.0", "ora": "^5.4.1", "picocolors": "^1.0.1", diff --git a/yarn.lock b/yarn.lock index 9591b3f27..b893d8965 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4249,7 +4249,8 @@ __metadata: ansi-escapes: "npm:^4.3.2" cli-width: "npm:^4.1.0" external-editor: "npm:^3.1.0" - lodash: "npm:^4.17.21" + lodash.get: "npm:^4.4.2" + lodash.set: "npm:^4.3.2" mute-stream: "npm:1.0.0" ora: "npm:^5.4.1" picocolors: "npm:^1.0.1" @@ -4972,6 +4973,13 @@ __metadata: languageName: node linkType: hard +"lodash.get@npm:^4.4.2": + version: 4.4.2 + resolution: "lodash.get@npm:4.4.2" + checksum: 10/2a4925f6e89bc2c010a77a802d1ba357e17ed1ea03c2ddf6a146429f2856a216663e694a6aa3549a318cbbba3fd8b7decb392db457e6ac0b83dc745ed0a17380 + languageName: node + linkType: hard + "lodash.ismatch@npm:^4.4.0": version: 4.4.0 resolution: "lodash.ismatch@npm:4.4.0" @@ -4986,6 +4994,13 @@ __metadata: languageName: node linkType: hard +"lodash.set@npm:^4.3.2": + version: 4.3.2 + resolution: "lodash.set@npm:4.3.2" + checksum: 10/f0968109bca5625c8ce1f1beab758634484443604d3950477e46d8d2631562e5ceae4465b9ce8a393fd47f5a411329f9bacf956c7c95530af1290db1a20343ba + languageName: node + linkType: hard + "lodash@npm:^4.17.21": version: 4.17.21 resolution: "lodash@npm:4.17.21"