Skip to content

Commit

Permalink
Remove more lodash (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Nov 18, 2021
1 parent 3a796d8 commit 32854c0
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 75 deletions.
9 changes: 3 additions & 6 deletions packages/core/index.js
@@ -1,6 +1,3 @@
const _ = {
isFunction: require('lodash/isFunction'),
};
const readline = require('readline');
const chalk = require('chalk');
const MuteStream = require('mute-stream');
Expand Down Expand Up @@ -49,7 +46,7 @@ class StateManager {
this.screen = new ScreenManager(this.rl);

let config = configFactory;
if (_.isFunction(configFactory)) {
if (typeof configFactory === 'function') {
config = configFactory(this.rl);
}

Expand All @@ -69,7 +66,7 @@ class StateManager {

// Load asynchronous properties
const showLoader = setTimeout(this.startLoading, 500);
if (_.isFunction(message)) {
if (typeof message === 'function') {
message = await runAsync(message)();
}

Expand Down Expand Up @@ -196,7 +193,7 @@ class StateManager {
prefix: this.getPrefix(),
...state,
// Only pass message down if it's a string. Otherwise we're still in init state
message: _.isFunction(message) ? 'Loading...' : message,
message: typeof message === 'function' ? 'Loading...' : message,
value: transformer(value, { isFinal: status === 'done' }),
validate: undefined,
filter: undefined,
Expand Down
5 changes: 1 addition & 4 deletions packages/core/lib/screen-manager.js
@@ -1,6 +1,3 @@
const _ = {
last: require('lodash/last'),
};
const cliWidth = require('cli-width');
const stripAnsi = require('strip-ansi');
const stringWidth = require('string-width');
Expand All @@ -9,7 +6,7 @@ const util = require('./readline');
const { breakLines } = require('./utils');

const height = (content) => content.split('\n').length;
const lastLine = (content) => _.last(content.split('\n'));
const lastLine = (content) => content.split('\n').pop();

module.exports = class ScreenManager {
constructor(rl) {
Expand Down
13 changes: 5 additions & 8 deletions packages/core/lib/utils.js
@@ -1,22 +1,19 @@
const _ = {
flatten: require('lodash/flatten'),
};

/**
* Force line returns at specific width. This function is ANSI code friendly and it'll
* ignore invisible codes during width calculation.
* @param {string} lines
* @param {string} content
* @param {number} width
* @return {string}
*/
exports.breakLines = (content, width) => {
const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g');
return _.flatten(
content.split('\n').map((line) => {
return content
.split('\n')
.flatMap((line) => {
const chunk = line.match(regex);
// Remove the last match as it's always empty
chunk.pop();
return chunk || '';
})
).join('\n');
.join('\n');
};
5 changes: 1 addition & 4 deletions packages/inquirer/lib/objects/choice.js
@@ -1,7 +1,4 @@
'use strict';
const _ = {
isFunction: require('lodash/isFunction'),
};

/**
* Choice object
Expand Down Expand Up @@ -31,7 +28,7 @@ module.exports = class Choice {
});
}

if (_.isFunction(val.disabled)) {
if (typeof val.disabled === 'function') {
this.disabled = val.disabled(answers);
} else {
this.disabled = val.disabled;
Expand Down
3 changes: 1 addition & 2 deletions packages/inquirer/lib/prompts/base.js
Expand Up @@ -4,7 +4,6 @@
* Should be extended by prompt types.
*/
const _ = {
assign: require('lodash/assign'),
defaults: require('lodash/defaults'),
clone: require('lodash/clone'),
};
Expand All @@ -17,7 +16,7 @@ const ScreenManager = require('../utils/screen-manager');
class Prompt {
constructor(question, rl, answers) {
// Setup instance defaults property
_.assign(this, {
Object.assign(this, {
answers,
status: 'pending',
});
Expand Down
7 changes: 2 additions & 5 deletions packages/inquirer/lib/prompts/checkbox.js
Expand Up @@ -3,9 +3,6 @@
* `list` type prompt
*/

const _ = {
map: require('lodash/map'),
};
const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const figures = require('figures');
Expand Down Expand Up @@ -167,8 +164,8 @@ class CheckboxPrompt extends Base {
(choice) => Boolean(choice.checked) && !choice.disabled
);

this.selection = _.map(choices, 'short');
return _.map(choices, 'value');
this.selection = choices.map((choice) => choice.short);
return choices.map((choice) => choice.value);
}

onUpKey() {
Expand Down
7 changes: 2 additions & 5 deletions packages/inquirer/lib/prompts/confirm.js
Expand Up @@ -3,9 +3,6 @@
* `confirm` type prompt
*/

const _ = {
isBoolean: require('lodash/isBoolean'),
};
const chalk = require('chalk');
const { take, takeUntil } = require('rxjs/operators');
const Base = require('./base');
Expand All @@ -28,8 +25,8 @@ class ConfirmPrompt extends Base {
},
});

if (_.isBoolean(this.opt.default)) {
rawDefault = this.opt.default;
if (this.opt.default != null) {
rawDefault = Boolean(this.opt.default);
}

this.opt.default = rawDefault ? 'Y/n' : 'y/N';
Expand Down
5 changes: 1 addition & 4 deletions packages/inquirer/lib/prompts/expand.js
Expand Up @@ -3,9 +3,6 @@
* `rawlist` type prompt
*/

const _ = {
uniq: require('lodash/uniq'),
};
const chalk = require('chalk');
const { map, takeUntil } = require('rxjs/operators');
const Base = require('./base');
Expand Down Expand Up @@ -218,7 +215,7 @@ class ExpandPrompt extends Base {
if (errors.length) {
throw new Error(
'Duplicate key error: `key` param must be unique. Duplicates: ' +
_.uniq(errors).join(', ')
[...new Set(errors)].join(',')
);
}
}
Expand Down
8 changes: 2 additions & 6 deletions packages/inquirer/lib/ui/baseUI.js
@@ -1,7 +1,4 @@
'use strict';
const _ = {
omit: require('lodash/omit'),
};
const MuteStream = require('mute-stream');
const readline = require('readline');

Expand Down Expand Up @@ -61,8 +58,7 @@ class UI {
}
}

function setupReadlineOptions(opt) {
opt = opt || {};
function setupReadlineOptions(opt = {}) {
// Inquirer 8.x:
// opt.skipTTYChecks = opt.skipTTYChecks === undefined ? opt.input !== undefined : opt.skipTTYChecks;
opt.skipTTYChecks = opt.skipTTYChecks === undefined ? true : opt.skipTTYChecks;
Expand All @@ -87,9 +83,9 @@ function setupReadlineOptions(opt) {

return {
terminal: true,
...opt,
input,
output,
..._.omit(opt, ['input', 'output']),
};
}

Expand Down
5 changes: 1 addition & 4 deletions packages/inquirer/lib/ui/bottom-bar.js
Expand Up @@ -11,9 +11,7 @@ const _ = {
};

class BottomBar extends Base {
constructor(opt) {
opt = opt || {};

constructor(opt = {}) {
super(opt);

this.log = through(this.writeLog.bind(this));
Expand Down Expand Up @@ -79,7 +77,6 @@ class BottomBar extends Base {

/**
* Helper for writing message in Prompt
* @param {BottomBar} prompt - The Prompt object that extends tty
* @param {String} message - The message to be output
*/
write(message) {
Expand Down
8 changes: 3 additions & 5 deletions packages/inquirer/lib/ui/prompt.js
@@ -1,10 +1,8 @@
'use strict';
const _ = {
isPlainObject: require('lodash/isPlainObject'),
clone: require('lodash/clone'),
get: require('lodash/get'),
set: require('lodash/set'),
isFunction: require('lodash/isFunction'),
};
const { defer, empty, from, of } = require('rxjs');
const { concatMap, filter, publish, reduce } = require('rxjs/operators');
Expand All @@ -25,7 +23,7 @@ class PromptUI extends Base {
run(questions, answers) {
// Keep global reference to the answers
if (_.isPlainObject(answers)) {
this.answers = _.clone(answers);
this.answers = { ...answers };
} else {
this.answers = {};
}
Expand Down Expand Up @@ -79,7 +77,7 @@ class PromptUI extends Base {
}

processQuestion(question) {
question = _.clone(question);
question = { ...question };
return defer(() => {
const obs = of(question);

Expand Down Expand Up @@ -129,7 +127,7 @@ class PromptUI extends Base {
return empty();
}

if (!_.isFunction(question.when)) {
if (typeof question.when !== 'function') {
return of(question);
}

Expand Down
17 changes: 10 additions & 7 deletions packages/inquirer/lib/utils/paginator.js
@@ -1,16 +1,16 @@
'use strict';

const _ = {
sum: require('lodash/sum'),
flatten: require('lodash/flatten'),
};
const chalk = require('chalk');

/**
* The paginator returns a subset of the choices if the list is too long.
*/

class Paginator {
/**
* @param {import("./screen-manager")} [screen]
* @param {{isInfinite?: boolean}} [options]
*/
constructor(screen, options = {}) {
const { isInfinite = true } = options;
this.lastIndex = 0;
Expand All @@ -24,8 +24,11 @@ class Paginator {

if (this.screen) {
lines = this.screen.breakLines(lines);
active = _.sum(lines.map((lineParts) => lineParts.length).splice(0, active));
lines = _.flatten(lines);
active = lines
.map((lineParts) => lineParts.length)
.splice(0, active)
.reduce((a, b) => a + b, 0);
lines = lines.flat();
}

// Make sure there's enough lines to paginate
Expand Down Expand Up @@ -58,7 +61,7 @@ class Paginator {
}

// Duplicate the lines so it give an infinite list look
const infinite = _.flatten([lines, lines, lines]);
const infinite = [lines, lines, lines].flat();
const topIndex = Math.max(0, active + lines.length - this.pointer);

return infinite.splice(topIndex, pageSize);
Expand Down
23 changes: 12 additions & 11 deletions packages/inquirer/lib/utils/screen-manager.js
@@ -1,8 +1,4 @@
'use strict';
const _ = {
last: require('lodash/last'),
flatten: require('lodash/flatten'),
};
const util = require('./readline');
const cliWidth = require('cli-width');
const stripAnsi = require('strip-ansi');
Expand All @@ -13,8 +9,9 @@ function height(content) {
return content.split('\n').length;
}

/** @param {string} content */
function lastLine(content) {
return _.last(content.split('\n'));
return content.split('\n').pop();
}

class ScreenManager {
Expand Down Expand Up @@ -153,11 +150,13 @@ class ScreenManager {
return width;
}

breakLines(lines, width) {
/**
* @param {string[]} lines
*/
breakLines(lines, width = this.normalizedCliWidth()) {
// Break lines who're longer than the cli width so we can normalize the natural line
// returns behavior across terminals.
width = width || this.normalizedCliWidth();
const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g');
const regex = new RegExp(`(?:(?:\\033[[0-9;]*m)*.?){1,${width}}`, 'g');
return lines.map((line) => {
const chunk = line.match(regex);
// Last match is always empty
Expand All @@ -166,9 +165,11 @@ class ScreenManager {
});
}

forceLineReturn(content, width) {
width = width || this.normalizedCliWidth();
return _.flatten(this.breakLines(content.split('\n'), width)).join('\n');
/**
* @param {string} content
*/
forceLineReturn(content, width = this.normalizedCliWidth()) {
return this.breakLines(content.split('\n'), width).flat().join('\n');
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/inquirer/lib/utils/utils.js
@@ -1,7 +1,4 @@
'use strict';
const _ = {
isFunction: require('lodash/isFunction'),
};
const { from, of } = require('rxjs');
const runAsync = require('run-async');

Expand All @@ -15,7 +12,7 @@ const runAsync = require('run-async');
*/

exports.fetchAsyncQuestionProperty = function (question, prop, answers) {
if (!_.isFunction(question[prop])) {
if (typeof question[prop] !== 'function') {
return of(question);
}

Expand Down

0 comments on commit 32854c0

Please sign in to comment.