|
| 1 | +const MINUTE = 60; |
| 2 | +const HOUR = MINUTE * 60; |
| 3 | +const DAY = HOUR * 24; |
| 4 | +const WEEK = DAY * 7; |
| 5 | +const MONTH = (DAY * 365) / 12; |
| 6 | +const YEAR = DAY * 365; |
| 7 | + |
| 8 | +export const toDateISOString = (data) => { |
| 9 | + const date = new Date(data); |
| 10 | + return date.toLocaleDateString("en-US", { |
| 11 | + weekday: "long", |
| 12 | + day: "numeric", |
| 13 | + month: "long", |
| 14 | + year: "numeric", |
| 15 | + hour12: true, |
| 16 | + hour: "numeric", |
| 17 | + minute: "2-digit", |
| 18 | + second: "2-digit", |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +export const bytesToSize = (bytes, decimals = 2) => { |
| 23 | + if (bytes === 0) return "0 Bytes"; |
| 24 | + |
| 25 | + const k = 1024; |
| 26 | + const dm = decimals < 0 ? 0 : decimals; |
| 27 | + const sizes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; |
| 28 | + |
| 29 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 30 | + |
| 31 | + return `${(bytes / Math.pow(k, i)).toFixed(dm)} ${sizes[i]}`; |
| 32 | +}; |
| 33 | + |
| 34 | +export const isEmpty = (string) => { |
| 35 | + // NOTE(jim): If a number gets passed in, it isn't considered empty for zero. |
| 36 | + if (string === 0) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + if (!string) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + if (typeof string === "object") { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + if (string.length === 0) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + string = string.toString(); |
| 53 | + |
| 54 | + return !string.trim(); |
| 55 | +}; |
| 56 | + |
| 57 | +export function classNames() { |
| 58 | + var classes = []; |
| 59 | + |
| 60 | + for (var i = 0; i < arguments.length; i++) { |
| 61 | + var arg = arguments[i]; |
| 62 | + if (!arg) continue; |
| 63 | + |
| 64 | + var argType = typeof arg; |
| 65 | + |
| 66 | + if (argType === "string" || argType === "number") { |
| 67 | + classes.push(arg); |
| 68 | + } else if (Array.isArray(arg)) { |
| 69 | + if (arg.length) { |
| 70 | + var inner = classNames.apply(null, arg); |
| 71 | + if (inner) { |
| 72 | + classes.push(inner); |
| 73 | + } |
| 74 | + } |
| 75 | + } else if (argType === "object") { |
| 76 | + if (arg.toString !== Object.prototype.toString) { |
| 77 | + classes.push(arg.toString()); |
| 78 | + } else { |
| 79 | + for (var key in arg) { |
| 80 | + if (hasOwn.call(arg, key) && arg[key]) { |
| 81 | + classes.push(key); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return classes.join(" "); |
| 89 | +} |
0 commit comments