-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Add
fetchEmojis
and fetchShortcodes
functions.
- Loading branch information
Showing
12 changed files
with
228 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ShortcodePreset, CompactEmoji, Emoji } from './types'; | ||
import fetchFromCDN, { FetchFromCDNOptions } from './fetchFromCDN'; | ||
import fetchShortcodes from './fetchShortcodes'; | ||
|
||
export interface FetchEmojisOptions extends FetchFromCDNOptions { | ||
compact?: boolean; | ||
shortcodes?: ShortcodePreset[]; | ||
} | ||
|
||
// Compact | ||
async function fetchEmojis( | ||
locale: string, | ||
options: FetchEmojisOptions & { compact: true }, | ||
): Promise<CompactEmoji[]>; | ||
|
||
// Full | ||
async function fetchEmojis( | ||
locale: string, | ||
options?: FetchEmojisOptions & { compact: false }, | ||
): Promise<Emoji[]>; | ||
|
||
async function fetchEmojis(locale: string, options: FetchEmojisOptions = {}) { | ||
const { compact = false, shortcodes: presets = [], ...opts } = options; | ||
const emojis = await fetchFromCDN(`${locale}/${compact ? 'compact' : 'data'}.json`, opts); | ||
|
||
if (presets.length > 0) { | ||
await Promise.all(presets.map((preset) => fetchShortcodes(locale, preset, opts))); | ||
} | ||
|
||
return emojis; | ||
} | ||
|
||
export default fetchEmojis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ShortcodePreset, ShortcodesDataset } from './types'; | ||
import fetchFromCDN, { FetchFromCDNOptions } from './fetchFromCDN'; | ||
import { NON_LATIN_LOCALES } from './constants'; | ||
|
||
export default function fetchShortcodes( | ||
locale: string, | ||
preset: ShortcodePreset, | ||
options?: FetchFromCDNOptions, | ||
): Promise<ShortcodesDataset> { | ||
if (preset === 'cldr-native' && !NON_LATIN_LOCALES.includes(locale)) { | ||
return Promise.resolve({}); | ||
} | ||
|
||
return fetchFromCDN(`${locale}/shortcodes/${preset}.json`, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fetchEmojis from '../src/fetchEmojis'; | ||
import { setupFetch } from './helpers'; | ||
|
||
describe('fetchEmojis()', () => { | ||
beforeEach(() => { | ||
setupFetch(); | ||
}); | ||
|
||
it('triggers a fetch', async () => { | ||
await fetchEmojis('de'); | ||
|
||
expect(global.fetch).toHaveBeenCalledWith( | ||
'https://cdn.jsdelivr.net/npm/emojibase-data@latest/de/data.json', | ||
{ | ||
credentials: 'omit', | ||
mode: 'cors', | ||
redirect: 'error', | ||
}, | ||
); | ||
}); | ||
|
||
it('triggers a fetch (compact)', async () => { | ||
await fetchEmojis('ko', { compact: true, version: '1.2.3' }); | ||
|
||
expect(global.fetch).toHaveBeenCalledWith( | ||
'https://cdn.jsdelivr.net/npm/emojibase-data@1.2.3/ko/compact.json', | ||
{ | ||
credentials: 'omit', | ||
mode: 'cors', | ||
redirect: 'error', | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import fetchShortcodes from '../src/fetchShortcodes'; | ||
import { setupFetch } from './helpers'; | ||
|
||
describe('fetchShortcodes()', () => { | ||
beforeEach(() => { | ||
setupFetch({ '0000': 'shortcode' }); | ||
}); | ||
|
||
it('triggers a fetch', async () => { | ||
await fetchShortcodes('de', 'cldr'); | ||
|
||
expect(global.fetch).toHaveBeenCalledWith( | ||
'https://cdn.jsdelivr.net/npm/emojibase-data@latest/de/shortcodes/cldr.json', | ||
{ | ||
credentials: 'omit', | ||
mode: 'cors', | ||
redirect: 'error', | ||
}, | ||
); | ||
}); | ||
|
||
it('returns an empty dataset for `cldr-native` and an invalid locale', async () => { | ||
const res1 = await fetchShortcodes('ja', 'cldr-native'); | ||
|
||
expect(res1).not.toEqual({}); | ||
|
||
const res2 = await fetchShortcodes('en', 'cldr-native'); | ||
|
||
expect(res2).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable */ | ||
|
||
// @ts-expect-error | ||
import regeneratorRuntime from 'regenerator-runtime'; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface Global { | ||
fetch: any; | ||
sessionStorage: any; | ||
localStorage: any; | ||
regeneratorRuntime: any; | ||
} | ||
} | ||
} | ||
|
||
export function setupFetch(response: unknown = [1, 2, 3]) { | ||
global.regeneratorRuntime = regeneratorRuntime; | ||
|
||
Object.defineProperty(global, 'fetch', { | ||
configurable: true, | ||
value: jest.fn(() => | ||
Promise.resolve({ | ||
json: () => response, | ||
ok: true, | ||
}), | ||
), | ||
}); | ||
|
||
Object.defineProperty(global, 'sessionStorage', { | ||
configurable: true, | ||
value: { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
}, | ||
}); | ||
|
||
Object.defineProperty(global, 'localStorage', { | ||
configurable: true, | ||
value: { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.