Format src files with Prettier (#215)
Formats files in `src` with [prettier for consistent code style](https://prettier.io/docs/en/why-prettier) and adds npm script Related to https://github.com/webrecorder/replayweb.page/issues/201
This commit is contained in:
+33
-22
@@ -1,13 +1,14 @@
|
||||
// ===========================================================================
|
||||
async function digestMessage(message, hashtype) {
|
||||
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
|
||||
const hashBuffer = await crypto.subtle.digest(hashtype, msgUint8); // hash the message
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join(""); // convert bytes to hex string
|
||||
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
|
||||
const hashBuffer = await crypto.subtle.digest(hashtype, msgUint8); // hash the message
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
||||
const hashHex = hashArray
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join(""); // convert bytes to hex string
|
||||
return hashHex;
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================================
|
||||
function tsToDate(ts) {
|
||||
if (!ts) {
|
||||
@@ -18,12 +19,19 @@ function tsToDate(ts) {
|
||||
ts += "00000101000000".substr(ts.length);
|
||||
}
|
||||
|
||||
const datestr = (ts.substring(0, 4) + "-" +
|
||||
ts.substring(4, 6) + "-" +
|
||||
ts.substring(6, 8) + "T" +
|
||||
ts.substring(8, 10) + ":" +
|
||||
ts.substring(10, 12) + ":" +
|
||||
ts.substring(12, 14) + "-00:00");
|
||||
const datestr =
|
||||
ts.substring(0, 4) +
|
||||
"-" +
|
||||
ts.substring(4, 6) +
|
||||
"-" +
|
||||
ts.substring(6, 8) +
|
||||
"T" +
|
||||
ts.substring(8, 10) +
|
||||
":" +
|
||||
ts.substring(10, 12) +
|
||||
":" +
|
||||
ts.substring(12, 14) +
|
||||
"-00:00";
|
||||
|
||||
return new Date(datestr);
|
||||
}
|
||||
@@ -33,21 +41,19 @@ function getPageDateTS(page) {
|
||||
let date = null;
|
||||
try {
|
||||
date = new Date(page.ts || page.date);
|
||||
} catch (e) {
|
||||
} catch (e) {
|
||||
// leave date unchanged in case of error
|
||||
}
|
||||
|
||||
const timestamp = (date && !isNaN(date)) ? getTS(date.toISOString()) : "";
|
||||
return {date, timestamp};
|
||||
const timestamp = date && !isNaN(date) ? getTS(date.toISOString()) : "";
|
||||
return { date, timestamp };
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================================
|
||||
function getTS(iso) {
|
||||
return iso.replace(/[-:T]/g, "").slice(0, 14);
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================================
|
||||
function getReplayLink(view, url, ts) {
|
||||
const params = new URLSearchParams();
|
||||
@@ -57,7 +63,6 @@ function getReplayLink(view, url, ts) {
|
||||
return "#" + params.toString();
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================================
|
||||
async function sourceToId(url) {
|
||||
try {
|
||||
@@ -69,10 +74,9 @@ async function sourceToId(url) {
|
||||
|
||||
const digest = await digestMessage(url, "SHA-256");
|
||||
const coll = "id-" + digest.slice(0, 12);
|
||||
return {url, coll};
|
||||
return { url, coll };
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================================
|
||||
// simple parse of scheme, host, rest of path
|
||||
// not using URL due to different browser behavior
|
||||
@@ -102,8 +106,15 @@ function parseURLSchemeHostPath(url) {
|
||||
path = "";
|
||||
}
|
||||
|
||||
return {scheme, host, path};
|
||||
return { scheme, host, path };
|
||||
}
|
||||
|
||||
|
||||
export { digestMessage, tsToDate, getTS, getPageDateTS, getReplayLink, sourceToId, parseURLSchemeHostPath };
|
||||
export {
|
||||
digestMessage,
|
||||
tsToDate,
|
||||
getTS,
|
||||
getPageDateTS,
|
||||
getReplayLink,
|
||||
sourceToId,
|
||||
parseURLSchemeHostPath,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user