-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
75b9988
commit 72aab2c
Showing
13 changed files
with
184 additions
and
161 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,122 @@ | ||
// helper.mjs | ||
|
||
/** | ||
* Copies the text content of an HTML element to the clipboard. | ||
* @async | ||
* @function clipText | ||
* @param {string} [id=""] - The ID of the HTML element to copy the text from. | ||
* @throws {Error} Throws an error if the specified element is missing. | ||
* @returns {Promise<void>} A Promise that resolves when the text has been copied to the clipboard. | ||
*/ | ||
export async function clipText(id = ``) { | ||
const element = document.getElementById(id); | ||
if (element === null) { | ||
throw Error(`select text element "${id}" is missing`); | ||
} | ||
element.focus(); // select the element to avoid NotAllowedError: Clipboard write is not allowed in this context | ||
await navigator.clipboard.writeText(`${element.textContent}`).then( | ||
function () { | ||
console.log( | ||
`Copied ${humanFilesize(element.textContent.length)} to the clipboard` | ||
); | ||
const button = document.getElementById(`copyReadme`), | ||
oneSecond = 1000; | ||
if (button === null) return; | ||
const save = button.textContent; | ||
button.textContent = `✓ Copied`; | ||
window.setTimeout(() => { | ||
button.textContent = `${save}`; | ||
}, oneSecond); | ||
}, | ||
function (err) { | ||
console.error(`could not save any text to the clipboard: ${err}`); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Retrieves an element from the DOM using its ID. | ||
* | ||
* @param {string} elementId - The ID of the element to retrieve. | ||
* @returns {HTMLElement} - The retrieved element. | ||
* @throws {Error} - If the element is not found in the DOM. | ||
*/ | ||
export function getElmById(elementId) { | ||
const element = document.getElementById(elementId); | ||
if (element == null) { | ||
throw new Error(`The ${elementId} element is null.`); | ||
} | ||
return element; | ||
} | ||
|
||
/** | ||
* Converts a file size in bytes to a human-readable format. | ||
* | ||
* @param {number} size - The file size in bytes. | ||
* @returns {string} A human-readable string representation of the file size. | ||
*/ | ||
export function humanFilesize(size = 0) { | ||
const three = 3, | ||
round = 100, | ||
kB = 1000, | ||
MB = Math.pow(kB, 2), | ||
GB = Math.pow(kB, three); | ||
if (size > GB) | ||
return `${(Math.round((size * round) / GB) / round).toFixed(2)} GB`; | ||
if (size > MB) | ||
return `${(Math.round((size * round) / MB) / round).toFixed(1)} MB`; | ||
if (size > kB) | ||
return `${(Math.round((size * round) / kB) / round).toFixed()} kB`; | ||
return `${Math.round(size).toFixed()} bytes`; | ||
} | ||
|
||
/** | ||
* Checks if a given year is valid, i.e. between 1980 and the current year. | ||
* @param {number} year - The year to be validated. | ||
* @returns {boolean} - Returns true if the year is valid, false otherwise. | ||
*/ | ||
export function validYear(year) { | ||
if (`${year}` == "") { | ||
return true; | ||
} | ||
const epochYear = 1980; | ||
const currentYear = new Date().getFullYear(); | ||
if (year < epochYear || year > currentYear) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Checks if a given month is valid. | ||
* @param {number} month - The month to be validated. | ||
* @returns {boolean} - Returns true if the month is valid, false otherwise. | ||
*/ | ||
export function validMonth(month) { | ||
if (`${month}` == "") { | ||
return true; | ||
} | ||
const jan = 1, | ||
dec = 12; | ||
if (month < jan || month > dec) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Checks if a given day is valid. | ||
* @param {number} day - The day to be checked. | ||
* @returns {boolean} - Returns true if the day is valid, false otherwise. | ||
*/ | ||
export function validDay(day) { | ||
if (`${day}` == "") { | ||
return true; | ||
} | ||
const first = 1, | ||
last = 31; | ||
if (day < first || day > last) { | ||
return false; | ||
} | ||
return true; | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.