Skip to content

Instantly share code, notes, and snippets.

View neysidev's full-sized avatar
🦦
loading...

Mety neysidev

🦦
loading...
View GitHub Profile
function discountPrice(discount: number, total: number): string {
const percentage = (discount / 100) * total
return (total - percentage).toLocaleString()
}
/**
* Calculate percentage
*
* @param {number} percent - The percentage to be converted to a decimal
* @param {number} total - total number of items
*
* @example (50 / 100) * 300 => 150
* @returns {number} percentage value
*/
function percentage(percent: number, total: number): number {
function hasNetwork(): boolean {
return navigator.onLine
}
export function changeMode() {
if (!localStorage || !document) return
if (
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches)
) {
document.documentElement.classList.add("dark")
} else {
@neysidev
neysidev / array-to-csv.js
Created February 5, 2022 13:15
Convert array to csv
function arrayToCsv(data) {
return data
.map(
row =>
row
.map(String) // convert every value to String
.map(v => v.replaceAll('"', '""')) // escape double colons
.map(v => `"${v}"`) // quote it
.join(",") // comma-separated
)
function classNames(...classes) {
return classes.filter(Boolean).join(" ")
}
function downloadFile(data, name) {
const blob = new Blob([data], { type: "octet-stream" })
const href = URL.createObjectURL(blob)
const linkTag = Object.assign(document.createElement("a"), {
href,
hidden: true,
download: name,
})