2022-08-18 18:38:42 +03:00
|
|
|
/**
|
|
|
|
* @typedef {Object} UrlHistoryItem
|
|
|
|
* @prop {string} path
|
|
|
|
* @prop {number} time
|
|
|
|
*/
|
|
|
|
|
2022-08-25 19:21:08 +03:00
|
|
|
/**
|
|
|
|
* @typedef {UrlHistoryItem[]} UrlHistoryArray
|
|
|
|
*/
|
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
/**
|
|
|
|
* Represents a validated history
|
|
|
|
*/
|
|
|
|
class UrlHistory {
|
2022-08-25 19:21:08 +03:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {UrlHistoryArray} urlHistory
|
|
|
|
*/
|
2022-08-18 18:38:42 +03:00
|
|
|
constructor(urlHistory) {
|
2022-08-25 19:21:08 +03:00
|
|
|
/** @private */
|
|
|
|
this.history = urlHistory;
|
2022-08-18 18:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this.history.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate from latest item to newest item (reversed!)
|
|
|
|
*/
|
|
|
|
*[Symbol.iterator]() {
|
|
|
|
yield* this.history.slice().reverse();
|
|
|
|
}
|
|
|
|
|
2022-08-25 19:21:08 +03:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {any[]} history
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-08-18 18:38:42 +03:00
|
|
|
static isValidHistory(history) {
|
2022-08-25 19:21:08 +03:00
|
|
|
for (const item of history) {
|
|
|
|
if (typeof item?.path !== 'string' || !Number.isSafeInteger(item?.time)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2022-08-18 18:38:42 +03:00
|
|
|
}
|
|
|
|
|
2022-08-25 19:21:08 +03:00
|
|
|
/**
|
|
|
|
* @param {unknown} urlHistory
|
|
|
|
* @returns {UrlHistory}
|
|
|
|
*/
|
|
|
|
static create(urlHistory) {
|
|
|
|
if (!Array.isArray(urlHistory)) {
|
|
|
|
return new UrlHistory([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isValidHistory(urlHistory)) {
|
|
|
|
return new UrlHistory([]);
|
|
|
|
}
|
|
|
|
|
2022-08-25 19:21:41 +03:00
|
|
|
const now = Date.now();
|
|
|
|
const filteredHistory = urlHistory.filter((item) => {
|
|
|
|
return now - item.time < this.MAX_AGE;
|
|
|
|
});
|
|
|
|
|
|
|
|
return new UrlHistory(filteredHistory);
|
2022-08-18 18:38:42 +03:00
|
|
|
}
|
2022-08-25 19:21:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
static MAX_AGE = 1000 * 60 * 60 * 24;
|
2022-08-18 18:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UrlHistory;
|