From 1ad311d4c4532dd45bc5d01117797423023cd5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:03:58 +0100 Subject: [PATCH 1/8] remove lodash noop --- packages/core/index.js | 7 ++++--- packages/inquirer/test/specs/inquirer.js | 3 +-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/index.js b/packages/core/index.js index 215091e1d..63fdd8fa9 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -1,6 +1,5 @@ const _ = { isFunction: require('lodash/isFunction'), - noop: require('lodash/noop'), }; const readline = require('readline'); const chalk = require('chalk'); @@ -85,14 +84,16 @@ class StateManager { } onKeypress(value, key) { - const { onKeypress = _.noop } = this.config; + const { onKeypress } = this.config; // Ignore enter keypress. The "line" event is handling those. if (key.name === 'enter' || key.name === 'return') { return; } this.setState({ value: this.rl.line, error: null }); - onKeypress(this.rl.line, key, this.getState(), this.setState); + if (onKeypress) { + onKeypress(this.rl.line, key, this.getState(), this.setState); + } } startLoading() { diff --git a/packages/inquirer/test/specs/inquirer.js b/packages/inquirer/test/specs/inquirer.js index 1b05e90a0..43c4b1813 100644 --- a/packages/inquirer/test/specs/inquirer.js +++ b/packages/inquirer/test/specs/inquirer.js @@ -8,7 +8,6 @@ const stream = require('stream'); const tty = require('tty'); const { expect } = require('chai'); const sinon = require('sinon'); -const _ = require('lodash'); const { Observable } = require('rxjs'); const inquirer = require('../../lib/inquirer'); @@ -826,7 +825,7 @@ describe('inquirer.prompt', () => { describe('#restoreDefaultPrompts()', () => { it('restore default prompts', () => { const ConfirmPrompt = inquirer.prompt.prompts.confirm; - inquirer.registerPrompt('confirm', _.noop); + inquirer.registerPrompt('confirm', () => {}); inquirer.restoreDefaultPrompts(); expect(ConfirmPrompt).to.equal(inquirer.prompt.prompts.confirm); }); From 33805bfccf64b0577d720fefefc7e157fcf3b114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:10:02 +0100 Subject: [PATCH 2/8] remove lodash extend --- packages/inquirer/lib/objects/choice.js | 3 +-- packages/inquirer/lib/prompts/confirm.js | 3 +-- packages/inquirer/lib/prompts/rawlist.js | 3 +-- packages/inquirer/lib/ui/baseUI.js | 15 ++++++--------- packages/inquirer/test/helpers/readline.js | 5 ++--- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 22d8a9b39..d73dddb02 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -1,7 +1,6 @@ 'use strict'; const _ = { isNumber: require('lodash/isNumber'), - extend: require('lodash/extend'), isFunction: require('lodash/isFunction'), }; @@ -26,7 +25,7 @@ module.exports = class Choice { this.value = val; this.short = String(val); } else { - _.extend(this, val, { + Object.assign(this, val, { name: val.name || val.value, value: 'value' in val ? val.value : val.name, short: val.short || val.name || val.value, diff --git a/packages/inquirer/lib/prompts/confirm.js b/packages/inquirer/lib/prompts/confirm.js index 8171aab5d..287279af0 100644 --- a/packages/inquirer/lib/prompts/confirm.js +++ b/packages/inquirer/lib/prompts/confirm.js @@ -4,7 +4,6 @@ */ const _ = { - extend: require('lodash/extend'), isBoolean: require('lodash/isBoolean'), }; const chalk = require('chalk'); @@ -18,7 +17,7 @@ class ConfirmPrompt extends Base { let rawDefault = true; - _.extend(this.opt, { + Object.assign(this.opt, { filter(input) { let value = rawDefault; if (input != null && input !== '') { diff --git a/packages/inquirer/lib/prompts/rawlist.js b/packages/inquirer/lib/prompts/rawlist.js index 06036eafb..a018b4ad5 100644 --- a/packages/inquirer/lib/prompts/rawlist.js +++ b/packages/inquirer/lib/prompts/rawlist.js @@ -4,7 +4,6 @@ */ const _ = { - extend: require('lodash/extend'), isNumber: require('lodash/isNumber'), findIndex: require('lodash/findIndex'), }; @@ -29,7 +28,7 @@ class RawListPrompt extends Base { this.selected = 0; this.rawDefault = 0; - _.extend(this.opt, { + Object.assign(this.opt, { validate(val) { return val != null; }, diff --git a/packages/inquirer/lib/ui/baseUI.js b/packages/inquirer/lib/ui/baseUI.js index 547e56584..917b44d90 100644 --- a/packages/inquirer/lib/ui/baseUI.js +++ b/packages/inquirer/lib/ui/baseUI.js @@ -1,6 +1,5 @@ 'use strict'; const _ = { - extend: require('lodash/extend'), omit: require('lodash/omit'), }; const MuteStream = require('mute-stream'); @@ -86,14 +85,12 @@ function setupReadlineOptions(opt) { ms.pipe(opt.output || process.stdout); const output = ms; - return _.extend( - { - terminal: true, - input, - output, - }, - _.omit(opt, ['input', 'output']) - ); + return { + terminal: true, + input, + output, + ..._.omit(opt, ['input', 'output']), + }; } module.exports = UI; diff --git a/packages/inquirer/test/helpers/readline.js b/packages/inquirer/test/helpers/readline.js index 0c6c2f8df..25a703f8c 100644 --- a/packages/inquirer/test/helpers/readline.js +++ b/packages/inquirer/test/helpers/readline.js @@ -1,11 +1,10 @@ const { EventEmitter } = require('events'); const sinon = require('sinon'); const util = require('util'); -const _ = require('lodash'); const stub = {}; -_.extend(stub, { +Object.assign(stub, { write: sinon.stub().returns(stub), moveCursor: sinon.stub().returns(stub), setPrompt: sinon.stub().returns(stub), @@ -32,6 +31,6 @@ const ReadlineStub = function () { }; util.inherits(ReadlineStub, EventEmitter); -_.assign(ReadlineStub.prototype, stub); +Object.assign(ReadlineStub.prototype, stub); module.exports = ReadlineStub; From c6a43d9d42440a0b710a993b6421e1493b3a5d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:13:09 +0100 Subject: [PATCH 3/8] moved jsdoc to where it belongs --- packages/inquirer/lib/objects/choices.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 917b91962..f7719487c 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -12,11 +12,9 @@ const Choice = require('./choice'); /** * Choices collection * Collection of multiple `choice` object - * @constructor - * @param {Array} choices All `choice` to keep in the collection */ - module.exports = class Choices { + /** @param {Array} choices All `choice` to keep in the collection */ constructor(choices, answers) { this.choices = choices.map((val) => { if (val.type === 'separator') { From 094506b93bcd801e133533594da6357f9d22c5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:15:41 +0100 Subject: [PATCH 4/8] removed lodash find --- packages/inquirer/lib/objects/choices.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index f7719487c..0c42c16db 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -4,7 +4,6 @@ const _ = { isNumber: require('lodash/isNumber'), filter: require('lodash/filter'), map: require('lodash/map'), - find: require('lodash/find'), }; const Separator = require('./separator'); const Choice = require('./choice'); @@ -111,7 +110,7 @@ module.exports = class Choices { } find(func) { - return _.find(this.choices, func); + return this.choices.find(func); } push(...args) { From 12d995d5ef6fbc147191e40d7d423db990023537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:21:04 +0100 Subject: [PATCH 5/8] use native map --- packages/inquirer/lib/objects/choices.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 0c42c16db..8a6c90a6f 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -114,7 +114,7 @@ module.exports = class Choices { } push(...args) { - const objs = _.map(args, (val) => new Choice(val)); + const objs = args.map((val) => new Choice(val)); this.choices.push(...objs); this.realChoices = this.choices .filter(Separator.exclude) From b9c7c679617dcac8601528d9ab71980c7c510f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:25:21 +0100 Subject: [PATCH 6/8] remove lodash isNumber --- packages/inquirer/lib/objects/choice.js | 2 +- packages/inquirer/lib/prompts/expand.js | 2 +- packages/inquirer/lib/prompts/list.js | 4 ++-- packages/inquirer/lib/prompts/rawlist.js | 5 ++--- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index d73dddb02..0bd72bf9f 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -20,7 +20,7 @@ module.exports = class Choice { return val; } - if (typeof val === 'string' || _.isNumber(val)) { + if (typeof val === 'string' || typeof val === 'number') { this.name = String(val); this.value = val; this.short = String(val); diff --git a/packages/inquirer/lib/prompts/expand.js b/packages/inquirer/lib/prompts/expand.js index af7cccfed..a97a168cc 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -233,7 +233,7 @@ class ExpandPrompt extends Base { */ generateChoicesString(choices, defaultChoice) { let defIndex = choices.realLength - 1; - if (_.isNumber(defaultChoice) && this.opt.choices.getChoice(defaultChoice)) { + if (typeof defaultChoice === 'number' && this.opt.choices.getChoice(defaultChoice)) { defIndex = defaultChoice; } else if (typeof defaultChoice === 'string') { const index = _.findIndex( diff --git a/packages/inquirer/lib/prompts/list.js b/packages/inquirer/lib/prompts/list.js index 912457f31..bb168cda0 100644 --- a/packages/inquirer/lib/prompts/list.js +++ b/packages/inquirer/lib/prompts/list.js @@ -31,9 +31,9 @@ class ListPrompt extends Base { const def = this.opt.default; // If def is a Number, then use as index. Otherwise, check for value. - if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) { + if (typeof def === 'number' && def >= 0 && def < this.opt.choices.realLength) { this.selected = def; - } else if (!_.isNumber(def) && def != null) { + } else if (typeof def !== 'number' && def != null) { const index = _.findIndex( this.opt.choices.realChoices, ({ value }) => value === def diff --git a/packages/inquirer/lib/prompts/rawlist.js b/packages/inquirer/lib/prompts/rawlist.js index a018b4ad5..8254ef224 100644 --- a/packages/inquirer/lib/prompts/rawlist.js +++ b/packages/inquirer/lib/prompts/rawlist.js @@ -4,7 +4,6 @@ */ const _ = { - isNumber: require('lodash/isNumber'), findIndex: require('lodash/findIndex'), }; const chalk = require('chalk'); @@ -35,10 +34,10 @@ class RawListPrompt extends Base { }); const def = this.opt.default; - if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) { + if (typeof def === 'number' && def >= 0 && def < this.opt.choices.realLength) { this.selected = def; this.rawDefault = def; - } else if (!_.isNumber(def) && def != null) { + } else if (typeof def !== 'number' && def != null) { const index = _.findIndex( this.opt.choices.realChoices, ({ value }) => value === def From 4b36cadd683b0ff0aee6026e69ecabb647c3cab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:35:56 +0100 Subject: [PATCH 7/8] remove _.findIndex --- packages/inquirer/lib/prompts/expand.js | 6 +----- packages/inquirer/lib/prompts/list.js | 6 +----- packages/inquirer/lib/prompts/rawlist.js | 8 +------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/packages/inquirer/lib/prompts/expand.js b/packages/inquirer/lib/prompts/expand.js index a97a168cc..a431c45e8 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -6,7 +6,6 @@ const _ = { uniq: require('lodash/uniq'), isNumber: require('lodash/isNumber'), - findIndex: require('lodash/findIndex'), }; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); @@ -236,10 +235,7 @@ class ExpandPrompt extends Base { if (typeof defaultChoice === 'number' && this.opt.choices.getChoice(defaultChoice)) { defIndex = defaultChoice; } else if (typeof defaultChoice === 'string') { - const index = _.findIndex( - choices.realChoices, - ({ value }) => value === defaultChoice - ); + const index = choices.realChoices.findIndex(({ value }) => value === defaultChoice); defIndex = index === -1 ? defIndex : index; } diff --git a/packages/inquirer/lib/prompts/list.js b/packages/inquirer/lib/prompts/list.js index bb168cda0..85260f575 100644 --- a/packages/inquirer/lib/prompts/list.js +++ b/packages/inquirer/lib/prompts/list.js @@ -5,7 +5,6 @@ const _ = { isNumber: require('lodash/isNumber'), - findIndex: require('lodash/findIndex'), }; const chalk = require('chalk'); const figures = require('figures'); @@ -34,10 +33,7 @@ class ListPrompt extends Base { if (typeof def === 'number' && def >= 0 && def < this.opt.choices.realLength) { this.selected = def; } else if (typeof def !== 'number' && def != null) { - const index = _.findIndex( - this.opt.choices.realChoices, - ({ value }) => value === def - ); + const index = this.opt.choices.realChoices.findIndex(({ value }) => value === def); this.selected = Math.max(index, 0); } diff --git a/packages/inquirer/lib/prompts/rawlist.js b/packages/inquirer/lib/prompts/rawlist.js index 8254ef224..243fa8c64 100644 --- a/packages/inquirer/lib/prompts/rawlist.js +++ b/packages/inquirer/lib/prompts/rawlist.js @@ -3,9 +3,6 @@ * `rawlist` type prompt */ -const _ = { - findIndex: require('lodash/findIndex'), -}; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); const Base = require('./base'); @@ -38,10 +35,7 @@ class RawListPrompt extends Base { this.selected = def; this.rawDefault = def; } else if (typeof def !== 'number' && def != null) { - const index = _.findIndex( - this.opt.choices.realChoices, - ({ value }) => value === def - ); + const index = this.opt.choices.realChoices.findIndex(({ value }) => value === def); const safeIndex = Math.max(index, 0); this.selected = safeIndex; this.rawDefault = safeIndex; From 4a74e19c35e61fbe52530e19b6840f307547b8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 17 Nov 2021 23:36:44 +0100 Subject: [PATCH 8/8] remove _.isNumber --- packages/inquirer/lib/objects/choice.js | 1 - packages/inquirer/lib/objects/choices.js | 5 ++--- packages/inquirer/lib/prompts/expand.js | 1 - packages/inquirer/lib/prompts/list.js | 3 --- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 0bd72bf9f..25042a9b1 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -1,6 +1,5 @@ 'use strict'; const _ = { - isNumber: require('lodash/isNumber'), isFunction: require('lodash/isFunction'), }; diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 8a6c90a6f..9e89b200c 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); const _ = { - isNumber: require('lodash/isNumber'), filter: require('lodash/filter'), map: require('lodash/map'), }; @@ -57,7 +56,7 @@ module.exports = class Choices { */ getChoice(selector) { - assert(_.isNumber(selector)); + assert(typeof selector === 'number'); return this.realChoices[selector]; } @@ -68,7 +67,7 @@ module.exports = class Choices { */ get(selector) { - assert(_.isNumber(selector)); + assert(typeof selector === 'number'); return this.choices[selector]; } diff --git a/packages/inquirer/lib/prompts/expand.js b/packages/inquirer/lib/prompts/expand.js index a431c45e8..9be3617ee 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -5,7 +5,6 @@ const _ = { uniq: require('lodash/uniq'), - isNumber: require('lodash/isNumber'), }; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); diff --git a/packages/inquirer/lib/prompts/list.js b/packages/inquirer/lib/prompts/list.js index 85260f575..5788a0242 100644 --- a/packages/inquirer/lib/prompts/list.js +++ b/packages/inquirer/lib/prompts/list.js @@ -3,9 +3,6 @@ * `list` type prompt */ -const _ = { - isNumber: require('lodash/isNumber'), -}; const chalk = require('chalk'); const figures = require('figures'); const cliCursor = require('cli-cursor');