Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Example offsets (please take DST into account!):
// -8 = America, Los Angeles
// -5 = America, New York
// +1 = Europe, Amsterdam
// +8 = Australia, Perth
// +9.5 = Australia, Darwin
const timeZoneOffset = +1;
const makeSlug = function (text) {
return text.replace(/\/+/g, '-');
};
const makeFileSystemSafe = function (text, replacement = '', maxLength = 255) {
const illegal = /[\/\?<>\\:\*\|":]/g;
const control = /[\x00-\x1f\x80-\x9f]/g;
const reserved = /^\.+$/;
const windowsReserved = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
const windowsTrailing = /[\. ]+$/;
const sanitizedText = text
.replace(illegal, replacement)
.replace(control, replacement)
.replace(reserved, replacement)
.replace(windowsReserved, replacement)
.replace(windowsTrailing, replacement);
return sanitizedText.slice(0, maxLength);
};
const formatDate = function (utcDateAsText, timeZoneOffset) {
const utcDate = new Date(utcDateAsText);
const localDate = new Date(utcDate.getTime() + (3600000 * timeZoneOffset));
const pad = (number) => {
return (number < 10) ? '0' + number : number;
};
return localDate.getFullYear() +
'-' + pad(localDate.getMonth() + 1) +
'-' + pad(localDate.getDate()) +
' ' + pad(localDate.getHours()) +
':' + pad(localDate.getMinutes()) +
':' + pad(localDate.getSeconds());
};
const createScreenshotFileName = function (name, date, timeZoneOffset) {
return makeFileSystemSafe(formatDate(date, timeZoneOffset), '-') + '-' + makeFileSystemSafe(makeSlug(name), '', 150);
};
output = {}; // Used by Zapier
output['filename'] = createScreenshotFileName(inputData.name, inputData.dateCreated, timeZoneOffset);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.