|
| 1 | +const hasOwn = {}.hasOwnProperty; |
| 2 | +const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/; |
| 3 | +const localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/; |
| 4 | +const nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; |
| 5 | + |
| 6 | +export const noop = () => {}; |
| 7 | + |
| 8 | +export const pluralize = (text, count) => { |
| 9 | + return count > 1 || count === 0 ? `${text}s` : text; |
| 10 | +}; |
| 11 | + |
| 12 | +export function toDateISOString(data: string) { |
| 13 | + const date = new Date(data); |
| 14 | + return date.toLocaleDateString('en-US', { |
| 15 | + weekday: 'long', |
| 16 | + day: 'numeric', |
| 17 | + month: 'long', |
| 18 | + year: 'numeric', |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +export const elide = (string, length = 140, emptyState = '...') => { |
| 23 | + if (isEmpty(string)) { |
| 24 | + return emptyState; |
| 25 | + } |
| 26 | + |
| 27 | + if (string.length < length) { |
| 28 | + return string.trim(); |
| 29 | + } |
| 30 | + |
| 31 | + return `${string.substring(0, length)}...`; |
| 32 | +}; |
| 33 | + |
| 34 | +export function bytesToSize(bytes: number, decimals: number = 2) { |
| 35 | + if (bytes === 0) return '0 Bytes'; |
| 36 | + |
| 37 | + const k = 1000; |
| 38 | + const dm = decimals < 0 ? 0 : decimals; |
| 39 | + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
| 40 | + |
| 41 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 42 | + |
| 43 | + return `${(bytes / Math.pow(k, i)).toFixed(dm)} ${sizes[i]}`; |
| 44 | +} |
| 45 | + |
| 46 | +export function isEmpty(text: any) { |
| 47 | + // NOTE(jim): If a number gets passed in, it isn't considered empty for zero. |
| 48 | + if (text === 0) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + if (!text) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + if (typeof text === 'object') { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + if (text.length === 0) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + text = text.toString(); |
| 65 | + |
| 66 | + return Boolean(!text.trim()); |
| 67 | +} |
| 68 | + |
| 69 | +export function createSlug(text: any) { |
| 70 | + if (isEmpty(text)) { |
| 71 | + return 'untitled'; |
| 72 | + } |
| 73 | + |
| 74 | + const a = 'æøåàáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;'; |
| 75 | + const b = 'aoaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------'; |
| 76 | + const p = new RegExp(a.split('').join('|'), 'g'); |
| 77 | + |
| 78 | + return text |
| 79 | + .toString() |
| 80 | + .toLowerCase() |
| 81 | + .replace(/\s+/g, '-') // Replace spaces with - |
| 82 | + .replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special chars |
| 83 | + .replace(/&/g, '-and-') // Replace & with 'and' |
| 84 | + .replace(/[^\w\-]+/g, '') // Remove all non-word chars |
| 85 | + .replace(/\-\-+/g, '-') // Replace multiple - with single - |
| 86 | + .replace(/^-+/, '') // Trim - from start of text |
| 87 | + .replace(/-+$/, ''); // Trim - from end of text |
| 88 | +} |
| 89 | + |
| 90 | +export function isUrl(string: any) { |
| 91 | + if (typeof string !== 'string') { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + var match = string.match(protocolAndDomainRE); |
| 96 | + if (!match) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + var everythingAfterProtocol = match[1]; |
| 101 | + if (!everythingAfterProtocol) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + if (localhostDomainRE.test(everythingAfterProtocol) || nonLocalhostDomainRE.test(everythingAfterProtocol)) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | +} |
| 111 | + |
| 112 | +export function debounce<Args extends unknown[]>(fn: (...args: Args) => void, delay: number) { |
| 113 | + let timeoutID: number | undefined; |
| 114 | + let lastArgs: Args | undefined; |
| 115 | + |
| 116 | + const run = () => { |
| 117 | + if (lastArgs) { |
| 118 | + fn(...lastArgs); |
| 119 | + lastArgs = undefined; |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + const debounced = (...args: Args) => { |
| 124 | + clearTimeout(timeoutID); |
| 125 | + lastArgs = args; |
| 126 | + timeoutID = window.setTimeout(run, delay); |
| 127 | + }; |
| 128 | + |
| 129 | + debounced.flush = () => { |
| 130 | + clearTimeout(timeoutID); |
| 131 | + }; |
| 132 | + |
| 133 | + return debounced; |
| 134 | +} |
| 135 | + |
| 136 | +export function classNames(...args: any[]): string { |
| 137 | + var classes = []; |
| 138 | + |
| 139 | + for (var i = 0; i < arguments.length; i++) { |
| 140 | + var arg = arguments[i]; |
| 141 | + if (!arg) continue; |
| 142 | + |
| 143 | + var argType = typeof arg; |
| 144 | + |
| 145 | + if (argType === 'string' || argType === 'number') { |
| 146 | + classes.push(arg); |
| 147 | + } else if (Array.isArray(arg)) { |
| 148 | + if (arg.length) { |
| 149 | + var inner = classNames.apply(null, arg); |
| 150 | + if (inner) { |
| 151 | + classes.push(inner); |
| 152 | + } |
| 153 | + } |
| 154 | + } else if (argType === 'object') { |
| 155 | + if (arg.toString !== Object.prototype.toString) { |
| 156 | + classes.push(arg.toString()); |
| 157 | + } else { |
| 158 | + for (var key in arg) { |
| 159 | + if (hasOwn.call(arg, key) && arg[key]) { |
| 160 | + classes.push(key); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return classes.join(' '); |
| 168 | +} |
0 commit comments