feat: type improvements
This commit is contained in:
@ -3,17 +3,15 @@ import {snakeCase} from 'snake-case'
|
||||
|
||||
/**
|
||||
* Transforms field names to camel case.
|
||||
* @param object
|
||||
* @returns {*}
|
||||
*/
|
||||
export function objectToCamelCase(object) {
|
||||
export function objectToCamelCase(object: Record<string, any>) {
|
||||
|
||||
// When calling recursively, this can be called without being and object or array in which case we just return the value
|
||||
if (typeof object !== 'object') {
|
||||
return object
|
||||
}
|
||||
|
||||
const parsedObject = {}
|
||||
const parsedObject: Record<string, any> = {}
|
||||
for (const m in object) {
|
||||
parsedObject[camelCase(m)] = object[m]
|
||||
|
||||
@ -25,7 +23,7 @@ export function objectToCamelCase(object) {
|
||||
|
||||
// Call it again for arrays
|
||||
if (Array.isArray(object[m])) {
|
||||
parsedObject[camelCase(m)] = object[m].map(o => objectToCamelCase(o))
|
||||
parsedObject[camelCase(m)] = object[m].map((o: Record<string, any>) => objectToCamelCase(o))
|
||||
// Because typeof [] === 'object' is true for arrays, we leave the loop here to prevent converting arrays to objects.
|
||||
continue
|
||||
}
|
||||
@ -40,17 +38,15 @@ export function objectToCamelCase(object) {
|
||||
|
||||
/**
|
||||
* Transforms field names to snake case - used before making an api request.
|
||||
* @param object
|
||||
* @returns {*}
|
||||
*/
|
||||
export function objectToSnakeCase(object) {
|
||||
export function objectToSnakeCase(object: Record<string, any>) {
|
||||
|
||||
// When calling recursively, this can be called without being and object or array in which case we just return the value
|
||||
if (typeof object !== 'object') {
|
||||
return object
|
||||
}
|
||||
|
||||
const parsedObject = {}
|
||||
const parsedObject: Record<string, any> = {}
|
||||
for (const m in object) {
|
||||
parsedObject[snakeCase(m)] = object[m]
|
||||
|
||||
@ -65,7 +61,7 @@ export function objectToSnakeCase(object) {
|
||||
|
||||
// Call it again for arrays
|
||||
if (Array.isArray(object[m])) {
|
||||
parsedObject[snakeCase(m)] = object[m].map(o => objectToSnakeCase(o))
|
||||
parsedObject[snakeCase(m)] = object[m].map((o: Record<string, any>) => objectToSnakeCase(o))
|
||||
// Because typeof [] === 'object' is true for arrays, we leave the loop here to prevent converting arrays to objects.
|
||||
continue
|
||||
}
|
||||
|
@ -5,11 +5,11 @@
|
||||
* @param rootElement
|
||||
* @param closeCallback A closure function to call when the click event happened outside of the rootElement.
|
||||
*/
|
||||
export const closeWhenClickedOutside = (event, rootElement, closeCallback) => {
|
||||
export const closeWhenClickedOutside = (event: MouseEvent, rootElement: HTMLElement, closeCallback: () => void) => {
|
||||
// We walk up the tree to see if any parent of the clicked element is the root element.
|
||||
// If it is not, we call the close callback. We're doing all this hassle to only call the
|
||||
// closing callback when a click happens outside of the rootElement.
|
||||
let parent = event.target.parentElement
|
||||
let parent = (event.target as HTMLElement)?.parentElement
|
||||
while (parent !== rootElement) {
|
||||
if (parent === null || parent.parentElement === null) {
|
||||
parent = null
|
||||
|
@ -1,12 +1,12 @@
|
||||
/**
|
||||
* Make date objects from timestamps
|
||||
*/
|
||||
export function parseDateOrNull(date) {
|
||||
export function parseDateOrNull(date: string | Date) {
|
||||
if (date instanceof Date) {
|
||||
return date
|
||||
}
|
||||
|
||||
if ((typeof date === 'string' || date instanceof String) && !date.startsWith('0001')) {
|
||||
if ((typeof date === 'string') && !date.startsWith('0001')) {
|
||||
return new Date(date)
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,13 @@
|
||||
// Save the current list view to local storage
|
||||
|
||||
import type { IList } from '@/modelTypes/IList'
|
||||
|
||||
type ListView = Record<IList['id'], string>
|
||||
|
||||
const DEFAULT_LIST_VIEW = 'list.list' as const
|
||||
|
||||
// We use local storage and not a store here to make it persistent across reloads.
|
||||
export const saveListView = (listId, routeName) => {
|
||||
export const saveListView = (listId: IList['id'], routeName: string) => {
|
||||
if (routeName.includes('settings.')) {
|
||||
return
|
||||
}
|
||||
@ -10,12 +17,12 @@ export const saveListView = (listId, routeName) => {
|
||||
}
|
||||
|
||||
const savedListView = localStorage.getItem('listView')
|
||||
let savedListViewJson = false
|
||||
let savedListViewJson: ListView | false = false
|
||||
if (savedListView !== null) {
|
||||
savedListViewJson = JSON.parse(savedListView)
|
||||
savedListViewJson = JSON.parse(savedListView) as ListView
|
||||
}
|
||||
|
||||
let listView = {}
|
||||
let listView: ListView = {}
|
||||
if (savedListViewJson) {
|
||||
listView = savedListViewJson
|
||||
}
|
||||
@ -24,7 +31,7 @@ export const saveListView = (listId, routeName) => {
|
||||
localStorage.setItem('listView', JSON.stringify(listView))
|
||||
}
|
||||
|
||||
export const getListView = listId => {
|
||||
export const getListView = (listId: IList['id']) => {
|
||||
// Remove old stored settings
|
||||
const savedListView = localStorage.getItem('listView')
|
||||
if (savedListView !== null && savedListView.startsWith('list.')) {
|
||||
@ -32,13 +39,13 @@ export const getListView = listId => {
|
||||
}
|
||||
|
||||
if (!savedListView) {
|
||||
return 'list.list'
|
||||
return DEFAULT_LIST_VIEW
|
||||
}
|
||||
|
||||
const savedListViewJson = JSON.parse(savedListView)
|
||||
const savedListViewJson: ListView = JSON.parse(savedListView)
|
||||
|
||||
if (!savedListViewJson[listId]) {
|
||||
return 'list.list'
|
||||
return DEFAULT_LIST_VIEW
|
||||
}
|
||||
|
||||
return savedListViewJson[listId]
|
||||
|
@ -10,7 +10,7 @@ const days = {
|
||||
friday: 5,
|
||||
saturday: 6,
|
||||
sunday: 0,
|
||||
}
|
||||
} as Record<string, number>
|
||||
|
||||
for (const n in days) {
|
||||
test(`today on a ${n}`, () => {
|
||||
@ -32,7 +32,7 @@ const nextMonday = {
|
||||
friday: 3,
|
||||
saturday: 2,
|
||||
sunday: 1,
|
||||
}
|
||||
} as Record<string, number>
|
||||
|
||||
for (const n in nextMonday) {
|
||||
test(`next monday on a ${n}`, () => {
|
||||
@ -48,7 +48,7 @@ const thisWeekend = {
|
||||
friday: 1,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
}
|
||||
} as Record<string, number>
|
||||
|
||||
for (const n in thisWeekend) {
|
||||
test(`this weekend on a ${n}`, () => {
|
||||
@ -64,7 +64,7 @@ const laterThisWeek = {
|
||||
friday: 0,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
}
|
||||
} as Record<string, number>
|
||||
|
||||
for (const n in laterThisWeek) {
|
||||
test(`later this week on a ${n}`, () => {
|
||||
@ -80,7 +80,7 @@ const laterNextWeek = {
|
||||
friday: 7 + 0,
|
||||
saturday: 7 + 0,
|
||||
sunday: 7 + 0,
|
||||
}
|
||||
} as Record<string, number>
|
||||
|
||||
for (const n in laterNextWeek) {
|
||||
test(`later next week on a ${n} (this week)`, () => {
|
||||
|
@ -1,4 +1,6 @@
|
||||
export function calculateDayInterval(dateString: string, currentDay = (new Date().getDay())) {
|
||||
type Day<T extends number = number> = T
|
||||
|
||||
export function calculateDayInterval(dateString: string, currentDay = (new Date().getDay())): Day {
|
||||
switch (dateString) {
|
||||
case 'today':
|
||||
return 0
|
||||
|
@ -6,7 +6,7 @@
|
||||
* @param dateString
|
||||
* @returns {Date}
|
||||
*/
|
||||
export const createDateFromString = dateString => {
|
||||
export function createDateFromString(dateString: string | Date) {
|
||||
if (dateString instanceof Date) {
|
||||
return dateString
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import {i18n} from '@/i18n'
|
||||
|
||||
const locales = {en: enGB, de, ch: de, fr, ru}
|
||||
|
||||
export function dateIsValid(date) {
|
||||
export function dateIsValid(date: Date | null) {
|
||||
if (date === null) {
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user