From 4229b41a018db02eed736675977113ef5284affe Mon Sep 17 00:00:00 2001 From: Veikkosuhonen Date: Thu, 18 Jan 2024 22:42:28 +0200 Subject: [PATCH] Migrate common.js to typescript --- src/auth/{common.js => common.ts} | 35 +++++++++++++------------------ 1 file changed, 15 insertions(+), 20 deletions(-) rename src/auth/{common.js => common.ts} (65%) diff --git a/src/auth/common.js b/src/auth/common.ts similarity index 65% rename from src/auth/common.js rename to src/auth/common.ts index 6a300b2..77573b4 100644 --- a/src/auth/common.js +++ b/src/auth/common.ts @@ -1,21 +1,27 @@ -const isNumber = (value) => !Number.isNaN(parseInt(value, 10)) +const isNumber = (value: string) => !Number.isNaN(parseInt(value, 10)) -const normalizeOrganisationCode = (r) => { - if (r.startsWith('T')) { - return r.replace('T', '7') +/** + * TODO: Better explanation needed. Remove hardcodings + */ +export const normalizeOrganisationCode = (code: string) => { + if (code.startsWith('T')) { + return code.replace('T', '7') } - if (!r.includes('_')) { - return r + if (!code.includes('_')) { + return code } - const [left, right] = r.split('_') + const [left, right] = code.split('_') const prefix = [...left].filter(isNumber).join('') const suffix = `${left[0]}${right}` const providercode = `${prefix}0-${suffix}` return providercode } -const mapToDegreeCode = (organisationCode) => { +/** + * TODO: Better explanation needed. Remove hardcodings + */ +export const mapToDegreeCode = (organisationCode: string) => { if (!organisationCode) return '' const isKielikeskusOrAvoin = ['H906', 'H930'].includes(organisationCode) @@ -40,12 +46,7 @@ const mapToDegreeCode = (organisationCode) => { // Year starting month const MONTH = 8 -/** - * - * @param {Date | string | number} date - * @return {Date} first day of study year - */ -const startOfStudyYear = (date) => { +export const startOfStudyYear = (date: Date|string|number) => { let d = null if (typeof date !== 'object') { d = new Date(date) @@ -57,9 +58,3 @@ const startOfStudyYear = (date) => { return new Date(`${year}-${MONTH}-01`) } - -module.exports = { - normalizeOrganisationCode, - mapToDegreeCode, - startOfStudyYear, -}