From 16593408981d59e1bd3ad4de14071f45a8116d81 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 23 Nov 2019 21:48:24 -0500 Subject: [PATCH] Add support for prefix wildcards --- ext/bg/js/database.js | 30 +++++++++++++++++++++++------- ext/bg/js/search.js | 10 +++++++--- ext/bg/js/translator.js | 8 ++++---- ext/mixed/js/core.js | 4 ++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index a20d5f15..2d309f85 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -28,7 +28,7 @@ class Database { } try { - this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => { + this.db = await Database.open('dict', 5, (db, transaction, oldVersion) => { Database.upgrade(db, transaction, oldVersion, [ { version: 2, @@ -76,6 +76,15 @@ class Database { indices: ['dictionary', 'expression', 'reading', 'sequence'] } } + }, + { + version: 5, + stores: { + terms: { + primaryKey: {keyPath: 'id', autoIncrement: true}, + indices: ['dictionary', 'expression', 'reading', 'sequence', 'expressionReverse', 'readingReverse'] + } + } } ]); }); @@ -143,14 +152,17 @@ class Database { } }; + const useWildcard = !!wildcard; + const prefixWildcard = wildcard === 'prefix'; + const dbTransaction = this.db.transaction(['terms'], 'readonly'); const dbTerms = dbTransaction.objectStore('terms'); - const dbIndex1 = dbTerms.index('expression'); - const dbIndex2 = dbTerms.index('reading'); + const dbIndex1 = dbTerms.index(prefixWildcard ? 'expressionReverse' : 'expression'); + const dbIndex2 = dbTerms.index(prefixWildcard ? 'readingReverse' : 'reading'); for (let i = 0; i < termList.length; ++i) { - const term = termList[i]; - const query = wildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term); + const term = prefixWildcard ? stringReverse(termList[i]) : termList[i]; + const query = useWildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term); promises.push( Database.getAll(dbIndex1, query, i, processRow), Database.getAll(dbIndex2, query, i, processRow) @@ -377,7 +389,9 @@ class Database { rules, score, glossary, - dictionary: summary.title + dictionary: summary.title, + expressionReverse: stringReverse(expression), + readingReverse: stringReverse(reading) }); } } else { @@ -391,7 +405,9 @@ class Database { glossary, sequence, termTags, - dictionary: summary.title + dictionary: summary.title, + expressionReverse: stringReverse(expression), + readingReverse: stringReverse(reading) }); } } diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index d2e0fd56..67a78075 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -207,10 +207,14 @@ class DisplaySearch extends Display { async onSearchQueryUpdated(query, animate) { try { const details = {}; - const match = /[*\uff0a]+$/.exec(query); + const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(query); if (match !== null) { - details.wildcard = true; - query = query.substring(0, query.length - match[0].length); + if (match[1]) { + details.wildcard = 'prefix'; + } else if (match[3]) { + details.wildcard = 'suffix'; + } + query = match[2]; } const valid = (query.length > 0); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 202014c9..0f3d0aa0 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -230,7 +230,7 @@ class Translator { const titles = Object.keys(dictionaries); const deinflections = ( details.wildcard ? - await this.findTermWildcard(text, titles) : + await this.findTermWildcard(text, titles, details.wildcard) : await this.findTermDeinflections(text, titles) ); @@ -268,8 +268,8 @@ class Translator { return [definitions, length]; } - async findTermWildcard(text, titles) { - const definitions = await this.database.findTermsBulk([text], titles, true); + async findTermWildcard(text, titles, wildcard) { + const definitions = await this.database.findTermsBulk([text], titles, wildcard); if (definitions.length === 0) { return []; } @@ -308,7 +308,7 @@ class Translator { deinflectionArray.push(deinflection); } - const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, false); + const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, null); for (const definition of definitions) { const definitionRules = Deinflector.rulesToRuleFlags(definition.rules); diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index f0c04457..b9536391 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -118,6 +118,10 @@ function toIterable(value) { throw new Error('Could not convert to iterable'); } +function stringReverse(string) { + return string.split('').reverse().join('').replace(/([\uDC00-\uDFFF])([\uD800-\uDBFF])/g, '$2$1'); +} + /* * Async utilities