-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from sgcarstrends/128-duplicated-makes-in-veh…
…icle-type-page Fix duplicated makes in vehicle type page
- Loading branch information
Showing
4 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
[ | ||
{ | ||
"_id": "66e29f9807b823f9e46e52ce", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "PI", | ||
"fuel_type": "Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52cd", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "PI", | ||
"fuel_type": "Petrol-Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52cc", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "PI", | ||
"fuel_type": "Petrol", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52cb", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "AMD", | ||
"fuel_type": "Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 113 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52ca", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "AMD", | ||
"fuel_type": "Petrol-Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 17 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52c9", | ||
"month": "2024-08", | ||
"make": "BMW", | ||
"importer_type": "AMD", | ||
"fuel_type": "Petrol", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 117 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52d0", | ||
"month": "2024-08", | ||
"make": "BYD", | ||
"importer_type": "AMD", | ||
"fuel_type": "Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 363 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52fe", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "PI", | ||
"fuel_type": "Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52fd", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "PI", | ||
"fuel_type": "Petrol-Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 2 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52fc", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "PI", | ||
"fuel_type": "Diesel", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52fb", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "PI", | ||
"fuel_type": "Petrol", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 3 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52fa", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "AMD", | ||
"fuel_type": "Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 39 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52f9", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "AMD", | ||
"fuel_type": "Petrol-Electric (Plug-In)", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 1 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52f8", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "AMD", | ||
"fuel_type": "Petrol-Electric", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": 192 | ||
}, | ||
{ | ||
"_id": "66e29f9807b823f9e46e52f7", | ||
"month": "2024-08", | ||
"make": "MERCEDES BENZ", | ||
"importer_type": "AMD", | ||
"fuel_type": "Petrol", | ||
"vehicle_type": "Sports Utility Vehicle", | ||
"number": "" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import mockCars from "@/__mocks__/mock-cars.json"; | ||
import { mergeCarsByVehicleType } from "@/utils/mergeCarsByVehicleType"; | ||
|
||
describe("mergeCarsByVehicleType", () => { | ||
it("should merge cars by make and vehicle type", () => { | ||
const cars = mergeCarsByVehicleType(mockCars); | ||
expect(cars).toEqual([ | ||
{ | ||
_id: "66e29f9807b823f9e46e52d0", | ||
fuel_type: "Electric", | ||
importer_type: "AMD", | ||
make: "BYD", | ||
month: "2024-08", | ||
number: 363, | ||
vehicle_type: "Sports Utility Vehicle", | ||
}, | ||
{ | ||
_id: "66e29f9807b823f9e46e52cb", | ||
fuel_type: "Electric", | ||
importer_type: "AMD", | ||
make: "BMW", | ||
month: "2024-08", | ||
number: 247, | ||
vehicle_type: "Sports Utility Vehicle", | ||
}, | ||
{ | ||
_id: "66e29f9807b823f9e46e52fd", | ||
fuel_type: "Petrol-Electric", | ||
importer_type: "PI", | ||
make: "MERCEDES BENZ", | ||
month: "2024-08", | ||
number: 237, | ||
vehicle_type: "Sports Utility Vehicle", | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Car } from "@/types"; | ||
|
||
export const mergeCarsByVehicleType = (cars: Car[]): Car[] => { | ||
const filteredCars = cars.filter(({ number }) => number > 0); | ||
|
||
const mergedData: Record<string, Car> = {}; | ||
filteredCars.forEach((car) => { | ||
const { make, number, vehicle_type } = car; | ||
const key = `${make}-${vehicle_type}`; | ||
|
||
if (!mergedData[key]) { | ||
mergedData[key] = { ...car, number: 0 }; | ||
} | ||
|
||
mergedData[key].number += number; | ||
}); | ||
|
||
return Object.values(mergedData).sort((a, b) => b.number - a.number); | ||
}; |