Skip to content

Instantly share code, notes, and snippets.

@stillio-support
Created February 28, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stillio-support/6ee7ea65c966805a6f93f57362023430 to your computer and use it in GitHub Desktop.
Save stillio-support/6ee7ea65c966805a6f93f57362023430 to your computer and use it in GitHub Desktop.
// 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