Skip to content

Commit

Permalink
refactor(decoders): use arrow fn since lighter in build
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Aug 11, 2024
1 parent f2ad985 commit fbffd86
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/api/restaurants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { Request } from "~/core/request";
import { decodeRestaurant } from "~/decoders/restaurant";
import { type Meal, Moment, type Restaurant } from "~/models";

export async function restaurants (identifier: string, fetcher: Fetcher = defaultFetcher): Promise<Array<Restaurant>> {
export const restaurants = async (identifier: string, fetcher: Fetcher = defaultFetcher): Promise<Array<Restaurant>> => {
const request = new Request(`${identifier}/externe/crous-${identifier}.min.json`);
const response = await fetcher(request);

const content = response.content.replace(/[\u0000-\u001F]/g, "");
const { restaurants } = JSON.parse(content);

return restaurants.map(decodeRestaurant);
}
};

export function isRestaurantOpen (restaurant: Restaurant, dayIndex: number, moment: Moment): boolean {
export const isRestaurantOpen = (restaurant: Restaurant, dayIndex: number, moment: Moment): boolean => {
const day = restaurant.opening.split(",")[dayIndex];
const momentAsINT = moment === Moment.LUNCH ? 1 : moment === Moment.MORNING ? 0 : 2;
const opening = day[momentAsINT];

return opening === "1";
}
};

export function meals (restaurant: Restaurant, date = new Date()): Array<Meal> | undefined {
export const meals = (restaurant: Restaurant, date = new Date()): Array<Meal> | undefined => {
const currentSTR = date.toLocaleDateString();
return restaurant.menus.find((menu) => menu.date.toLocaleDateString() === currentSTR)?.meals;
}
};
4 changes: 2 additions & 2 deletions src/decoders/contact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Contact } from "~/models";

export function decodeContact (contact: any): Contact {
export const decodeContact = (contact: any): Contact => {
return {
phone: contact.tel,
email: contact.email
};
}
};
4 changes: 2 additions & 2 deletions src/decoders/feed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Feed } from "~/models";

export function decodeFeed (feed: any): Feed {
export const decodeFeed = (feed: any): Feed => {
const name = feed.name.replace("FLUX ", "");
const identifier = feed.url.split("/")[4];

Expand All @@ -9,4 +9,4 @@ export function decodeFeed (feed: any): Feed {
identifier,
isDefault: feed.is_default
};
}
};
4 changes: 2 additions & 2 deletions src/decoders/image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Image } from "~/models";

export function decodeImage (image: any): Image {
export const decodeImage = (image: any): Image => {
return {
href: image.src,
description: image.alt
};
}
};
2 changes: 1 addition & 1 deletion src/decoders/meal-category.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MealCategory } from "~/models";

export function decodeMealCategory (category: any): MealCategory {
export const decodeMealCategory = (category: any): MealCategory => {
return {
name: category.name,
dishes: category.dishes.map((dish: any) => dish.name)
Expand Down
2 changes: 1 addition & 1 deletion src/decoders/meal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Meal, MealCategory } from "~/models";
import { decodeMealCategory } from "./meal-category";
import { decodeMoment } from "./moment";

export function decodeMeal (meal: any): Meal {
export const decodeMeal = (meal: any): Meal => {
let information: string | undefined;
const categories = meal.foodcategory.map(decodeMealCategory)
.filter((category: MealCategory) => {
Expand Down
4 changes: 2 additions & 2 deletions src/decoders/menu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Menu } from "~/models";
import { decodeMeal } from "./meal";

export function decodeMenu (menu: any): Menu {
export const decodeMenu = (menu: any): Menu => {
return {
date: new Date(menu.date),
meals: menu.meal.map(decodeMeal)
};
}
};
2 changes: 1 addition & 1 deletion src/decoders/moment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Moment, UnknownEnumValue } from "~/models";

export function decodeMoment (value: any): Moment {
export const decodeMoment = (value: any): Moment => {
switch (value) {
case "matin":
return Moment.MORNING;
Expand Down
2 changes: 1 addition & 1 deletion src/decoders/payment-method.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PaymentMethod, UnknownEnumValue } from "~/models";

export function decodePaymentMethod (value: any): PaymentMethod {
export const decodePaymentMethod = (value: any): PaymentMethod => {
const unwrapped = value.name;

switch (unwrapped) {
Expand Down
2 changes: 1 addition & 1 deletion src/decoders/restaurant-kind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RestaurantKind, UnknownEnumValue } from "~/models";

export function decodeRestaurantKind (value: any): RestaurantKind {
export const decodeRestaurantKind = (value: any): RestaurantKind => {
switch (value) {
case "Cafétéria":
return RestaurantKind.CAFETERIA;
Expand Down
4 changes: 2 additions & 2 deletions src/decoders/restaurant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { decodeRestaurantKind } from "./restaurant-kind";
import { decodeContact } from "./contact";
import { decodeImage } from "./image";

export function decodeRestaurant (restaurant: any): Restaurant {
export const decodeRestaurant = (restaurant: any): Restaurant => {
return {
id: restaurant.id,
title: restaurant.title,
Expand All @@ -29,4 +29,4 @@ export function decodeRestaurant (restaurant: any): Restaurant {
paymentMethods: restaurant.payment.map(decodePaymentMethod),
menus: restaurant.menus.map(decodeMenu)
};
}
};

0 comments on commit fbffd86

Please sign in to comment.