My task involves manipulating an array of items (specifically, rooms) in a program. I need to filter the array based on a certain property (rooms with more than 10 seats), group them by another property (the area the room is in), store them in a dictionary, and finally sort them by the dictionary's key.
https://i.sstatic.net/KSsoBm.png
To achieve this, I have implemented the following code:
import { ascend, filter, groupBy, pipe, sort } from "ramda";
class Room {
// Number of seats in the room
public seats!: number;
// Area on site, an area can have many rooms
public area!: string;
// Name of the room
public room!: number;
}
class NamedRoomDictionary {
[index: string]: Room[];
}
const GetRoomsWithMoreThanTenSeats = (rooms: Room[]): Room[] =>
filter(room => room.seats > 10, rooms);
const GroupByArea = (rooms: Room[]): NamedRoomDictionary =>
groupBy(room => room.area, rooms);
const SortByArea = (rooms: NamedRoomDictionary): NamedRoomDictionary =>
sort(ascend(room => room.index), rooms)
const SortBigRoomsByArea = pipe(
GetRoomsWithMoreThanTenSeats,
GroupByArea,
SortByArea
);
const rooms: Room[] = [
{room: 1, area: 'A', seats: 15},
{room: 2, area: 'D', seats: 5},
{room: 3, area: 'R', seats: 8},
{room: 4, area: 'E', seats: 14},
{room: 5, area: 'A', seats: 458},
{room: 6, area: 'F', seats: 10},
{room: 7, area: 'A', seats: 4},
{room: 8, area: 'A', seats: 256},
{room: 9, area: 'D', seats: 100}
];
const sorted = SortBigRoomsByArea(rooms);
console.log(sorted)
You can view and run this project on Repl.it.
Despite my efforts, I encountered the following errors:
Property 'index' does not exist on type 'Room[]'.ts(2339)
The error above pertains to room.index
on the line
sort(ascend(room => room.index), rooms)
.
Argument of type 'NamedRoomDictionary' is not assignable to parameter of type 'readonly Room[][]'. Type 'NamedRoomDictionary' is missing the properties required by 'readonly Room[][]': length, concat, join, slice, and several others.ts(2345)
This second error is related to rooms
on the line
sort(ascend(room => room.index), rooms)
.
A functional example can be seen on Repl.it. This version successfully groups the rooms but doesn't implement sorting.
Being relatively new to TypeScript syntax and the Ramda library, I would appreciate any guidance on resolving these issues.
Output of just grouping Repl.it
{ A:
[ { room: 1, area: 'A', seats: 15 },
{ room: 5, area: 'A', seats: 458 },
{ room: 8, area: 'A', seats: 256 } ],
E: [ { room: 4, area: 'E', seats: 14 } ],
D: [ { room: 9, area: 'D', seats: 100 } ] }
Desired output of sorting repl.it
{ A:
[ { room: 1, area: 'A', seats: 15 },
{ room: 5, area: 'A', seats: 458 },
{ room: 8, area: 'A', seats: 256 } ],
D: [ { room: 9, area: 'D', seats: 100 } ],
E: [ { room: 4, area: 'E', seats: 14 } ] }