| 1 |
/** Takes each possible code section and filters out undefined */ |
| 2 |
export const findTheCode = (item) => |
| 3 |
[item?.template?.code, item?.template?.code2].filter(Boolean).join('') |
| 4 |
|
| 5 |
/** Removes any hash or qs values from URL - Airtable adds timestamps */ |
| 6 |
export const stripUrlParams = (url) => url?.[0]?.url?.split(/[?#]/)?.[0] |
| 7 |
|
| 8 |
/** Lowers the quality of images */ |
| 9 |
export const lowerImageQuality = (html) => { |
| 10 |
return html.replace(/\w+:\/\/\S*(w=(\d*))&\w+\S*"/g, (url, w, width) => |
| 11 |
// Could lower the width here if needed |
| 12 |
url.replace(w, 'w=' + Math.floor(Number(width)) + '&q=10'), |
| 13 |
) |
| 14 |
} |
| 15 |
|
| 16 |
/** Capitalize first letter of a string */ |
| 17 |
export const capitalize = (str) => |
| 18 |
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() |
| 19 |
|
| 20 |
export const runAtLeastFor = async (functionPromise, time, options) => { |
| 21 |
if (options.dryRun) { |
| 22 |
return new Promise((resolve) => setTimeout(resolve, time)) |
| 23 |
} |
| 24 |
const start = Date.now() |
| 25 |
try { |
| 26 |
return await Promise.all([ |
| 27 |
await functionPromise(), |
| 28 |
new Promise((resolve) => setTimeout(resolve, time)), |
| 29 |
]) |
| 30 |
} catch (error) { |
| 31 |
console.error(error) |
| 32 |
return await new Promise((resolve) => |
| 33 |
// Check at least min milliseconds have passed |
| 34 |
setTimeout(resolve, Math.max(0, time - (Date.now() - start))), |
| 35 |
) |
| 36 |
} |
| 37 |
} |
| 38 |
|