Skip to content

Commit

Permalink
refactor: change the way of lodash import (#1582)
Browse files Browse the repository at this point in the history
- use specific import from submodules
- for bedder readibility
- for possibility of treeshaking
  • Loading branch information
m0dh4x authored Jan 8, 2025
1 parent 65e0574 commit dd4d796
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 44 deletions.
4 changes: 2 additions & 2 deletions app/domains/beratungshilfe/formular/xstateConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import merge from "lodash/merge";
import persoenlicheDatenFlow from "~/domains/shared/formular/persoenlicheDaten/flow.json";
import type { Config } from "~/services/flow/server/buildFlowController";
import { abgabeXstateConfig } from "./abgabe/xstateConfig";
Expand Down Expand Up @@ -38,7 +38,7 @@ export const beratungshilfeXstateConfig = {
"anwaltliche-vertretung": anwaltlicheVertretungXstateConfig,
rechtsproblem: rechtsproblemXstateConfig,
"finanzielle-angaben": beratungshilfeFinanzielleAngabenXstateConfig,
"persoenliche-daten": _.merge(persoenlicheDatenFlow, {
"persoenliche-daten": merge(persoenlicheDatenFlow, {
meta: { done: beratungshilfePersoenlicheDatenDone },
states: {
start: {
Expand Down
16 changes: 8 additions & 8 deletions app/domains/fluggastrechte/formular/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import merge from "lodash/merge";
import type { AllContextKeys } from "~/domains/common";
import type { Flow } from "~/domains/flows.server";
import type { ArrayConfigServer } from "~/services/array";
Expand Down Expand Up @@ -116,26 +116,26 @@ export const fluggastrechtFlow = {
"redirect-vorabcheck-ergebnis": { on: {} },
},
},
grundvoraussetzungen: _.merge(grundvoraussetzungenFlow, {
grundvoraussetzungen: merge(grundvoraussetzungenFlow, {
meta: { done: grundvoraussetzungenDone },
}),
"streitwert-kosten": _.merge(streitwertKostenFlow, {
"streitwert-kosten": merge(streitwertKostenFlow, {
meta: { done: streitwertKostenDone },
}),
flugdaten: _.merge(flugdatenFlow, { meta: { done: flugdatenDone } }),
"persoenliche-daten": _.merge(persoenlicheDatenFlow, {
flugdaten: merge(flugdatenFlow, { meta: { done: flugdatenDone } }),
"persoenliche-daten": merge(persoenlicheDatenFlow, {
states: {
person: { meta: { done: personDone } },
"weitere-personen": { meta: { done: weiterePersonenDone } },
},
}),
prozessfuehrung: _.merge(prozessfuehrungFlow, {
prozessfuehrung: merge(prozessfuehrungFlow, {
meta: { done: prozessfuehrungDone },
}),
zusammenfassung: _.merge(zusammenfassungFlow, {
zusammenfassung: merge(zusammenfassungFlow, {
meta: { done: () => false },
}),
abgabe: _.merge(abgabeFlow, {
abgabe: merge(abgabeFlow, {
meta: { done: () => false },
}),
},
Expand Down
7 changes: 4 additions & 3 deletions app/domains/geldEinklagen/formular/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from "lodash";
import merge from "lodash/merge";
import cloneDeep from "lodash/cloneDeep";

Check warning on line 2 in app/domains/geldEinklagen/formular/index.ts

View workflow job for this annotation

GitHub Actions / code-quality / npm run lint

`lodash/cloneDeep` import should occur before import of `lodash/merge`
import type { Flow } from "~/domains/flows.server";
import type { GeldEinklagenFormularContext } from "./context";
import geldEinklagenFormularFlow from "./flow.json";
Expand Down Expand Up @@ -27,9 +28,9 @@ export const geldEinklagenFormular = {
berechneteGerichtskosten: berechneteGerichtskosten.toString(),
};
},
config: _.merge(geldEinklagenFormularFlow, {
config: merge(geldEinklagenFormularFlow, {
states: {
"persoenliche-daten": _.merge(_.cloneDeep(persoenlicheDatenFlow), {
"persoenliche-daten": merge(cloneDeep(persoenlicheDatenFlow), {
meta: {
done: ({ context }: { context: GeldEinklagenFormularContext }) =>
Boolean(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import mapValues from "lodash/mapValues";
import { and } from "xstate";
import type { Context } from "~/domains/contexts";
import type { Flow } from "~/domains/flows.server";
Expand Down Expand Up @@ -64,7 +64,7 @@ export const getProzesskostenhilfeEinkuenfteSubflow = (
_doneFunction: ProzesskostenhilfeFinanzielleAngabenEinkuenfteGuard = einkuenfteDone,
subflowPrefix?: PKHEinkuenfteSubflowTypes,
) => {
const stepIds = _.mapValues(einkuenfteStepIds, (step) =>
const stepIds = mapValues(einkuenfteStepIds, (step) =>
subflowPrefix ? `${subflowPrefix}-${step}` : step,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import merge from "lodash/merge";
import type { Config } from "~/services/flow/server/buildFlowController";
import type { ProzesskostenhilfeFinanzielleAngabenContext } from "./context";
import {
Expand Down Expand Up @@ -99,7 +99,7 @@ export const finanzielleAngabenXstateConfig = {
],
},
},
"partner-einkuenfte": _.merge(
"partner-einkuenfte": merge(
getProzesskostenhilfeEinkuenfteSubflow(einkuenfteDone, "partner"),
{
states: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import partition from "lodash/partition";
import type { ProzesskostenhilfePDF } from "data/pdf/prozesskostenhilfe/prozesskostenhilfe.generated";
import type { ProzesskostenhilfeFormularContext } from "~/domains/prozesskostenhilfe/formular";
import { finanzielleAngabeEinkuenfteGuards as einkuenfteGuards } from "~/domains/prozesskostenhilfe/formular/finanzielleAngaben/einkuenfte/guards";
Expand Down Expand Up @@ -187,7 +187,7 @@ export const fillZahlungsverpflichtungen: PkhPdfFillFunction = ({
...versicherungen.map(mapVersicherungToZahlungsverpflichtung),
];

const [zahlungLong, zahlungShort] = _.partition(
const [zahlungLong, zahlungShort] = partition(
zahlungenWithDescription,
(zahlung) =>
zahlung.description.length >
Expand Down
4 changes: 2 additions & 2 deletions app/routes/shared/pdfDownloadLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { redirect, type LoaderFunctionArgs } from "@remix-run/node";
import _ from "lodash";
import isEmpty from "lodash/isEmpty";
import type { BeratungshilfeFormularContext } from "~/domains/beratungshilfe/formular";
import { beratungshilfePdfFromUserdata } from "~/domains/beratungshilfe/services/pdf";
import { parsePathname, type FlowId } from "~/domains/flowIds";
Expand Down Expand Up @@ -67,7 +67,7 @@ export async function pdfDownloadLoader({ request }: LoaderFunctionArgs) {
(await getSessionData(flowId, request.headers.get("Cookie"))).userData,
flowId,
);
if (_.isEmpty(userData)) return redirect(flowId);
if (isEmpty(userData)) return redirect(flowId);

const fileContent = await pdfFunction(userData, flowTranslations);
const fileSize = fileContent.length;
Expand Down
4 changes: 2 additions & 2 deletions app/services/array/resolveArraysFromKeys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import set from "lodash/set";
import type { Context } from "~/domains/contexts";
import { resolveArrayCharacter } from "./resolveArrayCharacter";

Expand All @@ -8,7 +8,7 @@ export const resolveArraysFromKeys = (
) => {
const resolvedObject: Context = {};
Object.entries(data).forEach(([key, value]) =>
_.set(resolvedObject, resolveArrayCharacter(key, arrayIndexes), value),
set(resolvedObject, resolveArrayCharacter(key, arrayIndexes), value),
);
return resolvedObject;
};
4 changes: 2 additions & 2 deletions app/services/cms/fetchAllFormFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import merge from "lodash/merge";
import type { FlowId } from "~/domains/flowIds";
import { flows } from "~/domains/flows.server";
import { flowPageApiIdFromFlowType } from "./apiFromFlowType";
Expand Down Expand Up @@ -29,7 +29,7 @@ export async function fetchAllFormFields(
)
: {};

return _.merge(formFields, formFieldsStaging);
return merge(formFields, formFieldsStaging);
}

function formFieldsFromEntries(
Expand Down
4 changes: 2 additions & 2 deletions app/services/flow/pageData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import merge from "lodash/merge";
import type { Context } from "~/domains/contexts";
import { type PageData } from "./pageDataSchema";

export function addPageDataToUserData(userData: Context, pageData: PageData) {
return _.merge(userData, { pageData });
return merge(userData, { pageData });
}
4 changes: 2 additions & 2 deletions app/services/flow/pruner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import pick from "lodash/pick";
import type { Context } from "~/domains/contexts";
import type { FlowId } from "~/domains/flowIds";
import { flows } from "~/domains/flows.server";
Expand All @@ -16,7 +16,7 @@ export async function pruneIrrelevantData(data: Context, flowId: FlowId) {
const flowController = buildFlowController({ guards, config, data });
const formPaths = validFormPaths(flowController);
const validFormFields = filterFormFields(formFields, formPaths);
return _.pick(data, validFormFields);
return pick(data, validFormFields);
}

export function filterFormFields(
Expand Down
4 changes: 2 additions & 2 deletions app/services/flow/server/buildFlowController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getShortestPaths } from "@xstate/graph";
import _ from "lodash";
import isEqual from "lodash/isEqual";
import type {
MachineConfig,
MachineContext,
Expand Down Expand Up @@ -117,7 +117,7 @@ const findNode = (machine: FlowStateMachine, stepId: string) => {
value: pathToStateValue(statepath),
context: {},
});
return resolvedState._nodes.find((node) => _.isEqual(node.path, statepath));
return resolvedState._nodes.find((node) => isEqual(node.path, statepath));
};

const isFinalStep = (machine: FlowStateMachine, stepId: string) => {
Expand Down
4 changes: 2 additions & 2 deletions app/services/flow/server/flowTransitionValidation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import isEmpty from "lodash/isEmpty";
import type { FlowId } from "~/domains/flowIds";
import type { Flow } from "~/domains/flows.server";
import { buildFlowController } from "./buildFlowController";
Expand Down Expand Up @@ -27,7 +27,7 @@ export async function validateFlowTransition(
): Promise<FlowTransitionResult> {
const { sourceFlowId, eligibleSourcePages } = config;

if (_.isEmpty(eligibleSourcePages)) {
if (isEmpty(eligibleSourcePages)) {
throw Error("This property should not be empty");
}

Expand Down
5 changes: 3 additions & 2 deletions app/services/session.server/fieldsFromContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from "lodash";
import zipObject from "lodash/zipObject";
import at from "lodash/at";
import type { Context } from "~/domains/contexts";
import { resolveArrayCharacter } from "~/services/array/resolveArrayCharacter";
import { pageDataSchema } from "~/services/flow/pageDataSchema";
Expand All @@ -12,5 +13,5 @@ export const fieldsFromContext = (context: Context, fieldnames: string[]) => {
const resolvedFieldnames = fieldnames.map((fieldname) =>
resolveArrayCharacter(fieldname, arrayIndexes),
);
return _.zipObject(fieldnames, _.at(context, resolvedFieldnames));
return zipObject(fieldnames, at(context, resolvedFieldnames));
};
4 changes: 2 additions & 2 deletions app/services/session.server/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from "crypto";
import type { Cookie, Session } from "@remix-run/node";
import { createSessionStorage, createCookie } from "@remix-run/node";
import _ from "lodash";
import merge from "lodash/merge";
import { type Context } from "~/domains/contexts";
import { flowIds, type FlowId } from "~/domains/flowIds";
import { config } from "~/services/env/env.server";
Expand Down Expand Up @@ -70,7 +70,7 @@ export const getSessionData = async (
};

export const updateSession = (session: Session, validatedData: Context) => {
const mergedData = _.merge(session.data, validatedData);
const mergedData = merge(session.data, validatedData);
Object.entries(mergedData).forEach(([key, value]) => {
session.set(key, value);
});
Expand Down
4 changes: 2 additions & 2 deletions scripts/airlines.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import fs from "node:fs";
import _ from "lodash";
import uniqBy from "lodash/uniqBy";

interface Airline {
name: string;
Expand Down Expand Up @@ -28,7 +28,7 @@ function processAirlineRow(row: string): Airline {
}

function saveAirlinesInFile(airlines: Airline[]): void {
const data = JSON.stringify(_.uniqBy(airlines, "iata"));
const data = JSON.stringify(uniqBy(airlines, "iata"));
const saveFilePath = "data/airlines/data.json";

fs.writeFile(saveFilePath, data, (error) => {
Expand Down
4 changes: 2 additions & 2 deletions scripts/airports.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import fs from "fs";
import { getName } from "i18n-iso-countries";
import _ from "lodash";
import uniqBy from "lodash/uniqBy";
import { z } from "zod";
import type { Airport } from "~/domains/fluggastrechte/services/airports/type";

Expand Down Expand Up @@ -159,7 +159,7 @@ async function fetchAllAirports(): Promise<Airport[]> {

async function fetchAndSaveAirports() {
const airports = await fetchAllAirports();
const data = JSON.stringify(_.uniqBy(airports, "iata"));
const data = JSON.stringify(uniqBy(airports, "iata"));

fs.writeFileSync(FILE_PATH_AIRPORTS_DATA, data);
console.log(`${FILE_PATH_AIRPORTS_DATA} written correctly`);
Expand Down
6 changes: 3 additions & 3 deletions scripts/unusedStrapiEntries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { readFileSync } from "node:fs";
import _ from "lodash";
import partition from "lodash/partition";
import { flows } from "~/domains/flows.server";
import { strapiFileSchema, type StrapiSchemas } from "~/services/cms/schemas";
import { type Config } from "~/services/flow/server/buildFlowController";
Expand Down Expand Up @@ -38,11 +38,11 @@ function urlsFromPages(pages: MinimalPage[]) {
}

function partitionPagesByStepId(pages: MinimalPage[]) {
return _.partition(pages, (page) => page.stepId !== null);
return partition(pages, (page) => page.stepId !== null);
}

function partitionPagesByFlowId(pages: MinimalPage[]) {
return _.partition(pages, (page) => page.flow_ids.length > 0);
return partition(pages, (page) => page.flow_ids.length > 0);
}

function unusedStrapiEntry() {
Expand Down

0 comments on commit dd4d796

Please sign in to comment.