Skip to content

Commit

Permalink
const -> function for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
heybereket committed May 21, 2023
1 parent 96ba601 commit 425ab1f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions hooks/useDistance.tsx → hooks/useDistance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useEffect, useState } from "react";
import haversine from "haversine-distance";

export const useDistance = (
export function useDistance(
latitude: number,
longitude: number
): number | null => {
): number | null {
const [distance, setDistance] = useState<number | null>(null);

useEffect((): void => {
Expand Down Expand Up @@ -34,4 +34,4 @@ export const useDistance = (
}, [latitude, longitude]);

return distance;
};
}
16 changes: 8 additions & 8 deletions utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const getDayOrdinal = (day: number) => {
function getDayOrdinal(day: number) {
const suffixes: string[] = ["th", "st", "nd", "rd"];
const v: number = day % 100;
return day + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);
};
}

export const convertDate = (dateParam: string) => {
export function convertDate(dateParam: string) {
const date: Date = new Date(dateParam);
const month: number = date.getUTCMonth();
const day: number = date.getUTCDate();
Expand All @@ -29,18 +29,18 @@ export const convertDate = (dateParam: string) => {

const output: string = `${monthName} ${dayOrdinal}`;
return output;
};
}

export const isLive = (start: string, end: string) => {
export function isLive(start: string, end: string) {
const today: Date = new Date();
const newToday: string = today.toISOString().split("T")[0];

today.setHours(0, 0, 0, 0);

return newToday >= start && newToday <= end;
};
}

export const convertSeconds = (epochSeconds: number): string => {
export function convertSeconds(epochSeconds: number): string {
const date: Date = new Date(epochSeconds * 1000);
const hours: number = date.getHours();
const minutes: number = date.getMinutes();
Expand All @@ -49,4 +49,4 @@ export const convertSeconds = (epochSeconds: number): string => {
const formattedMinutes: string | number =
minutes < 10 ? `0${minutes}` : minutes;
return `${formattedHours}:${formattedMinutes} ${ampm}`;
};
}
4 changes: 2 additions & 2 deletions utils/detectNsfw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nsfwjs from "nsfwjs";

export const detectNsfw = async (imageUrl: string): Promise<boolean> => {
export async function detectNsfw(imageUrl: string): Promise<boolean> {
const img: HTMLImageElement = document.createElement("img");
img.src = imageUrl;

Expand All @@ -18,4 +18,4 @@ export const detectNsfw = async (imageUrl: string): Promise<boolean> => {
return !!(nsfwProbability && nsfwProbability > 0.5);

// Image is safe
};
}
14 changes: 7 additions & 7 deletions utils/favourites.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { API_URL } from "@/lib/constants";
import router from "next/router";

export const getFavourites = async (setFavourites: any): Promise<void> => {
export async function getFavourites(setFavourites: any): Promise<void> {
const data: Response = await fetch(`${API_URL}/api/@me/favourites`);

if (data.ok) {
const JSONdata = await data.json();
setFavourites(JSONdata.favouritedTeams);
}
};
}

export const favouriteTeam = async (data: any): Promise<void> => {
export async function favouriteTeam(data: any): Promise<void> {
await fetch(`${API_URL}/api/@me/favourites`, {
method: "POST",
body: JSON.stringify(data),
});
};
}

export const unfavouriteTeam = async (
export async function unfavouriteTeam(
favouritedTeam: any,
noReload?: boolean
): Promise<void> => {
): Promise<void> {
await fetch(`${API_URL}/api/@me/favourites?id=${favouritedTeam[0].id}`, {
method: "DELETE",
});

if (!noReload) await router.push(router.pathname);
};
}
4 changes: 2 additions & 2 deletions utils/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface GeocodeResult {
}[];
}

export const getGeoData = async (address: string): Promise<GeoData | null> => {
export async function getGeoData(address: string): Promise<GeoData | null> {
const response: AxiosResponse<GeocodeResult, any> =
await axios.get<GeocodeResult>(
`https://maps.googleapis.com/maps/api/geocode/json?address=${address.replace(
Expand All @@ -30,4 +30,4 @@ export const getGeoData = async (address: string): Promise<GeoData | null> => {
}
const { lat, lng } = json.results[0].geometry.location;
return { lat, lng };
};
}
8 changes: 4 additions & 4 deletions utils/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from "./log";

export const setStorage = (key: string, value: string, ttl?: number): void => {
export function setStorage(key: string, value: string, ttl?: number): void {
const now: Date = new Date();

const item: { value: string; expiry: number } = {
Expand All @@ -17,9 +17,9 @@ export const setStorage = (key: string, value: string, ttl?: number): void => {
localStorage.clear();
}
}
};
}

export const getStorage = (key: string): null | any => {
export function getStorage(key: string): null | any {
const item: string | null = localStorage.getItem(key);
if (!item) return null;

Expand All @@ -32,4 +32,4 @@ export const getStorage = (key: string): null | any => {
}

return data.value;
};
}
4 changes: 2 additions & 2 deletions utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from "chalk";

export const log = (type: string, text: string): void => {
export function log(type: string, text: string): void {
switch (type) {
case "warning":
return console.log(`${chalk.yellow("INFO:")} ${text}`);

case "error":
return console.log(`${chalk.red("ERROR")} ${text}`);
}
};
}
10 changes: 5 additions & 5 deletions utils/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import { log } from "./log";
import { formatTime } from "./time";
import { HOFTeams } from "@/lib/lists/hallOfFame";

export const findTeam = (teamName: string): any => {
export function findTeam(teamName: string): any {
for (const team of HOFTeams) {
if ((team as any).name === teamName) {
return team;
}
}
return false;
};
}

export const teamNumberInRange = (
export function teamNumberInRange(
teamNumber: number,
teamNumberRange: string
): boolean => {
): boolean {
if (!teamNumberRange) {
return true;
}

const [start, end] = teamNumberRange.split("-");
return teamNumber >= parseInt(start) && teamNumber <= parseInt(end);
};
}

export async function fetchTeamsData(): Promise<any> {
const teamsData = getStorage(`teams_${CURR_YEAR}`);
Expand Down

1 comment on commit 425ab1f

@vercel
Copy link

@vercel vercel bot commented on 425ab1f May 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.