Skip to content

Commit

Permalink
🚧 add utils and try a delay
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Oct 8, 2023
1 parent 8857dc5 commit 174f53c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions client/composables/lnutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export const useLnUtils = () => {
/**
* Perform a sleep as a Promise
* ex: await this.$sleep(200)
* @param milliseconds
*/
const sleep = (milliseconds: number) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}

/**
* Generate a random integer similar to php's rand()
* @see https://www.php.net/rand
* @param min - The lowest value to return
* @param max - The highest value to return
*/
const rand = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1)) + min
}

/**
* Capitalize the first letter of a string
* @param string
*/
const ucFirst = (string: string) => {
return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
* Scroll to an element on the page
* @param id
* @param offset
*/
const properScroll = (id: string, offset: number) => {
const el = document.getElementById(id)
if (!el) return true
const y = el.getBoundingClientRect().top + window.pageYOffset + offset
window.scrollTo(0, y)
}

/**
* Get the name from a URL
*
* @param url
*/
const nameFromURL = (url: string): string => {
const domain = url.replace('http://', '').replace('https://', '').split(/[/?#]/)[0]
return domain
}

/**
* Get the query string from a URL
* @param url
*/

const queryFromURL = (urlString: string): string => {
const url = new URL(urlString)
return url.search
}

return { sleep, rand, ucFirst, properScroll, nameFromURL, queryFromURL }
}
1 change: 1 addition & 0 deletions client/middleware/auth.global.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Menu from '@/lib/menu'

export default defineNuxtRouteMiddleware(async (to, from) => {
await useLnUtils().sleep(100)
const api = useApi()
await api.checkUser()
const menu = new Menu(api)
Expand Down

0 comments on commit 174f53c

Please sign in to comment.