-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters