aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl/PrefetchAndCache.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-05-02 17:25:56 +0900
committerGitHub <noreply@github.com>2020-05-02 17:25:56 +0900
commit5df0537bcf65a341e79852b1b30379c73318529c (patch)
treeaee5efe52412855f620cb514a13a2c14373f27b7 /src/background/completion/impl/PrefetchAndCache.ts
parent685f2b7b69218b06b5bb676069e35f79c5048c9b (diff)
parent75abd90ecb8201ad845b266f96220d8adfe19b2d (diff)
Merge pull request #749 from ueokande/qa-0.28
QA 0.28
Diffstat (limited to 'src/background/completion/impl/PrefetchAndCache.ts')
-rw-r--r--src/background/completion/impl/PrefetchAndCache.ts37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/background/completion/impl/PrefetchAndCache.ts b/src/background/completion/impl/PrefetchAndCache.ts
index 3c074c2..d2889b0 100644
--- a/src/background/completion/impl/PrefetchAndCache.ts
+++ b/src/background/completion/impl/PrefetchAndCache.ts
@@ -5,14 +5,14 @@ const WHITESPACE = /\s/;
// `shortKey` returns a shorten key to pre-fetch completions and store in the
// cache. The shorten key is generated by the following rules:
-//
+//
// 1. If the query contains a space in the middle: i.e. the query consists of
// multiple words, the method removes the last word from the query, and
// returns joined remaining words with space.
-//
+//
// 2. If the query is a single word and it's an URL, the method returns a new
// URL excluding search query with the upper path of the original URL.
-//
+//
// 3. If the query is a single word and it's not an URL, the method returns a
// word with the half-length of the original query.
//
@@ -29,11 +29,15 @@ const WHITESPACE = /\s/;
//
export const shortKey = (query: string): string => {
if (WHITESPACE.test(query)) {
- return query.split(WHITESPACE).filter(word => word.length > 0).slice(0, -1).join(' ');
+ return query
+ .split(WHITESPACE)
+ .filter((word) => word.length > 0)
+ .slice(0, -1)
+ .join(" ");
}
let url;
try {
- url = new URL(query)
+ url = new URL(query);
} catch (e) {
return query.slice(0, query.length / 2);
}
@@ -42,12 +46,12 @@ export const shortKey = (query: string): string => {
// may be on typing or removing URLs such as "such as https://goog"
return query.slice(0, query.length / 2);
}
- if (url.pathname.endsWith('/')) {
+ if (url.pathname.endsWith("/")) {
// remove parameters and move to upper path
- return new URL('..', url).href;
+ return new URL("..", url).href;
}
// remove parameters
- return new URL('.', url).href;
+ return new URL(".", url).href;
};
export default class PrefetchAndCache<T> {
@@ -58,9 +62,8 @@ export default class PrefetchAndCache<T> {
constructor(
private getter: Getter<T>,
private filter: Filter<T>,
- private prefetchThrethold: number = 1,
- ) {
- }
+ private prefetchThrethold: number = 1
+ ) {}
async get(query: string): Promise<T[]> {
query = query.trim();
@@ -79,7 +82,7 @@ export default class PrefetchAndCache<T> {
private needToRefresh(query: string): boolean {
if (!this.shortKey) {
// no cache
- return true
+ return true;
}
if (query.length < this.shortKey.length) {
@@ -89,17 +92,17 @@ export default class PrefetchAndCache<T> {
}
if (!query.startsWith(this.shortKey)) {
- // queyr: "hello_w"
- // shorten: "hello_morning"
- return true
+ // queyr: "hello_w"
+ // shorten: "hello_morning"
+ return true;
}
- if (query.slice(this.shortKey.length).includes(' ')) {
+ if (query.slice(this.shortKey.length).includes(" ")) {
// queyr: "hello x"
// shorten: "hello"
return true;
}
- return false
+ return false;
}
}