From 36fc9d047d0336031b9a83cdaeea2d2ca43d2293 Mon Sep 17 00:00:00 2001 From: DuckySoLucky Date: Mon, 25 Nov 2024 21:36:15 +0100 Subject: [PATCH] refactor: get rid of lodash here --- src/lib/helper.ts | 10 +++++++++- src/lib/sections/stats/Pets.svelte | 12 ++++++++++-- src/lib/stats/fishing.ts | 4 +--- src/lib/stats/pets.ts | 7 +++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/lib/helper.ts b/src/lib/helper.ts index 4d18b395..d3ffcbc0 100644 --- a/src/lib/helper.ts +++ b/src/lib/helper.ts @@ -1,8 +1,8 @@ import type { Item, ProcessedItem } from "$types/stats"; import { getPrices } from "skyhelper-networth"; +import { v4 } from "uuid"; import * as constants from "./constants/constants"; import { getTexture } from "./custom_resources"; -import { v4 } from "uuid"; export * from "$lib/helper/cache"; export * from "$lib/helper/item"; @@ -357,3 +357,11 @@ export function getHeadTextureUUID(value: string) { return uuid; } + +export function uniqBy(arr: T[], key: string) { + const seen = new Set(); + return arr.filter((item) => { + const k = (item as Record)[key]; + return seen.has(k) ? false : seen.add(k); + }); +} diff --git a/src/lib/sections/stats/Pets.svelte b/src/lib/sections/stats/Pets.svelte index 054fdfaa..142411c7 100644 --- a/src/lib/sections/stats/Pets.svelte +++ b/src/lib/sections/stats/Pets.svelte @@ -7,12 +7,20 @@ import Items from "$lib/layouts/stats/Items.svelte"; import type { Stats as StatsType } from "$types/stats"; import { Collapsible } from "bits-ui"; - import _ from "lodash"; import { getContext } from "svelte"; //import { formatNumber } from "$lib/helper"; const profile = getContext("profile"); + // TODO: Once helper functions get moved to a global location, we can remove this function + function uniqBy(arr: T[], key: string) { + const seen = new Set(); + return arr.filter((item) => { + const k = (item as Record)[key]; + return seen.has(k) ? false : seen.add(k); + }); + } + const pets = profile.pets; @@ -60,7 +68,7 @@ {/if} - {@const uniquePets = _.uniqBy(pets.pets, "type")} + {@const uniquePets = uniqBy(pets.pets, "type")} {@const otherPets = pets.pets.filter((pet) => !uniquePets.includes(pet))} diff --git a/src/lib/stats/fishing.ts b/src/lib/stats/fishing.ts index 6bb2bd05..c6b6ceab 100644 --- a/src/lib/stats/fishing.ts +++ b/src/lib/stats/fishing.ts @@ -2,15 +2,13 @@ import * as constants from "$constants/constants"; import * as helper from "$lib/helper"; import type { Member } from "$types/global"; import type { TrophyFish } from "$types/stats"; -import _ from "lodash"; function getTrophyFish(userProfile: Member) { if (userProfile.trophy_fish === undefined) { return null; } - // NOTE: needed here cuz of the reverse() method, we love JavaScript.. - const reverstedTiers = _.clone(constants.TROPHY_FISH_TIERS).reverse(); + const reverstedTiers = JSON.parse(JSON.stringify(constants.TROPHY_FISH_TIERS)).reverse(); const output = []; for (const [id, data] of Object.entries(constants.TROPHY_FISH)) { diff --git a/src/lib/stats/pets.ts b/src/lib/stats/pets.ts index e852b4dd..85f8ed1c 100644 --- a/src/lib/stats/pets.ts +++ b/src/lib/stats/pets.ts @@ -2,7 +2,6 @@ import * as constants from "$constants/constants"; import * as helper from "$lib/helper"; import { NEU_CONSTANTS, NEU_ITEMS } from "$lib/scripts/parseNEURepository"; import type { Member, Pet, Pets, ProcessedItem, ProcessedPet, Profile } from "$types/global"; -import _ from "lodash"; import { getItemNetworth } from "skyhelper-networth"; let getMaxPetIdsCache = {} as { lastUpdated: number; data: Record }; @@ -336,7 +335,7 @@ function getPetScore(pets: ProcessedPet[]) { export async function getPets(userProfile: Member, items: ProcessedItem[], profile: Profile) { const output = {} as Pets; - const pets = _.clone(userProfile.pets_data?.pets ?? []); + const pets = JSON.parse(JSON.stringify(userProfile.pets_data?.pets ?? [])) as Pet[]; if (items !== undefined) { pets.push(...(items as unknown as Pet[])); } @@ -360,11 +359,11 @@ export async function getPets(userProfile: Member, items: ProcessedItem[], profi output.missing = getMissingPets(output.pets, profile.game_mode); const maxPetIds = getMaxPetIds(); - output.amount = _.uniqBy(output.pets, "type").length; + output.amount = helper.uniqBy(output.pets, "type").length; const totalPets = profile.game_mode === "bingo" ? Object.keys(maxPetIds) : Object.keys(maxPetIds).filter((pet) => pet !== "BINGO"); output.total = totalPets.length; - output.amountSkins = _.uniqBy(output.pets, "skin").length; + output.amountSkins = helper.uniqBy(output.pets, "skin").length; output.totalSkins = getPetSkins().length; output.petScore = getPetScore(output.pets);