The doc object contains certain parameters that should not be directly mapped to rooms.
It is important to explicitly specify which attributes you want to extract from the doc
object. For larger objects, creating a function like buildRoom
can help in shaping the output using dot access or destructuring, ultimately streamlining the process.
function buildRoom1(obj: typeof doc) {
const {
maxPlayers,
ownerId,
state,
type,
...
} = doc;
return {
maxPlayers,
ownerId,
roomId: doc.ownerId,
state,
type
};
}
response.rooms.push(buildRoom(doc));
Alternatively:
function pluck<T extends Record<PropertyKey, any>, K extends keyof T>(obj: T, ...fields: K[]) {
return fields.reduce((acc, field) => ({ ...acc, [field]: obj[field] }), {} as Pick<T, K>);
}
const room = pluck(doc, "ownderId", "state", "type", "maxPlayers");
response.rooms.push({ ...doc, roomId: room.ownderId });
Or with an additional level of abstraction:
function buildRoom2(obj: typeof doc) {
const props = pluck(obj, "ownderId", "state", "type", "maxPlayers");
return { ...props, roomId: props.ownderId };
}
response.rooms.push(buildRoom2(doc));