fix: simplify api

This commit is contained in:
Nolan Lawson 2020-06-14 23:38:43 -07:00
parent 807bf24edd
commit 92100a9b5d
8 changed files with 44 additions and 69 deletions

View File

@ -320,25 +320,9 @@ Note that as soon as any other non-close/delete method is called, the database w
___
#### getCustomEmojiByName
**getCustomEmojiByName**(`name`: string): *[CustomEmoji | null*
Return the custom emoji corresponding to the string name, or null if not found. Throws if the name is empty.
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`name` | string | |
**Returns:** *CustomEmoji | null*
___
#### getEmojiByGroup
**getEmojiByGroup**(`group`: number): *PromiseNativeEmoji]*
**getEmojiByGroup**(`group`: number): *Promise[NativeEmoji]*
Returns all emoji belonging to a group, ordered by `order`. Only returns native emoji;
custom emoji don't belong to a group.
@ -393,11 +377,12 @@ Name | Type | Description |
___
#### getEmojiByUnicode
#### getEmojiByUnicodeOrName
**getEmojiByUnicode**(`unicode`: string): *PromiseNativeEmoji | null*
**getEmojiByUnicodeOrName**(`unicodeOrName`: string): *PromiseEmoji | null*
Return a single emoji matching the unicode string, or null if not found.
Return a single native emoji matching the unicode string, or
a custom emoji matching the name, or null if not found.
Empty/null strings throw an error.
@ -405,9 +390,9 @@ Empty/null strings throw an error.
Name | Type | Description |
------ | ------ | ------ |
`unicode` | string | unicode string |
`unicodeOrName` | string | unicode (native emoji) or name (custom emoji) |
**Returns:** *PromiseNativeEmoji | null*
**Returns:** *PromiseEmoji | null*
___

View File

@ -112,10 +112,14 @@ export default class Database {
return getEmojiByShortcode(this._db, shortcode)
}
async getEmojiByUnicode (unicode) {
assertNonEmptyString(unicode)
async getEmojiByUnicodeOrName (unicodeOrName) {
assertNonEmptyString(unicodeOrName)
await this.ready()
return getEmojiByUnicode(this._db, unicode)
const custom = this._custom.byName(unicodeOrName)
if (custom) {
return custom
}
return getEmojiByUnicode(this._db, unicodeOrName)
}
async getPreferredSkinTone () {
@ -149,11 +153,6 @@ export default class Database {
return this._custom.all
}
getCustomEmojiByName (name) {
assertNonEmptyString(name)
return this._custom.byName(name) || null
}
async _shutdown () {
await this.ready() // reopen if we've already been closed/deleted
try {

View File

@ -158,7 +158,7 @@ $: {
async function updateDefaultFavoriteEmojis () {
if (database) {
defaultFavoriteEmojis = (await Promise.all(MOST_COMMONLY_USED_EMOJI.map(unicode => (
database.getEmojiByUnicode(unicode)
database.getEmojiByUnicodeOrName(unicode)
)))).filter(Boolean) // filter because in Jest tests we don't have all the emoji in the DB
}
}
@ -387,8 +387,7 @@ function fireEvent (name, detail) {
}
async function clickEmoji (unicodeOrName) {
const emoji = database.getCustomEmojiByName(unicodeOrName) ||
await database.getEmojiByUnicode(unicodeOrName)
const emoji = await database.getEmojiByUnicodeOrName(unicodeOrName)
const emojiSummary = [...currentEmojis, ...currentFavorites]
.find(_ => (_.unicode === unicodeOrName || _.name === unicodeOrName))
const skinTonedUnicode = emojiSummary.unicode && unicodeWithSkin(emojiSummary, currentSkinTone)

View File

@ -65,12 +65,13 @@ export default class Database {
}
/**
* Return a single emoji matching the unicode string, or null if not found.
* Return a single native emoji matching the unicode string, or
* a custom emoji matching the name, or null if not found.
*
* Empty/null strings throw an error.
* @param unicode - unicode string
* @param unicodeOrName - unicode (native emoji) or name (custom emoji)
*/
getEmojiByUnicode(unicode: string): Promise<NativeEmoji | null> {
getEmojiByUnicodeOrName(unicodeOrName: string): Promise<Emoji | null> {
return Promise.resolve(null)
}
@ -125,15 +126,6 @@ export default class Database {
return []
}
/**
* Return the custom emoji corresponding to the string name, or null if not found. Throws if the name is empty.
*
* @param name
*/
getCustomEmojiByName(name: string): CustomEmoji | null {
return null
}
/**
* Closes the underlying IndexedDB connection. The Database is not usable after that (or any other Databases
* with the same locale).

View File

@ -106,33 +106,33 @@ describe('database tests', () => {
test('close and then use afterwards should work okay', async () => {
const db = new Database({ dataSource: ALL_EMOJI })
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.close()
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.delete()
})
test('simultaneous closes', async () => {
const db = new Database({ dataSource: ALL_EMOJI })
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await Promise.all([db.close(), db.close()])
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.delete()
})
test('simultaneous close and delete', async () => {
const db = new Database({ dataSource: ALL_EMOJI })
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await Promise.all([db.close(), db.delete()])
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.delete()
})
test('delete and then use afterwards should work okay', async () => {
const db = new Database({ dataSource: ALL_EMOJI })
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.delete()
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.delete()
})
@ -142,9 +142,9 @@ describe('database tests', () => {
const db2 = new Database({ dataSource: ALL_EMOJI })
await db2.ready()
await db1.close()
expect((await db1.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db1.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db2.close()
expect((await db2.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db2.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
const db3 = new Database({ dataSource: ALL_EMOJI })
await db3.ready()
await db3.delete()
@ -174,7 +174,7 @@ describe('database tests', () => {
const db2 = new Database({ dataSource: ALL_EMOJI })
await db2.ready()
await db2._lazyUpdate
const queryPromise = db2.getEmojiByUnicode('🐵')
const queryPromise = db2.getEmojiByUnicodeOrName('🐵')
const closePromise = db1.close()
expect(await queryPromise)
await closePromise

View File

@ -220,12 +220,12 @@ describe('custom emoji', () => {
test('get custom emoji by name', async () => {
db.customEmoji = customEmojis
expect(db.getCustomEmojiByName('underscores_like_this')).toStrictEqual(
expect(await db.getEmojiByUnicodeOrName('underscores_like_this')).toStrictEqual(
{ name: 'underscores_like_this', shortcodes: ['underscores_like_this'], url: 'underscores.png' }
)
expect(db.getCustomEmojiByName('capitalletterslikethis')).toStrictEqual(
expect(await db.getEmojiByUnicodeOrName('capitalletterslikethis')).toStrictEqual(
{ name: 'CapitalLettersLikeThis', shortcodes: ['CapitalLettersLikeThis'], url: 'caps.png' }
)
expect(db.getCustomEmojiByName('unknown')).toBeNull()
expect(await db.getEmojiByUnicodeOrName('unknown')).toBeNull()
})
})

View File

@ -21,11 +21,11 @@ describe('getEmojiByUnicode', () => {
const db = new Database({ dataSource: EMOJI_WITH_PIRATES })
expect((await db.getEmojiByUnicode('😀')).annotation).toBe('grinning face')
expect((await db.getEmojiByUnicode(pirate.emoji)).annotation).toBe('pirate flag')
expect((await db.getEmojiByUnicode(pirate.emoji.substring(0, 1)))).toBe(null)
expect((await db.getEmojiByUnicode('smile'))).toBe(null)
expect((await db.getEmojiByUnicode('monkey_face'))).toBe(null)
expect((await db.getEmojiByUnicodeOrName('😀')).annotation).toBe('grinning face')
expect((await db.getEmojiByUnicodeOrName(pirate.emoji)).annotation).toBe('pirate flag')
expect((await db.getEmojiByUnicodeOrName(pirate.emoji.substring(0, 1)))).toBe(null)
expect((await db.getEmojiByUnicodeOrName('smile'))).toBe(null)
expect((await db.getEmojiByUnicodeOrName('monkey_face'))).toBe(null)
await db.delete()
})
@ -33,10 +33,10 @@ describe('getEmojiByUnicode', () => {
test('errors', async () => {
const db = new Database({ dataSource: ALL_EMOJI })
await expect(() => db.getEmojiByUnicode()).rejects.toThrow()
await expect(() => db.getEmojiByUnicode(1)).rejects.toThrow()
await expect(() => db.getEmojiByUnicode(null)).rejects.toThrow()
await expect(() => db.getEmojiByUnicode('')).rejects.toThrow()
await expect(() => db.getEmojiByUnicodeOrName()).rejects.toThrow()
await expect(() => db.getEmojiByUnicodeOrName(1)).rejects.toThrow()
await expect(() => db.getEmojiByUnicodeOrName(null)).rejects.toThrow()
await expect(() => db.getEmojiByUnicodeOrName('')).rejects.toThrow()
await db.delete()
})

View File

@ -20,7 +20,7 @@ describe('offline first', () => {
db = new Database({ dataSource: ALL_EMOJI })
await db.ready()
await expect(() => db._lazyUpdate).rejects.toThrow()
expect((await db.getEmojiByUnicode('🐵')).annotation).toBe('monkey face')
expect((await db.getEmojiByUnicodeOrName('🐵')).annotation).toBe('monkey face')
await db.close()
await db.delete()
})