simplify history influencer

This commit is contained in:
Cade Scroggins
2022-02-21 00:55:31 -08:00
parent 2e1e332ae2
commit d8c63e4a9e
+20 -37
View File
@@ -121,7 +121,7 @@
m: ['m/mail/u/1', 'm/mail/u/2'], m: ['m/mail/u/1', 'm/mail/u/2'],
o: ['o/discover/sets/new-for-you', 'o/discover/sets/weekly'], o: ['o/discover/sets/new-for-you', 'o/discover/sets/weekly'],
p: ['p/beema.finance'], p: ['p/beema.finance'],
r: ['r/r/startpages', 'r/r/onebag', 'r/r/fujix', 'r/r/superstonk'], r: ['r/r/startpages', 'r/r/unixporn', 'r/r/onebag', 'r/r/fujix'],
s: ['s/collection/tracks', 's/playlist/37i9dQZEVXcXr3r4FYT3J7'], s: ['s/collection/tracks', 's/playlist/37i9dQZEVXcXr3r4FYT3J7'],
u: ['u/explore', 'u/backgrounds'], u: ['u/explore', 'u/backgrounds'],
y: ['y/feed/trending'], y: ['y/feed/trending'],
@@ -844,7 +844,7 @@
} }
getSuggestions(parsedQuery) { getSuggestions(parsedQuery) {
const { query } = parsedQuery; const { lower, query } = parsedQuery;
if (this._isTooShort(query)) return Promise.resolve([]); if (this._isTooShort(query)) return Promise.resolve([]);
return new Promise((resolve) => { return new Promise((resolve) => {
@@ -853,7 +853,7 @@
this._addSearchPrefix( this._addSearchPrefix(
res res
.map((i) => i.phrase) .map((i) => i.phrase)
.filter((s) => s.toLowerCase() !== query.toLowerCase()) .filter((s) => s.toLowerCase() !== lower)
.slice(0, this._limit), .slice(0, this._limit),
parsedQuery parsedQuery
) )
@@ -872,67 +872,49 @@
this._storeName = 'history'; this._storeName = 'history';
} }
static clearHistory() { addItem({ isPath, lower }) {
localStorage.clear(); if (isPath || this._isTooShort(lower)) return;
}
addItem({ isPath, query }) {
if (isPath || query.length < this._minChars) return;
let exists; let exists;
const history = this._getHistory().map(([item, count]) => { const history = this._getHistory().map(([item, count]) => {
const match = item.toLowerCase() === query.toLowerCase(); const match = item === lower;
if (match) exists = true; if (match) exists = true;
return [item, match ? count + 1 : count]; return [item, match ? count + 1 : count];
}); });
if (!exists) history.push([query, 1]); if (!exists) history.push([lower, 1]);
this._setHistory(history.sort((a, b) => b[1] - a[1]));
const sorted = history
.sort((current, next) => current[1] - next[1])
.reverse();
this._setHistory(sorted);
} }
getSuggestions(parsedQuery) { getSuggestions(parsedQuery) {
const { query } = parsedQuery; const { lower } = parsedQuery;
if (this._isTooShort(query)) return Promise.resolve([]); if (this._isTooShort(lower)) return Promise.resolve([]);
return new Promise((resolve) => return new Promise((resolve) =>
resolve( resolve(
this._addSearchPrefix( this._addSearchPrefix(
this._getHistory() this._getHistory()
.map(([item]) => item) .filter(([item]) => item !== lower && item.includes(lower))
.filter( .slice(0, this._limit)
(item) => .map(([item]) => item),
query &&
item.toLowerCase() !== query.toLowerCase() &&
item.toLowerCase().includes(query.toLowerCase())
)
.slice(0, this._limit),
parsedQuery parsedQuery
) )
) )
); );
} }
_fetch() {
return JSON.parse(localStorage.getItem(this._storeName)) || [];
}
_getHistory() { _getHistory() {
this._history = this._history || this._fetch(); this._history =
return this._history; this._history ||
} JSON.parse(localStorage.getItem(this._storeName)) ||
[];
_save(history) { return this._history;
localStorage.setItem(this._storeName, JSON.stringify(history));
} }
_setHistory(history) { _setHistory(history) {
this._history = history; this._history = history;
this._save(history); localStorage.setItem(this._storeName, JSON.stringify(history));
} }
} }
@@ -1136,6 +1118,7 @@
const res = []; const res = [];
res.raw = query.trim(); res.raw = query.trim();
res.query = res.raw; res.query = res.raw;
res.lower = res.raw.toLowerCase();
res.split = null; res.split = null;
if (this._urlRegex.test(query)) { if (this._urlRegex.test(query)) {