diff --git a/packages/core/index.js b/packages/core/index.js index 63fdd8fa9..875cac2ff 100644 --- a/packages/core/index.js +++ b/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'); @@ -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); } @@ -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)(); } @@ -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, diff --git a/packages/core/lib/screen-manager.js b/packages/core/lib/screen-manager.js index 786d1f125..ccfc88d7d 100644 --- a/packages/core/lib/screen-manager.js +++ b/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'); @@ -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) { diff --git a/packages/core/lib/utils.js b/packages/core/lib/utils.js index 1fe34a363..2a744eaaf 100644 --- a/packages/core/lib/utils.js +++ b/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'); }; diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 25042a9b1..46c2364bf 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -1,7 +1,4 @@ 'use strict'; -const _ = { - isFunction: require('lodash/isFunction'), -}; /** * Choice object @@ -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; diff --git a/packages/inquirer/lib/prompts/base.js b/packages/inquirer/lib/prompts/base.js index ec503eeca..81dfb77e4 100644 --- a/packages/inquirer/lib/prompts/base.js +++ b/packages/inquirer/lib/prompts/base.js @@ -4,7 +4,6 @@ * Should be extended by prompt types. */ const _ = { - assign: require('lodash/assign'), defaults: require('lodash/defaults'), clone: require('lodash/clone'), }; @@ -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', }); diff --git a/packages/inquirer/lib/prompts/checkbox.js b/packages/inquirer/lib/prompts/checkbox.js index 4863a6588..64992cc0b 100644 --- a/packages/inquirer/lib/prompts/checkbox.js +++ b/packages/inquirer/lib/prompts/checkbox.js @@ -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'); @@ -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() { diff --git a/packages/inquirer/lib/prompts/confirm.js b/packages/inquirer/lib/prompts/confirm.js index 287279af0..7d628a15b 100644 --- a/packages/inquirer/lib/prompts/confirm.js +++ b/packages/inquirer/lib/prompts/confirm.js @@ -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'); @@ -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'; diff --git a/packages/inquirer/lib/prompts/expand.js b/packages/inquirer/lib/prompts/expand.js index 9be3617ee..3afed1bbe 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -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'); @@ -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(',') ); } } diff --git a/packages/inquirer/lib/ui/baseUI.js b/packages/inquirer/lib/ui/baseUI.js index 917b44d90..037d6ddf2 100644 --- a/packages/inquirer/lib/ui/baseUI.js +++ b/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'); @@ -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; @@ -87,9 +83,9 @@ function setupReadlineOptions(opt) { return { terminal: true, + ...opt, input, output, - ..._.omit(opt, ['input', 'output']), }; } diff --git a/packages/inquirer/lib/ui/bottom-bar.js b/packages/inquirer/lib/ui/bottom-bar.js index 9235273ec..3da44d7d2 100644 --- a/packages/inquirer/lib/ui/bottom-bar.js +++ b/packages/inquirer/lib/ui/bottom-bar.js @@ -11,9 +11,7 @@ const _ = { }; class BottomBar extends Base { - constructor(opt) { - opt = opt || {}; - + constructor(opt = {}) { super(opt); this.log = through(this.writeLog.bind(this)); @@ -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) { diff --git a/packages/inquirer/lib/ui/prompt.js b/packages/inquirer/lib/ui/prompt.js index 0d0e3ea26..82e313226 100644 --- a/packages/inquirer/lib/ui/prompt.js +++ b/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'); @@ -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 = {}; } @@ -79,7 +77,7 @@ class PromptUI extends Base { } processQuestion(question) { - question = _.clone(question); + question = { ...question }; return defer(() => { const obs = of(question); @@ -129,7 +127,7 @@ class PromptUI extends Base { return empty(); } - if (!_.isFunction(question.when)) { + if (typeof question.when !== 'function') { return of(question); } diff --git a/packages/inquirer/lib/utils/paginator.js b/packages/inquirer/lib/utils/paginator.js index 767bf6729..6c2e8549e 100644 --- a/packages/inquirer/lib/utils/paginator.js +++ b/packages/inquirer/lib/utils/paginator.js @@ -1,9 +1,5 @@ 'use strict'; -const _ = { - sum: require('lodash/sum'), - flatten: require('lodash/flatten'), -}; const chalk = require('chalk'); /** @@ -11,6 +7,10 @@ const chalk = require('chalk'); */ class Paginator { + /** + * @param {import("./screen-manager")} [screen] + * @param {{isInfinite?: boolean}} [options] + */ constructor(screen, options = {}) { const { isInfinite = true } = options; this.lastIndex = 0; @@ -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 @@ -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); diff --git a/packages/inquirer/lib/utils/screen-manager.js b/packages/inquirer/lib/utils/screen-manager.js index 12412c643..9fd1c1b71 100644 --- a/packages/inquirer/lib/utils/screen-manager.js +++ b/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'); @@ -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 { @@ -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 @@ -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'); } } diff --git a/packages/inquirer/lib/utils/utils.js b/packages/inquirer/lib/utils/utils.js index 921175105..40b125010 100644 --- a/packages/inquirer/lib/utils/utils.js +++ b/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'); @@ -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); }