Skip to content

Commit

Permalink
feat: move to individual lodash packages or standard JS (#1440)
Browse files Browse the repository at this point in the history
* feat: move to individual lodash packages

Moves to using individual lodash imports to reduce package size.

* Chore(inquirer): Remove lodash clone/defaults dependencies

* Chore(inquirer): Remove lodash.filter dependencies

* Chore(inquirer): Remove lodash.isplainobject dependency

---------

Co-authored-by: Simon Boudrias <admin@simonboudrias.com>
  • Loading branch information
43081j and SBoudrias authored Jun 26, 2024
1 parent 290bf73 commit 6b307b9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 35 deletions.
Binary file not shown.
Binary file not shown.
16 changes: 11 additions & 5 deletions packages/inquirer/lib/objects/choices.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import assert from 'node:assert';
import filter from 'lodash/filter.js';
import map from 'lodash/map.js';

import Separator from './separator.js';
import Choice from './choice.js';
Expand Down Expand Up @@ -78,11 +76,19 @@ export default class Choices {

/**
* Match the valid choices against a where clause
* @param {Object} whereClause Lodash `where` clause
* @param {Function|Object} whereClause filter function or key-value object to match against
* @return {Array} Matching choices or empty array
*/
where(whereClause) {
return filter(this.realChoices, whereClause);
let filterFn;
if (typeof whereClause === 'function') {
filterFn = whereClause;
} else {
const [key, value] = Object.entries(whereClause)[0];
filterFn = (choice) => choice[key] === value;
}

return this.realChoices.filter(filterFn);
}

/**
Expand All @@ -91,7 +97,7 @@ export default class Choices {
* @return {Array} Selected properties
*/
pluck(propertyName) {
return map(this.realChoices, propertyName);
return this.realChoices.map((choice) => choice[propertyName]);
}

// Expose usual Array methods
Expand Down
12 changes: 3 additions & 9 deletions packages/inquirer/lib/prompts/base.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import defaults from 'lodash/defaults.js';
import clone from 'lodash/clone.js';
/**
* Base prompt implementation
* Should be extended by prompt types.
*/
const _ = {
defaults,
clone,
};

import pc from 'picocolors';
import runAsync from 'run-async';
import { filter, flatMap, share, take, takeUntil } from 'rxjs';
Expand All @@ -24,7 +17,7 @@ export default class Prompt {
});

// Set defaults prompt options
this.opt = _.defaults(_.clone(question), {
this.opt = {
validate: () => true,
validatingText: '',
filter: (val) => val,
Expand All @@ -33,7 +26,8 @@ export default class Prompt {
suffix: '',
prefix: pc.green('?'),
transformer: (val) => val,
});
...question,
};

// Make sure name is present
if (!this.opt.name) {
Expand Down
56 changes: 37 additions & 19 deletions packages/inquirer/lib/ui/prompt.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import isPlainObject from 'lodash/isPlainObject.js';
import get from 'lodash/get.js';
import set from 'lodash/set.js';
import get from 'lodash.get';
import set from 'lodash.set';

const _ = {
isPlainObject,
set,
get,
};
import { defer, empty, from, of, concatMap, filter, publish, reduce } from 'rxjs';
import {
defer,
empty,
from,
of,
concatMap,
filter,
publish,
reduce,
isObservable,
} from 'rxjs';
import runAsync from 'run-async';
import * as utils from '../utils/utils.js';
import Base from './baseUI.js';
Expand All @@ -23,23 +31,33 @@ export default class PromptUI extends Base {

run(questions, answers) {
// Keep global reference to the answers
this.answers = _.isPlainObject(answers) ? { ...answers } : {};

// Make sure questions is an array.
if (_.isPlainObject(questions)) {
// It's either an object of questions or a single question
questions = Object.values(questions).every(
(v) => _.isPlainObject(v) && v.name === undefined,
this.answers = typeof answers === 'object' ? { ...answers } : {};

let obs;
if (Array.isArray(questions)) {
obs = from(questions);
} else if (isObservable(questions)) {
obs = questions;
} else if (
Object.values(questions).every(
(maybeQuestion) =>
typeof maybeQuestion === 'object' &&
!Array.isArray(maybeQuestion) &&
maybeQuestion != null,
)
? Object.entries(questions).map(([name, question]) => ({ name, ...question }))
: [questions];
) {
// Case: Called with a set of { name: question }
obs = from(
Object.entries(questions).map(([name, question]) => ({
name,
...question,
})),
);
} else {
// Case: Called with a single question config
obs = from([questions]);
}

// Create an observable, unless we received one as parameter.
// Note: As this is a public interface, we cannot do an instanceof check as we won't
// be using the exact same object in memory.
const obs = Array.isArray(questions) ? from(questions) : questions;

this.process = obs.pipe(
concatMap(this.processQuestion.bind(this)),
publish(), // Creates a hot Observable. It prevents duplicating prompts.
Expand Down
3 changes: 2 additions & 1 deletion packages/inquirer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"ansi-escapes": "^4.3.2",
"cli-width": "^4.1.0",
"external-editor": "^3.1.0",
"lodash": "^4.17.21",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"mute-stream": "1.0.0",
"ora": "^5.4.1",
"picocolors": "^1.0.1",
Expand Down
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4249,7 +4249,8 @@ __metadata:
ansi-escapes: "npm:^4.3.2"
cli-width: "npm:^4.1.0"
external-editor: "npm:^3.1.0"
lodash: "npm:^4.17.21"
lodash.get: "npm:^4.4.2"
lodash.set: "npm:^4.3.2"
mute-stream: "npm:1.0.0"
ora: "npm:^5.4.1"
picocolors: "npm:^1.0.1"
Expand Down Expand Up @@ -4972,6 +4973,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.get@npm:^4.4.2":
version: 4.4.2
resolution: "lodash.get@npm:4.4.2"
checksum: 10/2a4925f6e89bc2c010a77a802d1ba357e17ed1ea03c2ddf6a146429f2856a216663e694a6aa3549a318cbbba3fd8b7decb392db457e6ac0b83dc745ed0a17380
languageName: node
linkType: hard

"lodash.ismatch@npm:^4.4.0":
version: 4.4.0
resolution: "lodash.ismatch@npm:4.4.0"
Expand All @@ -4986,6 +4994,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.set@npm:^4.3.2":
version: 4.3.2
resolution: "lodash.set@npm:4.3.2"
checksum: 10/f0968109bca5625c8ce1f1beab758634484443604d3950477e46d8d2631562e5ceae4465b9ce8a393fd47f5a411329f9bacf956c7c95530af1290db1a20343ba
languageName: node
linkType: hard

"lodash@npm:^4.17.21":
version: 4.17.21
resolution: "lodash@npm:4.17.21"
Expand Down

0 comments on commit 6b307b9

Please sign in to comment.