-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
file-namer.js
61 lines (54 loc) · 1.74 KB
/
file-namer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
/**
* @fileoverview
* @suppress {reportUnknownTypes}
*/
/**
* Generate a filenamePrefix of name_YYYY-MM-DD_HH-MM-SS
* Date/time uses the local timezone, however Node has unreliable ICU
* support, so we must construct a YYYY-MM-DD date format manually. :/
* @param {string} name
* @param {string|undefined} fetchTime
*/
function getFilenamePrefix(name, fetchTime) {
const date = fetchTime ? new Date(fetchTime) : new Date();
const timeStr = date.toLocaleTimeString('en-US', {hour12: false});
const dateParts = date.toLocaleDateString('en-US', {
year: 'numeric', month: '2-digit', day: '2-digit',
}).split('/');
// @ts-expect-error - parts exists
dateParts.unshift(dateParts.pop());
const dateStr = dateParts.join('-');
const filenamePrefix = `${name}_${dateStr}_${timeStr}`;
// replace characters that are unfriendly to filenames
return filenamePrefix.replace(/[/?<>\\:*|"]/g, '-');
}
/**
* Generate a filenamePrefix of hostname_YYYY-MM-DD_HH-MM-SS.
* @param {{finalDisplayedUrl: string, fetchTime: string}} lhr
* @return {string}
*/
function getLhrFilenamePrefix(lhr) {
const hostname = new URL(lhr.finalDisplayedUrl).hostname;
return getFilenamePrefix(hostname, lhr.fetchTime);
}
/**
* Generate a filenamePrefix of name_YYYY-MM-DD_HH-MM-SS.
* @param {{name: string, steps: Array<{lhr: {fetchTime: string}}>}} flowResult
* @return {string}
*/
function getFlowResultFilenamePrefix(flowResult) {
const lhr = flowResult.steps[0].lhr;
const name = flowResult.name.replace(/\s/g, '-');
return getFilenamePrefix(name, lhr.fetchTime);
}
export {
getLhrFilenamePrefix,
getFilenamePrefix,
getFlowResultFilenamePrefix,
};