Skip to content

Instantly share code, notes, and snippets.

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

Mety neysidev

🦦
loading...
View GitHub Profile
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,
})
function classNames(...classes) {
return classes.filter(Boolean).join(" ")
}
@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
)
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 {
function hasNetwork(): boolean {
return navigator.onLine
}
/**
* 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 discountPrice(discount: number, total: number): string {
const percentage = (discount / 100) * total
return (total - percentage).toLocaleString()
}
const fs = require("fs")
fs.readFile("input.csv", "utf8", (err, data) => {
if (err) return console.log(err)
const content = []
data.split("\n").forEach(line => {
content.push(line.split(","))
})
import _ from "lodash"
import { formatBytes } from "./format" // https://gist.github.com/neysidev/71d6100023c4359a5cff256f2601c333
const openBrowseWindow = ({
accept = [],
multiple = false,
onChange = () => {},
}) => {
// Initialize the file input
const input = document.createElement("input")
const sleep = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
})
}
// How to use?
await sleep(5000)