diff --git a/frontend/jsconfig.json b/frontend/jsconfig.json index 9660d5f..9114199 100644 --- a/frontend/jsconfig.json +++ b/frontend/jsconfig.json @@ -22,6 +22,7 @@ "include": [ "src/**/*.d.ts", "src/**/*.js", + "src/**/*.ts", "src/**/*.svelte" ] } diff --git a/frontend/src/lib/actions/input.js b/frontend/src/lib/actions/input.js index 369dd17..fea6f19 100644 --- a/frontend/src/lib/actions/input.js +++ b/frontend/src/lib/actions/input.js @@ -14,7 +14,7 @@ export default function input(node, { autofocus, type, onValid, onInvalid, manda node.setAttribute('autocomplete', false); const getMessage = () => { - const checkInteger = () => (isInt(node.value) ? false : 'Value must be an integer'); + const checkInteger = () => (Number.isInteger(node.value) ? false : 'Value must be an integer'); const checkNumberBoundaries = boundaries => { if (node.value < boundaries[0]) { return `Input is too low for type ${type}`; diff --git a/frontend/src/lib/constants.js b/frontend/src/lib/constants.js deleted file mode 100644 index b6d1d9b..0000000 --- a/frontend/src/lib/constants.js +++ /dev/null @@ -1,7 +0,0 @@ -// Months -export const months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; -export const monthsAbbr = months.map(m => m.slice(0, 3)); - -// Days -export const days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; -export const daysAbbr = days.map(d => d.slice(0, 3)); diff --git a/frontend/src/lib/constants.ts b/frontend/src/lib/constants.ts new file mode 100644 index 0000000..7aa0a38 --- /dev/null +++ b/frontend/src/lib/constants.ts @@ -0,0 +1,27 @@ +export const months = [ + 'January', + 'February', + 'March', + 'April', + 'May', + 'June', + 'July', + 'August', + 'September', + 'October', + 'November', + 'December' +]; + +export const days = [ + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday', + 'Sunday' +]; + +export const daysAbbr = days.map(d => d.slice(0, 3)); +export const monthsAbbr = months.map(m => m.slice(0, 3)); diff --git a/frontend/src/lib/math.js b/frontend/src/lib/math.ts similarity index 58% rename from frontend/src/lib/math.js rename to frontend/src/lib/math.ts index c76ca77..22be3f0 100644 --- a/frontend/src/lib/math.js +++ b/frontend/src/lib/math.ts @@ -1,13 +1,4 @@ -// https://stackoverflow.com/a/14794066 -export function isInt(value) { - if (isNaN(value)) { - return false; - } - const x = parseFloat(value); - return (x | 0) === x; -} - -export function randInt(min, max) { +export function randInt(min: number, max: number) { return Math.round(Math.random() * (max - min) + min); } diff --git a/frontend/src/lib/strings.js b/frontend/src/lib/strings.ts similarity index 50% rename from frontend/src/lib/strings.js rename to frontend/src/lib/strings.ts index cc06f29..cf9ade2 100644 --- a/frontend/src/lib/strings.js +++ b/frontend/src/lib/strings.ts @@ -1,19 +1,19 @@ -export function capitalise(string = '') { +export function capitalise(string = ''): string { const capitalised = string.charAt(0).toUpperCase() + string.slice(1); return capitalised; } -export function jsonLooseParse(json) { - const obj = new Function(`return (${json})`)(); +export function jsonLooseParse(json: string): T { + const obj: T = new Function(`return (${json})`)(); return obj; } -export function convertLooseJson(json) { +export function convertLooseJson(json: any) { const j = JSON.stringify(jsonLooseParse(json)); return j; } -export function looseJsonIsValid(json) { +export function looseJsonIsValid(json: string): boolean { try { jsonLooseParse(json); return true; @@ -23,6 +23,6 @@ export function looseJsonIsValid(json) { } } -export function stringCouldBeID(string) { +export function stringCouldBeID(string: string) { return /^[a-zA-Z0-9_-]{1,}$/.test(string); }