add more apis

This commit is contained in:
Nolan Lawson 2020-05-08 16:16:18 -07:00
parent 221b682de2
commit ea9310cb62
4 changed files with 71 additions and 39 deletions

View file

@ -46,7 +46,7 @@ export class IndexedDBEngine {
})
}
searchEmojiByPrefix(prefix) {
getEmojiBySearchPrefix(prefix) {
prefix = prefix.toLowerCase()
return dbPromise(this._db, STORE_EMOJI, MODE_READONLY, (emojiStore, cb) => {
const range = IDBKeyRange.bound(prefix, prefix + '\uffff', false, true)
@ -55,4 +55,23 @@ export class IndexedDBEngine {
}
})
}
getEmojiByShortcode(shortcode) {
shortcode = shortcode.toLowerCase()
return dbPromise(this._db, STORE_EMOJI, MODE_READONLY, (emojiStore, cb) => {
const range = IDBKeyRange.only(shortcode)
emojiStore.index(INDEX_TOKENS).getAll(range).onsuccess = e => {
// of course, we could add an extra index just for shortcodes, but it seems
// simpler to just re-use the existing tokens index and filter in-memory
const results = e.target.result.filter(emoji => emoji.shortcodes.includes(shortcode))
cb(results[0])
}
})
}
getEmojiByUnicode(unicode) {
return dbPromise(this._db, STORE_EMOJI, MODE_READONLY, (emojiStore, cb) => {
emojiStore.get(unicode).onsuccess = e => cb(e.target.result)
})
}
}

50
src/database.js Normal file
View file

@ -0,0 +1,50 @@
import { IndexedDBEngine } from './IndexedDBEngine'
let idbEngine
function verifyNonEmptyString(str) {
if (typeof str !== 'string' || !str) {
throw new Error('expected a non-empty string, got: ' + str)
}
}
async function init () {
if (!idbEngine) {
idbEngine = new IndexedDBEngine()
await idbEngine.readyPromise
}
}
export async function loadData (emojiBaseData) {
if (!emojiBaseData || !Array.isArray(emojiBaseData)) {
throw new Error('Expected emojibase data, got: ' + emojiBaseData)
}
await init()
await idbEngine.loadData(emojiBaseData)
}
export async function getEmojiByGroup (group) {
if (typeof group !== 'number') {
throw new Error('group must be a number, got: ' + group)
}
await init()
return idbEngine.getEmojiByGroup(group)
}
export async function getEmojiBySearchPrefix (prefix) {
verifyNonEmptyString(prefix)
await init()
return idbEngine.getEmojiBySearchPrefix(prefix)
}
export async function getEmojiByShortcode (shortcode) {
verifyNonEmptyString(shortcode)
await init()
return idbEngine.getEmojiByShortcode(shortcode)
}
export async function getEmojiByUnicode (unicode) {
verifyNonEmptyString(unicode)
await init()
return idbEngine.getEmojiByUnicode(unicode)
}

View file

@ -1,7 +1 @@
import { loadData, getEmojiByGroup, searchEmojiByPrefix } from './loadData'
export {
loadData,
getEmojiByGroup,
searchEmojiByPrefix
}
export * from './database'

View file

@ -1,31 +0,0 @@
import { IndexedDBEngine } from './IndexedDBEngine'
let idbEngine
async function init () {
if (!idbEngine) {
idbEngine = new IndexedDBEngine()
await idbEngine.readyPromise
}
}
export async function loadData (emojiBaseData) {
await init()
await idbEngine.loadData(emojiBaseData)
}
export async function getEmojiByGroup (group) {
if (typeof group !== 'number') {
throw new Error('group must be a number, got: ' + group)
}
await init()
return idbEngine.getEmojiByGroup(group)
}
export async function searchEmojiByPrefix (prefix) {
if (typeof prefix !== 'string' || !prefix) {
throw new Error('expected a non-empty string, got: ' + prefix)
}
await init()
return idbEngine.searchEmojiByPrefix(prefix)
}