Skip to content

Commit

Permalink
support using other CalculationMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
canbax committed Aug 19, 2023
1 parent 997b8a9 commit bae684e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 14 deletions.
Binary file modified .DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"cSpell.words": [
"Adhan",
"dhuhr",
"Diyanet",
"fajr",
"isha",
"madhab",
"maghrib",
"Shafi"
]
}
18 changes: 14 additions & 4 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import express, {
} from "express";
import { ALL_PLACES } from "../data/geoData";
import { getPlace, findPlace, getTimes } from "../src/calculator";
import { isInRange, isValidDate } from "../src/util";
import {
getCalculationMethodParameter,
isInRange,
isValidDate,
} from "../src/util";

export const app: Express = express();

Expand Down Expand Up @@ -100,9 +104,12 @@ function getTimesFromCoordinates(req: Request, res: Response) {
const dateStr = req.query.date as string;
const date = isValidDate(dateStr) ? new Date(dateStr) : new Date(); // use today if invalid
const daysParam = Number(req.query.days as string);
const days = isNaN(daysParam) || daysParam < 1 ? 100 : daysParam; // 50 is default
const days = isNaN(daysParam) || daysParam < 1 ? 100 : daysParam; // 100 is default
const tzParam = Number(req.query.timezoneOffset as string);
const tzOffset = isNaN(tzParam) ? 0 : tzParam; // 0 is default
const calculateMethod = getCalculationMethodParameter(
req.query.calculationMethod as string
);
if (
isNaN(lat) ||
isNaN(lng) ||
Expand All @@ -112,7 +119,7 @@ function getTimesFromCoordinates(req: Request, res: Response) {
res.send({ error: "Invalid coordinates!" });
} else {
const place = findPlace(lat, lng);
const times = getTimes(lat, lng, date, days, tzOffset);
const times = getTimes(lat, lng, date, days, tzOffset, calculateMethod);
res.send({ place, times });
}
}
Expand All @@ -138,12 +145,15 @@ function getTimesFromPlace(req: Request, res: Response) {
const days = isNaN(daysParam) || daysParam < 1 ? 100 : daysParam; // 50 is default
const tzParam = Number(req.query.timezoneOffset as string);
const tzOffset = isNaN(tzParam) ? 0 : tzParam; // 0 is default
const calculateMethod = getCalculationMethodParameter(
req.query.calculationMethod as string
);
if (!place) {
res.send({ error: "Place cannot be found!" });
} else {
const lat = place.latitude;
const lng = place.longitude;
const times = getTimes(lat, lng, date, days, tzOffset);
const times = getTimes(lat, lng, date, days, tzOffset, calculateMethod);
res.send({ place, times });
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"lint": "eslint . --ext .ts --ignore-path .gitignore --max-warnings=0",
"lint-fix": "eslint . --ext .ts --ignore-path .gitignore --fix",
"prepare": "husky install",
"test-watch": "jest --watch",
"test": "jest",
"test-dev": "jest --detectOpenHandles",
"test-cov": "PORT=8080 jest --coverage",
Expand Down
13 changes: 7 additions & 6 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,29 @@ <h3>namaz-vakti</h3>
uygulaması için oluşturulmuştur

<br />
<h2>Kullanım</h2>
<a
target="_blank"
href="https://www.postman.com/canbax/workspace/namaz-vakti-api/api/bf039fea-6768-490b-b11d-11bb031bdd8a"
>
Postman API tanımı
<h2>Postman API tanımı</h2>
</a>

<h2>Kullanım</h2>
<br />
<span>Koordinatlardan vakit verileri: </span>
<a
target="_blank"
href="/api/timesFromCoordinates?lat=39.91987&lng=32.85427&date=2023-10-29&days=3&timezoneOffset=180"
href="/api/timesFromCoordinates?lat=39.91987&lng=32.85427&date=2023-10-29&days=3&timezoneOffset=180&calculationMethod=Turkey"
>
/api/timesFromCoordinates?lat=39.91987&lng=32.85427&date=2023-10-29&days=3&timezoneOffset=180
/api/timesFromCoordinates?lat=39.91987&lng=32.85427&date=2023-10-29&days=3&timezoneOffset=180&calculationMethod=Turkey
</a>
<br />
<span>Mahalden vakit verileri: </span>
<a
target="_blank"
href="/api/timesFromPlace?country=Turkey&region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180"
href="/api/timesFromPlace?country=Turkey&region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180&calculationMethod=Turkey"
>
/api/timesFromPlace?country=Turkey&amp;region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180
/api/timesFromPlace?country=Turkey&amp;region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180&calculationMethod=Turkey
</a>
<br />
<span>Ülkelerin listesi: </span>
Expand Down
5 changes: 3 additions & 2 deletions src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export function getTimes(
lng: number,
date: Date,
days: number,
timezoneOffset: number
timezoneOffset: number,
calculationMethod: keyof typeof CalculationMethod = "Turkey"
): TimesData {
const coordinates = new Coordinates(lat, lng);
const params = CalculationMethod.Turkey();
const params = CalculationMethod[calculationMethod]();
params.madhab = Madhab.Shafi;
const r: TimesData = {};
for (let i = 0; i < days; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/CalculationMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const CalculationMethod = {
return params;
},

// Dianet
// Diyanet
Turkey() {
const params = new CalculationParameters("Turkey", 18, 17);
params.methodAdjustments = {
Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CalculationMethod } from "./lib/Adhan";
import { DateString, HourString } from "./types";

export function prefix0(n: number) {
Expand Down Expand Up @@ -26,6 +27,13 @@ export function isValidDate(str: string | null | undefined): boolean {
return true;
}

export function getCalculationMethodParameter(
calculationMethod: string | undefined
): keyof typeof CalculationMethod {
const val = calculationMethod as keyof typeof CalculationMethod;
return val && CalculationMethod[val] ? val : "Turkey";
}

export function dateToString(date: Date): DateString {
const year = date.getFullYear();
const month = date.getMonth() + 1;
Expand Down
26 changes: 26 additions & 0 deletions test/calculator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getTimes, findPlace, getPlace } from "../src/calculator";
import { DateString } from "../src/types";
import { isHourStringsClose } from "../src/util";
import { DATA_ANKARA_1 } from "../data/mockData";
import { CalculationMethod } from "../src/lib/Adhan";

describe("calculator tests", () => {
it("should bring times data similar to https://namazvakitleri.diyanet.gov.tr/tr-TR for Ankara in 2022-12-10 for 31 days", () => {
Expand All @@ -16,6 +17,31 @@ describe("calculator tests", () => {
}
});

it.each([
"MuslimWorldLeague",
"Egyptian",
"Karachi",
"UmmAlQura",
"Dubai",
"MoonsightingCommittee",
"NorthAmerica",
"Kuwait",
"Qatar",
"Singapore",
"Tehran",
"Turkey",
"Other",
] as (keyof typeof CalculationMethod)[])(
"Should get valid times data for calculation method %s",
(x) => {
const times = getTimes(39.91987, 32.85427, new Date(), 1, 0, x);
expect(times).toBeDefined();
expect(Object.keys(times).length).toBe(1);
expect(Object.values(times).length).toBe(1);
expect(Object.values(times)[0].length).toBe(6);
}
);

it("should find closest place to 0,0", () => {
const p = findPlace(0, 0);
expect(p.country).toBe("Ghana");
Expand Down
29 changes: 28 additions & 1 deletion test/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
isValidDate,
dateToString,
isHourStringsClose,
getCalculationMethodParameter,
} from "../src/util";

describe("check re tests", () => {
describe("utils.ts", () => {
it("should detect close hour strings for the same hour values", () => {
expect(isHourStringsClose("00:00", "00:03")).toBe(true);
});
Expand Down Expand Up @@ -105,4 +106,30 @@ describe("check re tests", () => {
it("should convert Date object into 'DateString' when day and month are 1 digit", () => {
expect(dateToString(new Date(2022, 2, 4))).toBe("2022-03-04");
});

it("Should get default calculation method of if undefined", () => {
expect(getCalculationMethodParameter(undefined)).toBe("Turkey");
});

it("Should get default calculation method of if invalid parameter value is passed", () => {
expect(getCalculationMethodParameter("my invalid method")).toBe("Turkey");
});

it.each([
"MuslimWorldLeague",
"Egyptian",
"Karachi",
"UmmAlQura",
"Dubai",
"MoonsightingCommittee",
"NorthAmerica",
"Kuwait",
"Qatar",
"Singapore",
"Tehran",
"Turkey",
"Other",
])("Should calculation method for valid string %s", (x) => {
expect(getCalculationMethodParameter(x)).toBe(x);
});
});

1 comment on commit bae684e

@vercel
Copy link

@vercel vercel bot commented on bae684e Aug 19, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

namaz-vakti-api – ./

namaz-vakti-api-canbax.vercel.app
namaz-vakti.vercel.app
namaz-vakti-api-git-main-canbax.vercel.app

Please sign in to comment.