Ghost/ghost/member-attribution/lib/history.js
Simon Backx fe3430202a
Fixed member attribution for subdirectories (#15277)
fixes https://github.com/TryGhost/Team/issues/1829

- Remove the subdirectories when creating the Attribution instances
- URLs are now always stored relative to the subdirectory instead of the root directory (makes changing the subdirectory easier)
- Fixed returning absolute urls
- Added tests
2022-08-22 17:16:18 +02:00

40 lines
927 B
JavaScript

/**
* @typedef {UrlHistoryItem[]} UrlHistoryArray
*/
/**
* @typedef {Object} UrlHistoryItem
* @prop {string} path
* @prop {number} time
*/
/**
* Represents a validated history
*/
class UrlHistory {
constructor(urlHistory) {
this.history = urlHistory && UrlHistory.isValidHistory(urlHistory) ? urlHistory : [];
}
get length() {
return this.history.length;
}
/**
* Iterate from latest item to newest item (reversed!)
*/
*[Symbol.iterator]() {
yield* this.history.slice().reverse();
}
static isValidHistory(history) {
return Array.isArray(history) && !history.find(item => !this.isValidHistoryItem(item));
}
static isValidHistoryItem(item) {
return !!item && !!item.path && !!item.time && typeof item.path === 'string' && typeof item.time === 'number' && Number.isSafeInteger(item.time);
}
}
module.exports = UrlHistory;