Skip to content

Commit

Permalink
reduce bundle size by using lodash-es.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrippa committed Jan 15, 2024
1 parent ec12b4d commit 8a0ac9c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 28 deletions.
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"date-fns": "^3.2.0",
"immer": "^10.0.3",
"jest": "^29.7.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"nanoid": "^5.0.4",
"notistack": "^3.0.1",
"react": "^18.2.0",
Expand All @@ -31,7 +31,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/lodash": "^4.14.202",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.11.0",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
Expand Down
5 changes: 5 additions & 0 deletions src/constants/baseChar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Character } from "../types/Character.ts";
import { nanoid } from "nanoid";
import { DeepPartial } from "../types/utils.ts";
import { cloneDeep, merge } from "lodash-es";

export const baseChar: Character = {
name: "Untitled",
Expand Down Expand Up @@ -29,3 +31,6 @@ export const baseChar: Character = {
},
isPersistent: true,
};

export const createChar = (char: DeepPartial<Character>): Character =>
merge(cloneDeep(baseChar), char);
26 changes: 12 additions & 14 deletions src/pages/library/AddToCombat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
useFieldArray,
useForm,
} from "react-hook-form";
import { Character, LibraryCharacter } from "../../types/Character.ts";
import { LibraryCharacter } from "../../types/Character.ts";
import { nanoid } from "nanoid";
import { FormInput } from "../../components/FormInput.tsx";
import { Box, IconButton } from "@mui/material";
import { useGlobalState } from "../../state/State.tsx";
import DeleteIcon from "@mui/icons-material/Delete";
import { cloneDeep, merge } from "lodash";
import { baseChar } from "../../constants/baseChar.ts";
import { createChar } from "../../constants/baseChar.ts";

type AddToCombatForm = {
characters: {
Expand Down Expand Up @@ -48,17 +47,16 @@ export const AddToCombat = (props: { id: LibraryCharacter["libraryId"] }) => {
(c) => c.libraryId === props.id,
);
if (!rootCharacter) return;
const newCharacters = usps.map(
(c) =>
merge(cloneDeep(baseChar), {
...rootCharacter,
name: c ? `${rootCharacter.name} (${c})` : rootCharacter.name,
id: nanoid(),
currentHp: rootCharacter.maxHp,
concentrated: false,
initiative: 0,
effects: { preTurn: [], status: [], postTurn: [] },
}) as Character,
const newCharacters = usps.map((c) =>
createChar({
...rootCharacter,
name: c ? `${rootCharacter.name} (${c})` : rootCharacter.name,
id: nanoid(),
currentHp: rootCharacter.maxHp,
concentrated: false,
initiative: 0,
effects: { preTurn: [], status: [], postTurn: [] },
}),
);
setCharacters((d) => {
newCharacters.forEach((c) => (d[c.id] = c));
Expand Down
2 changes: 1 addition & 1 deletion src/pages/mainTable/row/DealDamage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useChar } from "../charContext.ts";
import { SingleExpandedField } from "../../../components/SingleExpandedField.tsx";
import DD from "../../../assets/sword.svg?react";
import { Box, SvgIcon, Typography } from "@mui/material";
import { clamp } from "lodash";
import { clamp } from "lodash-es";
import { specialDamageEffects } from "../../../constants/specialDamageEffects.ts";
import { rollDieString } from "../../../dies/rollDieString.ts";
import { useSnackbar } from "notistack";
Expand Down
2 changes: 1 addition & 1 deletion src/state/State.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useContext } from "react";
import { Character, LibraryCharacter } from "../types/Character.ts";
import { noop } from "lodash";
import { noop } from "lodash-es";
import { Updater } from "use-immer";
import { Options } from "../types/Options.ts";
import { TurnInfo } from "../types/TurnInfo.ts";
Expand Down
6 changes: 2 additions & 4 deletions src/state/StateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { globalState, State } from "./State.tsx";
import { useImmer } from "use-immer";
import { nanoid } from "nanoid";
import { PropsWithChildren, useEffect, useMemo, useState } from "react";
import { cloneDeep, merge } from "lodash";
import { baseChar } from "../constants/baseChar.ts";
import { baseChar, createChar } from "../constants/baseChar.ts";
import { getCharacters } from "../utils/getCharacters.ts";

const zahir = {
Expand Down Expand Up @@ -69,8 +68,7 @@ const initialChars = {

const migrateChars = (chars: State["charactersDict"]) => {
Object.keys(chars).forEach((k) => {
const newChar = cloneDeep(baseChar);
chars[k] = merge(newChar, chars[k]);
chars[k] = createChar(chars[k]);
});
return chars;
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getCharacters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { orderBy } from "lodash";
import { orderBy } from "lodash-es";
import { State } from "../state/State.tsx";

export const getCharacters = (
Expand Down

0 comments on commit 8a0ac9c

Please sign in to comment.