diff --git a/database.d.ts b/database.d.ts index 0df6d67e..bdc53987 100644 --- a/database.d.ts +++ b/database.d.ts @@ -78,6 +78,10 @@ export default class Database { * @param limit - maximum number of results to return */ getTopFavoriteEmoji(limit: number): Promise; + /** + * Returns all native emojis, unordered. + */ + getAllNativeEmojis(): Promise; /** * Set the custom emoji for this database. Throws an error if custom emoji are not in the correct format. * diff --git a/src/database/Database.js b/src/database/Database.js index 2889f78d..d8256539 100644 --- a/src/database/Database.js +++ b/src/database/Database.js @@ -14,7 +14,7 @@ import { openDatabase } from './databaseLifecycle' import { - isEmpty, getEmojiByGroup, + isEmpty, getAllNativeEmojis, getEmojiByGroup, getEmojiBySearchQuery, getEmojiByShortcode, getEmojiByUnicode, get, set, getTopFavoriteEmoji, incrementFavoriteEmojiCount } from './idbInterface' @@ -125,6 +125,11 @@ export default class Database { return (await getTopFavoriteEmoji(this._db, this._custom, limit)).map(cleanEmoji) } + async getAllNativeEmojis () { + await this.ready() + return (await getAllNativeEmojis(this._db)).map(cleanEmoji) + } + set customEmoji (customEmojis) { this._custom = customEmojiIndex(customEmojis) } diff --git a/src/database/idbInterface.js b/src/database/idbInterface.js index 037c5065..d88e39ed 100644 --- a/src/database/idbInterface.js +++ b/src/database/idbInterface.js @@ -241,3 +241,9 @@ export function getTopFavoriteEmoji (db, customEmojiIndex, limit) { } }) } + +export async function getAllNativeEmojis (db) { + return dbPromise(db, STORE_EMOJI, MODE_READONLY, (emojiStore, txn, cb) => { + getAllIDB(emojiStore.index(INDEX_GROUP_AND_ORDER), undefined, cb) + }) +} diff --git a/test/spec/database/getAllNativeEmojis.test.js b/test/spec/database/getAllNativeEmojis.test.js new file mode 100644 index 00000000..bbde594c --- /dev/null +++ b/test/spec/database/getAllNativeEmojis.test.js @@ -0,0 +1,13 @@ +import { ALL_EMOJI, basicAfterEach, basicBeforeEach } from '../shared' +import Database from '../../../src/database/Database' + +describe('getAllNativeEmojis', () => { + beforeEach(basicBeforeEach) + afterEach(basicAfterEach) + test('basic test', async () => { + const db = new Database({ dataSource: ALL_EMOJI }) + + expect((await db.getAllNativeEmojis()).length).toBe(189) + await db.delete() + }) +})