Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove more lodash stuff #1067

Merged
merged 8 commits into from Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions 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');
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 2 additions & 4 deletions 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'),
};

Expand All @@ -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,
Expand Down
14 changes: 5 additions & 9 deletions packages/inquirer/lib/objects/choices.js
@@ -1,22 +1,18 @@
'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');

/**
* 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') {
Expand Down Expand Up @@ -60,7 +56,7 @@ module.exports = class Choices {
*/

getChoice(selector) {
assert(_.isNumber(selector));
assert(typeof selector === 'number');
return this.realChoices[selector];
}

Expand All @@ -71,7 +67,7 @@ module.exports = class Choices {
*/

get(selector) {
assert(_.isNumber(selector));
assert(typeof selector === 'number');
return this.choices[selector];
}

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions packages/inquirer/lib/prompts/confirm.js
Expand Up @@ -4,7 +4,6 @@
*/

const _ = {
extend: require('lodash/extend'),
isBoolean: require('lodash/isBoolean'),
};
const chalk = require('chalk');
Expand All @@ -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 !== '') {
Expand Down
9 changes: 2 additions & 7 deletions packages/inquirer/lib/prompts/expand.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 3 additions & 10 deletions packages/inquirer/lib/prompts/list.js
Expand Up @@ -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');
Expand All @@ -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);
}

Expand Down
16 changes: 4 additions & 12 deletions packages/inquirer/lib/prompts/rawlist.js
Expand Up @@ -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');
Expand All @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions 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');
Expand Down Expand Up @@ -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;
5 changes: 2 additions & 3 deletions 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),
Expand All @@ -32,6 +31,6 @@ const ReadlineStub = function () {
};

util.inherits(ReadlineStub, EventEmitter);
_.assign(ReadlineStub.prototype, stub);
Object.assign(ReadlineStub.prototype, stub);

module.exports = ReadlineStub;
3 changes: 1 addition & 2 deletions packages/inquirer/test/specs/inquirer.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
});
Expand Down