Skip to content

Commit

Permalink
Migrate common.js to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Veikkosuhonen committed Jan 18, 2024
1 parent 15edeb0 commit 4229b41
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/auth/common.js → src/auth/common.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -57,9 +58,3 @@ const startOfStudyYear = (date) => {

return new Date(`${year}-${MONTH}-01`)
}

module.exports = {
normalizeOrganisationCode,
mapToDegreeCode,
startOfStudyYear,
}

0 comments on commit 4229b41

Please sign in to comment.