Skip to content

Commit

Permalink
refactor: get rid of lodash here
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckySoLucky committed Nov 25, 2024
1 parent 57e3ab9 commit 36fc9d0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/lib/helper.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -357,3 +357,11 @@ export function getHeadTextureUUID(value: string) {

return uuid;
}

export function uniqBy<T>(arr: T[], key: string) {
const seen = new Set();
return arr.filter((item) => {
const k = (item as Record<string, unknown>)[key];
return seen.has(k) ? false : seen.add(k);
});
}
12 changes: 10 additions & 2 deletions src/lib/sections/stats/Pets.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatsType>("profile");
// TODO: Once helper functions get moved to a global location, we can remove this function
function uniqBy<T>(arr: T[], key: string) {
const seen = new Set();
return arr.filter((item) => {
const k = (item as Record<string, unknown>)[key];
return seen.has(k) ? false : seen.add(k);
});
}
const pets = profile.pets;
</script>

Expand Down Expand Up @@ -60,7 +68,7 @@
</div>
{/if}
</Items>
{@const uniquePets = _.uniqBy(pets.pets, "type")}
{@const uniquePets = uniqBy(pets.pets, "type")}
{@const otherPets = pets.pets.filter((pet) => !uniquePets.includes(pet))}

<Items subtitle="Other Pets">
Expand Down
4 changes: 1 addition & 3 deletions src/lib/stats/fishing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/stats/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> };
Expand Down Expand Up @@ -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[]));
}
Expand All @@ -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);
Expand Down

0 comments on commit 36fc9d0

Please sign in to comment.