Skip to content

Commit

Permalink
Dedupe function to merge cars by certain type
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Nov 13, 2024
1 parent 3eea51d commit 4fa291a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/cars/fuel-types/[fuelType]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@/types";
import { capitaliseWords } from "@/utils/capitaliseWords";
import { fetchApi } from "@/utils/fetchApi";
import { mergeCarsByFuelType } from "@/utils/mergeCarsByFuelType";
import { mergeCarsByMake } from "@/utils/mergeCarsByMake";
import type { Metadata } from "next";
import type { Dataset, WithContext } from "schema-dts";

Expand Down Expand Up @@ -79,7 +79,7 @@ const CarsByFuelTypePage = async (props: {
return <EmptyData />;
}

const filteredCars = mergeCarsByFuelType(cars);
const filteredCars = mergeCarsByMake(cars);

const structuredData: WithContext<Dataset> = {
"@context": "https://schema.org",
Expand Down
4 changes: 2 additions & 2 deletions app/cars/vehicle-types/[vehicleType]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@/types";
import { capitaliseWords } from "@/utils/capitaliseWords";
import { fetchApi } from "@/utils/fetchApi";
import { mergeCarsByVehicleType } from "@/utils/mergeCarsByVehicleType";
import { mergeCarsByMake } from "@/utils/mergeCarsByMake";
import type { Metadata } from "next";
import type { Dataset, WithContext } from "schema-dts";

Expand Down Expand Up @@ -83,7 +83,7 @@ const CarsByVehicleTypePage = async (props: {
return <EmptyData />;
}

const filteredCars = mergeCarsByVehicleType(cars);
const filteredCars = mergeCarsByMake(cars);

const structuredData: WithContext<Dataset> = {
"@context": "https://schema.org",
Expand Down
4 changes: 1 addition & 3 deletions utils/mergeCarsByFuelType.ts → utils/mergeCarsByMake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import type { Car } from "@/types";
* @param cars - Array of Car objects to be merged
* @returns Sorted array of merged Car objects
*/
export const mergeCarsByFuelType = (cars: Car[]): Car[] => {
// Early return for empty array
export const mergeCarsByMake = (cars: Car[]): Car[] => {
if (!cars?.length) return [];

// Use reduce instead of forEach for more functional approach
const mergedData = cars
.filter(({ number }) => number > 0)
.reduce<Record<Car["make"], Car>>((acc, car) => {
Expand Down
29 changes: 0 additions & 29 deletions utils/mergeCarsByVehicleType.ts

This file was deleted.

0 comments on commit 4fa291a

Please sign in to comment.