From bae684e195ddc2763adf1a5703eb5c804ed10aa6 Mon Sep 17 00:00:00 2001 From: canbax Date: Sat, 19 Aug 2023 18:26:32 +0300 Subject: [PATCH] support using other CalculationMethods --- .DS_Store | Bin 6148 -> 6148 bytes .vscode/settings.json | 12 ++++++++++++ api/index.ts | 18 ++++++++++++++---- package.json | 1 + public/index.html | 13 +++++++------ src/calculator.ts | 5 +++-- src/lib/CalculationMethod.ts | 2 +- src/util.ts | 8 ++++++++ test/calculator.spec.ts | 26 ++++++++++++++++++++++++++ test/util.spec.ts | 29 ++++++++++++++++++++++++++++- 10 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.DS_Store b/.DS_Store index 52b0f12a32391dfc4a72a1bc40d87970f6a4518f..075caa5b0c720ea0dd38b10436ee8bbc57664868 100644 GIT binary patch delta 136 zcmZoMXfc=|&e%3FQH+&?fq{WzVxovF6OaJ{AexbZL4bjwh#`?7oguX>xF~O8qB>Xs z2SYMLK0_HpDzaoweqQ=S4f%~(4(yW+L|8U+bMSC5wrx!O&ODi4L{SuEpaKvl05Q;D Oh66y%u-Q>$KQjRI&>EBg delta 82 zcmZoMXfc=|&Zs)EPnamaz-vakti uygulaması için oluşturulmuştur
-

Kullanım

- Postman API tanımı +

Postman API tanımı

+ +

Kullanım


Koordinatlardan vakit verileri: - /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
Mahalden vakit verileri: - /api/timesFromPlace?country=Turkey&region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180 + /api/timesFromPlace?country=Turkey&region=Ankara&city=Ankara&date=2023-10-29&days=3&timezoneOffset=180&calculationMethod=Turkey
Ülkelerin listesi: diff --git a/src/calculator.ts b/src/calculator.ts index f1fa520..4ad3dea 100644 --- a/src/calculator.ts +++ b/src/calculator.ts @@ -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++) { diff --git a/src/lib/CalculationMethod.ts b/src/lib/CalculationMethod.ts index e790c80..3401384 100644 --- a/src/lib/CalculationMethod.ts +++ b/src/lib/CalculationMethod.ts @@ -84,7 +84,7 @@ const CalculationMethod = { return params; }, - // Dianet + // Diyanet Turkey() { const params = new CalculationParameters("Turkey", 18, 17); params.methodAdjustments = { diff --git a/src/util.ts b/src/util.ts index e8102a5..e7eeb42 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,4 @@ +import { CalculationMethod } from "./lib/Adhan"; import { DateString, HourString } from "./types"; export function prefix0(n: number) { @@ -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; diff --git a/test/calculator.spec.ts b/test/calculator.spec.ts index 289a742..cef6aec 100644 --- a/test/calculator.spec.ts +++ b/test/calculator.spec.ts @@ -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", () => { @@ -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"); diff --git a/test/util.spec.ts b/test/util.spec.ts index 566123d..415a506 100644 --- a/test/util.spec.ts +++ b/test/util.spec.ts @@ -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); }); @@ -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); + }); });