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/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 22d8a9b39..25042a9b1 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -1,7 +1,5 @@ 'use strict'; const _ = { - isNumber: require('lodash/isNumber'), - extend: require('lodash/extend'), isFunction: require('lodash/isFunction'), }; @@ -21,12 +19,12 @@ 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); } 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/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 917b91962..9e89b200c 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -1,10 +1,8 @@ 'use strict'; const assert = require('assert'); 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'); @@ -12,11 +10,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') { @@ -60,7 +56,7 @@ module.exports = class Choices { */ getChoice(selector) { - assert(_.isNumber(selector)); + assert(typeof selector === 'number'); return this.realChoices[selector]; } @@ -71,7 +67,7 @@ module.exports = class Choices { */ get(selector) { - assert(_.isNumber(selector)); + assert(typeof selector === 'number'); return this.choices[selector]; } @@ -113,11 +109,11 @@ module.exports = class Choices { } find(func) { - return _.find(this.choices, func); + return this.choices.find(func); } 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) 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/expand.js b/packages/inquirer/lib/prompts/expand.js index af7cccfed..9be3617ee 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -5,8 +5,6 @@ const _ = { uniq: require('lodash/uniq'), - isNumber: require('lodash/isNumber'), - findIndex: require('lodash/findIndex'), }; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); @@ -233,13 +231,10 @@ 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( - 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 912457f31..5788a0242 100644 --- a/packages/inquirer/lib/prompts/list.js +++ b/packages/inquirer/lib/prompts/list.js @@ -3,10 +3,6 @@ * `list` type prompt */ -const _ = { - isNumber: require('lodash/isNumber'), - findIndex: require('lodash/findIndex'), -}; const chalk = require('chalk'); const figures = require('figures'); const cliCursor = require('cli-cursor'); @@ -31,13 +27,10 @@ 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) { - const index = _.findIndex( - this.opt.choices.realChoices, - ({ value }) => value === def - ); + } else if (typeof def !== 'number' && def != null) { + 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 06036eafb..243fa8c64 100644 --- a/packages/inquirer/lib/prompts/rawlist.js +++ b/packages/inquirer/lib/prompts/rawlist.js @@ -3,11 +3,6 @@ * `rawlist` type prompt */ -const _ = { - extend: require('lodash/extend'), - isNumber: require('lodash/isNumber'), - findIndex: require('lodash/findIndex'), -}; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); const Base = require('./base'); @@ -29,21 +24,18 @@ class RawListPrompt extends Base { this.selected = 0; this.rawDefault = 0; - _.extend(this.opt, { + Object.assign(this.opt, { validate(val) { return val != null; }, }); 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) { - const index = _.findIndex( - this.opt.choices.realChoices, - ({ value }) => value === def - ); + } else if (typeof def !== 'number' && def != null) { + const index = this.opt.choices.realChoices.findIndex(({ value }) => value === def); const safeIndex = Math.max(index, 0); this.selected = safeIndex; this.rawDefault = safeIndex; 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; 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); });