2
0
mirror of https://github.com/iconify/iconify.git synced 2024-09-20 01:09:04 +00:00

Remove combined exports from core, they make it more difficult to use ES named exports

This commit is contained in:
Vjacheslav Trushkin 2021-09-24 18:38:52 +03:00
parent 691057527b
commit c28ad01412
17 changed files with 158 additions and 172 deletions

View File

@ -2,7 +2,7 @@ import crossFetch from 'cross-fetch';
import { sendAPIQuery } from '@iconify/core/lib/api/query'; import { sendAPIQuery } from '@iconify/core/lib/api/query';
import { setAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIModule } from '@iconify/core/lib/api/modules';
import { fetchAPIModule, setFetch } from '@iconify/core/lib/api/modules/fetch'; import { fetchAPIModule, setFetch } from '@iconify/core/lib/api/modules/fetch';
import { setAPIConfig } from '@iconify/core/lib/api/config'; import { addAPIProvider } from '@iconify/core/lib/api/config';
import { mockAPIModule } from '@iconify/core/lib/api/modules/mock'; import { mockAPIModule } from '@iconify/core/lib/api/modules/mock';
describe('Testing live API with fetch', () => { describe('Testing live API with fetch', () => {
@ -43,7 +43,7 @@ describe('Testing live API with fetch', () => {
it('Custom request with provider', (done) => { it('Custom request with provider', (done) => {
const provider = nextProvider(); const provider = nextProvider();
expect( expect(
setAPIConfig(provider, { addAPIProvider(provider, {
resources: [host], resources: [host],
}) })
).toBe(true); ).toBe(true);

View File

@ -1,4 +1,4 @@
import { setAPIConfig } from '@iconify/core/lib/api/config'; import { addAPIProvider } from '@iconify/core/lib/api/config';
import { setAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIModule } from '@iconify/core/lib/api/modules';
import { loadIcons, isPending } from '@iconify/core/lib/api/icons'; import { loadIcons, isPending } from '@iconify/core/lib/api/icons';
@ -17,7 +17,7 @@ describe('Testing API loadIcons', () => {
let asyncCounter = 0; let asyncCounter = 0;
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
}); });
@ -146,7 +146,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
}); });
@ -246,7 +246,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });
@ -342,7 +342,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });
@ -497,7 +497,7 @@ describe('Testing API loadIcons', () => {
const prefix2 = nextPrefix(); const prefix2 = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });

View File

@ -1,4 +1,4 @@
import { setAPIConfig } from '@iconify/core/lib/api/config'; import { addAPIProvider } from '@iconify/core/lib/api/config';
import { setAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIModule } from '@iconify/core/lib/api/modules';
import { loadIcons } from '@iconify/core/lib/api/icons'; import { loadIcons } from '@iconify/core/lib/api/icons';
import { mockAPIModule, mockAPIData } from '@iconify/core/lib/api/modules/mock'; import { mockAPIModule, mockAPIData } from '@iconify/core/lib/api/modules/mock';
@ -16,7 +16,7 @@ describe('Testing mock API module', () => {
const provider = nextPrefix(); const provider = nextPrefix();
beforeEach(() => { beforeEach(() => {
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local'], resources: ['https://api1.local'],
}); });
setAPIModule(provider, mockAPIModule); setAPIModule(provider, mockAPIModule);

View File

@ -1,4 +1,4 @@
import { setAPIConfig, getAPIConfig } from '@iconify/core/lib/api/config'; import { addAPIProvider, getAPIConfig } from '@iconify/core/lib/api/config';
import { setAPIModule, getAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIModule, getAPIModule } from '@iconify/core/lib/api/modules';
describe('Testing API modules', () => { describe('Testing API modules', () => {
@ -28,7 +28,7 @@ describe('Testing API modules', () => {
const provider = nextPrefix(); const provider = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://localhost:3000'], resources: ['https://localhost:3000'],
maxURL: 500, maxURL: 500,
}); });

View File

@ -1,6 +1,10 @@
import { fullIcon } from '@iconify/utils/lib/icon'; import { fullIcon } from '@iconify/utils/lib/icon';
import { listIcons } from '@iconify/core/lib/storage/storage';
import { import {
storageFunctions, iconExists,
getIcon,
addIcon,
addCollection,
allowSimpleNames, allowSimpleNames,
} from '@iconify/core/lib/storage/functions'; } from '@iconify/core/lib/storage/functions';
@ -16,18 +20,18 @@ describe('Testing IconifyStorageFunctions', () => {
const testName = `@${provider}:foo:bar`; const testName = `@${provider}:foo:bar`;
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
expect(storageFunctions.listIcons(provider)).toEqual([]); expect(listIcons(provider)).toEqual([]);
// Add and test one icon // Add and test one icon
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(true); ).toBe(true);
expect(storageFunctions.iconExists(testName)).toBe(true); expect(iconExists(testName)).toBe(true);
expect(storageFunctions.listIcons(provider)).toEqual([testName]); expect(listIcons(provider)).toEqual([testName]);
}); });
it('Invalid icon name', () => { it('Invalid icon name', () => {
@ -37,16 +41,16 @@ describe('Testing IconifyStorageFunctions', () => {
allowSimpleNames(false); allowSimpleNames(false);
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
// Add and test one icon (icon should not be added) // Add and test one icon (icon should not be added)
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(false); ).toBe(false);
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
}); });
it('Invalid icon set', () => { it('Invalid icon set', () => {
@ -55,7 +59,7 @@ describe('Testing IconifyStorageFunctions', () => {
// Icon set without prefix (should work only when simple names are allowed, tested later in this file) // Icon set without prefix (should work only when simple names are allowed, tested later in this file)
expect( expect(
storageFunctions.addCollection({ addCollection({
prefix: '', prefix: '',
icons: { icons: {
foo: { foo: {
@ -73,16 +77,16 @@ describe('Testing IconifyStorageFunctions', () => {
allowSimpleNames(true); allowSimpleNames(true);
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
// Add and test one icon // Add and test one icon
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(true); ).toBe(true);
expect(storageFunctions.iconExists(testName)).toBe(true); expect(iconExists(testName)).toBe(true);
// Reset config after test // Reset config after test
allowSimpleNames(false); allowSimpleNames(false);
@ -101,7 +105,7 @@ describe('Testing IconifyStorageFunctions', () => {
const prefix2 = `prefixed${n}`; const prefix2 = `prefixed${n}`;
const name2 = `icon${n2}`; const name2 = `icon${n2}`;
expect( expect(
storageFunctions.addCollection({ addCollection({
prefix: '', prefix: '',
icons: { icons: {
[name1]: { [name1]: {
@ -116,8 +120,8 @@ describe('Testing IconifyStorageFunctions', () => {
// Test 'test' // Test 'test'
name = name1; name = name1;
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="basic-icon" />', body: '<g data-icon="basic-icon" />',
}) })
@ -125,9 +129,9 @@ describe('Testing IconifyStorageFunctions', () => {
// Test prefixed icon, using ':' separator // Test prefixed icon, using ':' separator
name = `${prefix2}:${name2}`; name = `${prefix2}:${name2}`;
expect(storageFunctions.listIcons('', prefix2)).toEqual([name]); expect(listIcons('', prefix2)).toEqual([name]);
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="prefixed-icon" />', body: '<g data-icon="prefixed-icon" />',
}) })
@ -135,8 +139,8 @@ describe('Testing IconifyStorageFunctions', () => {
// Test prefixed icon, using '-' separator // Test prefixed icon, using '-' separator
name = `${prefix2}-${name2}`; name = `${prefix2}-${name2}`;
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="prefixed-icon" />', body: '<g data-icon="prefixed-icon" />',
}) })

View File

@ -1,8 +1,8 @@
import { import {
newStorage, newStorage,
addIcon, addIconToStorage,
iconExists, iconExists,
getIcon, getIconFromStorage,
addIconSet, addIconSet,
getStorage, getStorage,
listIcons, listIcons,
@ -13,19 +13,19 @@ describe('Testing storage', () => {
const storage = newStorage('', 'foo'); const storage = newStorage('', 'foo');
// Add one icon // Add one icon
addIcon(storage, 'test', { addIconToStorage(storage, 'test', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,
}); });
addIcon(storage, 'not-really-missing', { addIconToStorage(storage, 'not-really-missing', {
body: '<path d="" />', body: '<path d="" />',
width: 24, width: 24,
height: 24, height: 24,
}); });
// Add another icon with reserved keyword as name // Add another icon with reserved keyword as name
addIcon(storage, 'constructor', { addIconToStorage(storage, 'constructor', {
body: '<g></g>', body: '<g></g>',
width: 24, width: 24,
height: 24, height: 24,
@ -36,7 +36,7 @@ describe('Testing storage', () => {
storage.missing['not-really-missing'] = Date.now(); storage.missing['not-really-missing'] = Date.now();
// Add invalid icon // Add invalid icon
addIcon(storage, 'invalid', {}); addIconToStorage(storage, 'invalid', {});
// Should not include 'invalid' // Should not include 'invalid'
expect(Object.keys(storage.icons)).toEqual([ expect(Object.keys(storage.icons)).toEqual([
@ -63,7 +63,7 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
const icon = getIcon(storage, 'test'); const icon = getIconFromStorage(storage, 'test');
expect(icon).toEqual(expected); expect(icon).toEqual(expected);
// Test icon mutation // Test icon mutation
@ -85,10 +85,10 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 1, rotate: 1,
}; };
expect(getIcon(storage, 'constructor')).toEqual(expected); expect(getIconFromStorage(storage, 'constructor')).toEqual(expected);
expect(getIcon(storage, 'invalid')).toBeNull(); expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIcon(storage, 'missing')).toBeNull(); expect(getIconFromStorage(storage, 'missing')).toBeNull();
}); });
it('Adding simple icon set', () => { it('Adding simple icon set', () => {
@ -131,7 +131,7 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, 'icon1')).toEqual(expected); expect(getIconFromStorage(storage, 'icon1')).toEqual(expected);
expected = { expected = {
body: '<path d="icon2" />', body: '<path d="icon2" />',
width: 24, width: 24,
@ -142,9 +142,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, 'icon2')).toEqual(expected); expect(getIconFromStorage(storage, 'icon2')).toEqual(expected);
expect(getIcon(storage, 'invalid')).toBeNull(); expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIcon(storage, 'missing')).toBeNull(); expect(getIconFromStorage(storage, 'missing')).toBeNull();
}); });
it('Icon set with aliases that use transformations', () => { it('Icon set with aliases that use transformations', () => {
@ -187,7 +187,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, '16-chevron-left')).toEqual(expected); expect(getIconFromStorage(storage, '16-chevron-left')).toEqual(
expected
);
// Test alias // Test alias
expected = { expected = {
@ -200,7 +202,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, '16-chevron-right')).toEqual(expected); expect(getIconFromStorage(storage, '16-chevron-right')).toEqual(
expected
);
}); });
it('List icons in a global storage', () => { it('List icons in a global storage', () => {
@ -214,7 +218,7 @@ describe('Testing storage', () => {
expect(listIcons(provider, prefix)).toEqual([]); expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon without provider // Add one icon without provider
addIcon(storage1, 'test', { addIconToStorage(storage1, 'test', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,
@ -253,7 +257,7 @@ describe('Testing storage', () => {
expect(listIcons(provider, prefix)).toEqual([]); expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon with provider // Add one icon with provider
addIcon(storage2, 'test2', { addIconToStorage(storage2, 'test2', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,

View File

@ -107,7 +107,7 @@ configStorage[''] = createAPIConfig({
/** /**
* Add custom config for provider * Add custom config for provider
*/ */
export function setAPIConfig( export function addAPIProvider(
provider: string, provider: string,
customConfig: PartialIconifyAPIConfig customConfig: PartialIconifyAPIConfig
): boolean { ): boolean {

View File

@ -3,25 +3,17 @@ import type {
QueryDoneCallback, QueryDoneCallback,
} from '@iconify/api-redundancy'; } from '@iconify/api-redundancy';
import type { IconifyIconName } from '@iconify/utils/lib/icon/name'; import type { IconifyIconName } from '@iconify/utils/lib/icon/name';
import { sendAPIQuery } from './query';
import { loadIcons } from './icons';
import type { import type {
IconifyIconLoaderAbort, IconifyIconLoaderAbort,
IconifyIconLoaderCallback, IconifyIconLoaderCallback,
} from './icons'; } from './icons';
import type { import type { GetAPIConfig, PartialIconifyAPIConfig } from './config';
GetAPIConfig,
PartialIconifyAPIConfig,
} from './config';
import { getAPIConfig, setAPIConfig, listAPIProviders } from './config';
import type { import type {
IconifyAPIModule, IconifyAPIModule,
IconifyAPIQueryParams, IconifyAPIQueryParams,
IconifyAPICustomQueryParams, IconifyAPICustomQueryParams,
} from './modules'; } from './modules';
import { setAPIModule } from './modules';
import type { MergeParams, IconifyAPIMergeQueryParams } from './params'; import type { MergeParams, IconifyAPIMergeQueryParams } from './params';
import { mergeParams } from './params';
/** /**
* Iconify API functions * Iconify API functions
@ -44,11 +36,6 @@ export interface IconifyAPIFunctions {
) => boolean; ) => boolean;
} }
export const APIFunctions: IconifyAPIFunctions = {
loadIcons,
addAPIProvider: setAPIConfig,
};
/** /**
* Exposed internal functions * Exposed internal functions
* *
@ -93,14 +80,6 @@ export interface IconifyAPIInternalFunctions {
mergeParams: MergeParams; mergeParams: MergeParams;
} }
export const APIInternalFunctions: IconifyAPIInternalFunctions = {
getAPIConfig,
setAPIModule,
sendAPIQuery,
listAPIProviders,
mergeParams,
};
/** /**
* Types needed for internal functions * Types needed for internal functions
*/ */

View File

@ -1,7 +1,5 @@
import type { IconifyIcon } from '@iconify/types'; import type { IconifyIcon } from '@iconify/types';
import { fullIcon } from '@iconify/utils/lib/icon'; import { fullIcon } from '@iconify/utils/lib/icon';
import { replaceIDs } from '@iconify/utils/lib/svg/id';
import { calculateSize } from '@iconify/utils/lib/svg/size';
import { import {
defaults, defaults,
mergeCustomisations, mergeCustomisations,
@ -27,15 +25,16 @@ export interface IconifyBuilderFunctions {
} }
/** /**
* Exported builder functions * Build icon
*/ */
export const builderFunctions: IconifyBuilderFunctions = { export function buildIcon(
replaceIDs, icon: IconifyIcon,
calculateSize, customisations: IconifyIconCustomisations
buildIcon: (icon, customisations) => { ): IconifyIconBuildResult {
return iconToSVG( return iconToSVG(
fullIcon(icon), fullIcon(icon),
mergeCustomisations(defaults, customisations) customisations
? mergeCustomisations(defaults, customisations)
: defaults
); );
}, }
};

View File

@ -5,9 +5,8 @@ import type { IconifyIconName } from '@iconify/utils/lib/icon/name';
import { stringToIcon, validateIcon } from '@iconify/utils/lib/icon/name'; import { stringToIcon, validateIcon } from '@iconify/utils/lib/icon/name';
import { import {
getStorage, getStorage,
getIcon, getIconFromStorage,
listIcons, addIconToStorage,
addIcon as storeIcon,
addIconSet, addIconSet,
} from './storage'; } from './storage';
// import { parseIconSet } from '../icon'; // import { parseIconSet } from '../icon';
@ -64,7 +63,7 @@ export function getIconData(
const icon = const icon =
typeof name === 'string' ? stringToIcon(name, true, simpleNames) : name; typeof name === 'string' ? stringToIcon(name, true, simpleNames) : name;
return icon return icon
? getIcon(getStorage(icon.provider, icon.prefix), icon.name) ? getIconFromStorage(getStorage(icon.provider, icon.prefix), icon.name)
: null; : null;
} }
@ -77,7 +76,7 @@ export function addIcon(name: string, data: IconifyIcon): boolean {
return false; return false;
} }
const storage = getStorage(icon.provider, icon.prefix); const storage = getStorage(icon.provider, icon.prefix);
return storeIcon(storage, icon.name, data); return addIconToStorage(storage, icon.name, data);
} }
/** /**
@ -126,24 +125,16 @@ export function addCollection(data: IconifyJSON, provider?: string): boolean {
} }
/** /**
* Export * Check if icon exists
*/ */
export const storageFunctions: IconifyStorageFunctions = { export function iconExists(name: string): boolean {
// Check if icon exists return getIconData(name) !== null;
iconExists: (name) => getIconData(name) !== null, }
// Get raw icon data /**
getIcon: (name) => { * Get icon
*/
export function getIcon(name: string): Required<IconifyIcon> | null {
const result = getIconData(name); const result = getIconData(name);
return result ? { ...result } : null; return result ? { ...result } : null;
}, }
// List icons
listIcons,
// Add icon
addIcon,
// Add icon set
addCollection,
};

View File

@ -22,8 +22,9 @@ export interface IconStorage {
/** /**
* Storage by provider and prefix * Storage by provider and prefix
*/ */
const storage: Record<string, Record<string, IconStorage>> = const storage: Record<string, Record<string, IconStorage>> = Object.create(
Object.create(null); null
);
/** /**
* Create new storage * Create new storage
@ -78,7 +79,7 @@ export function addIconSet(
/** /**
* Add icon to storage * Add icon to storage
*/ */
export function addIcon( export function addIconToStorage(
storage: IconStorage, storage: IconStorage,
name: string, name: string,
icon: IconifyIcon icon: IconifyIcon
@ -105,7 +106,7 @@ export function iconExists(storage: IconStorage, name: string): boolean {
/** /**
* Get icon data * Get icon data
*/ */
export function getIcon( export function getIconFromStorage(
storage: IconStorage, storage: IconStorage,
name: string name: string
): Readonly<FullIconifyIcon> | null { ): Readonly<FullIconifyIcon> | null {

View File

@ -2,7 +2,7 @@ import crossFetch from 'cross-fetch';
import { sendAPIQuery } from '../../lib/api/query'; import { sendAPIQuery } from '../../lib/api/query';
import { setAPIModule } from '../../lib/api/modules'; import { setAPIModule } from '../../lib/api/modules';
import { fetchAPIModule, setFetch } from '../../lib/api/modules/fetch'; import { fetchAPIModule, setFetch } from '../../lib/api/modules/fetch';
import { setAPIConfig } from '../../lib/api/config'; import { addAPIProvider } from '../../lib/api/config';
import { mockAPIModule } from '../../lib/api/modules/mock'; import { mockAPIModule } from '../../lib/api/modules/mock';
describe('Testing live API with fetch', () => { describe('Testing live API with fetch', () => {
@ -43,7 +43,7 @@ describe('Testing live API with fetch', () => {
it('Custom request with provider', (done) => { it('Custom request with provider', (done) => {
const provider = nextProvider(); const provider = nextProvider();
expect( expect(
setAPIConfig(provider, { addAPIProvider(provider, {
resources: [host], resources: [host],
}) })
).toBe(true); ).toBe(true);

View File

@ -1,5 +1,5 @@
import type { PendingQueryItem } from '@iconify/api-redundancy'; import type { PendingQueryItem } from '@iconify/api-redundancy';
import { setAPIConfig } from '../../lib/api/config'; import { addAPIProvider } from '../../lib/api/config';
import type { import type {
IconifyAPIIconsQueryParams, IconifyAPIIconsQueryParams,
IconifyAPIQueryParams, IconifyAPIQueryParams,
@ -22,7 +22,7 @@ describe('Testing API loadIcons', () => {
let asyncCounter = 0; let asyncCounter = 0;
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
}); });
@ -159,7 +159,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
}); });
@ -267,7 +267,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });
@ -371,7 +371,7 @@ describe('Testing API loadIcons', () => {
const prefix = nextPrefix(); const prefix = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });
@ -534,7 +534,7 @@ describe('Testing API loadIcons', () => {
const prefix2 = nextPrefix(); const prefix2 = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'], resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test rotate: 100, // 100ms to speed up test
}); });

View File

@ -1,4 +1,4 @@
import { setAPIConfig } from '../../lib/api/config'; import { addAPIProvider } from '../../lib/api/config';
import { setAPIModule } from '../../lib/api/modules'; import { setAPIModule } from '../../lib/api/modules';
import { loadIcons } from '../../lib/api/icons'; import { loadIcons } from '../../lib/api/icons';
import type { IconifyMockAPIDelayDoneCallback } from '../../lib/api/modules/mock'; import type { IconifyMockAPIDelayDoneCallback } from '../../lib/api/modules/mock';
@ -17,7 +17,7 @@ describe('Testing mock API module', () => {
const provider = nextPrefix(); const provider = nextPrefix();
beforeEach(() => { beforeEach(() => {
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://api1.local'], resources: ['https://api1.local'],
}); });
setAPIModule(provider, mockAPIModule); setAPIModule(provider, mockAPIModule);

View File

@ -1,5 +1,5 @@
import type { IconifyAPIConfig } from '../../lib/api/config'; import type { IconifyAPIConfig } from '../../lib/api/config';
import { setAPIConfig, getAPIConfig } from '../../lib/api/config'; import { addAPIProvider, getAPIConfig } from '../../lib/api/config';
import type { import type {
IconifyAPIIconsQueryParams, IconifyAPIIconsQueryParams,
IconifyAPIModule, IconifyAPIModule,
@ -37,7 +37,7 @@ describe('Testing API modules', () => {
const provider = nextPrefix(); const provider = nextPrefix();
// Set config // Set config
setAPIConfig(provider, { addAPIProvider(provider, {
resources: ['https://localhost:3000'], resources: ['https://localhost:3000'],
maxURL: 500, maxURL: 500,
}); });

View File

@ -1,6 +1,10 @@
import { fullIcon } from '@iconify/utils/lib/icon'; import { fullIcon } from '@iconify/utils/lib/icon';
import { listIcons } from '../../lib/storage/storage';
import { import {
storageFunctions, iconExists,
getIcon,
addIcon,
addCollection,
allowSimpleNames, allowSimpleNames,
} from '../../lib/storage/functions'; } from '../../lib/storage/functions';
@ -16,18 +20,18 @@ describe('Testing IconifyStorageFunctions', () => {
const testName = `@${provider}:foo:bar`; const testName = `@${provider}:foo:bar`;
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
expect(storageFunctions.listIcons(provider)).toEqual([]); expect(listIcons(provider)).toEqual([]);
// Add and test one icon // Add and test one icon
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(true); ).toBe(true);
expect(storageFunctions.iconExists(testName)).toBe(true); expect(iconExists(testName)).toBe(true);
expect(storageFunctions.listIcons(provider)).toEqual([testName]); expect(listIcons(provider)).toEqual([testName]);
}); });
it('Invalid icon name', () => { it('Invalid icon name', () => {
@ -37,16 +41,16 @@ describe('Testing IconifyStorageFunctions', () => {
allowSimpleNames(false); allowSimpleNames(false);
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
// Add and test one icon (icon should not be added) // Add and test one icon (icon should not be added)
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(false); ).toBe(false);
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
}); });
it('Invalid icon set', () => { it('Invalid icon set', () => {
@ -55,7 +59,7 @@ describe('Testing IconifyStorageFunctions', () => {
// Icon set without prefix (should work only when simple names are allowed, tested later in this file) // Icon set without prefix (should work only when simple names are allowed, tested later in this file)
expect( expect(
storageFunctions.addCollection({ addCollection({
prefix: '', prefix: '',
icons: { icons: {
foo: { foo: {
@ -73,16 +77,16 @@ describe('Testing IconifyStorageFunctions', () => {
allowSimpleNames(true); allowSimpleNames(true);
// Empty // Empty
expect(storageFunctions.iconExists(testName)).toBe(false); expect(iconExists(testName)).toBe(false);
expect(storageFunctions.getIcon(testName)).toBeNull(); expect(getIcon(testName)).toBeNull();
// Add and test one icon // Add and test one icon
expect( expect(
storageFunctions.addIcon(testName, { addIcon(testName, {
body: '<g />', body: '<g />',
}) })
).toBe(true); ).toBe(true);
expect(storageFunctions.iconExists(testName)).toBe(true); expect(iconExists(testName)).toBe(true);
// Reset config after test // Reset config after test
allowSimpleNames(false); allowSimpleNames(false);
@ -101,7 +105,7 @@ describe('Testing IconifyStorageFunctions', () => {
const prefix2 = `prefixed${n}`; const prefix2 = `prefixed${n}`;
const name2 = `icon${n2}`; const name2 = `icon${n2}`;
expect( expect(
storageFunctions.addCollection({ addCollection({
prefix: '', prefix: '',
icons: { icons: {
[name1]: { [name1]: {
@ -116,8 +120,8 @@ describe('Testing IconifyStorageFunctions', () => {
// Test 'test' // Test 'test'
name = name1; name = name1;
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="basic-icon" />', body: '<g data-icon="basic-icon" />',
}) })
@ -125,9 +129,9 @@ describe('Testing IconifyStorageFunctions', () => {
// Test prefixed icon, using ':' separator // Test prefixed icon, using ':' separator
name = `${prefix2}:${name2}`; name = `${prefix2}:${name2}`;
expect(storageFunctions.listIcons('', prefix2)).toEqual([name]); expect(listIcons('', prefix2)).toEqual([name]);
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="prefixed-icon" />', body: '<g data-icon="prefixed-icon" />',
}) })
@ -135,8 +139,8 @@ describe('Testing IconifyStorageFunctions', () => {
// Test prefixed icon, using '-' separator // Test prefixed icon, using '-' separator
name = `${prefix2}-${name2}`; name = `${prefix2}-${name2}`;
expect(storageFunctions.iconExists(name)).toBe(true); expect(iconExists(name)).toBe(true);
expect(storageFunctions.getIcon(name)).toEqual( expect(getIcon(name)).toEqual(
fullIcon({ fullIcon({
body: '<g data-icon="prefixed-icon" />', body: '<g data-icon="prefixed-icon" />',
}) })

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/ban-ts-comment */
import { import {
newStorage, newStorage,
addIcon, addIconToStorage,
iconExists, iconExists,
getIcon, getIconFromStorage,
addIconSet, addIconSet,
getStorage, getStorage,
listIcons, listIcons,
@ -15,19 +15,19 @@ describe('Testing storage', () => {
const storage = newStorage('', 'foo'); const storage = newStorage('', 'foo');
// Add one icon // Add one icon
addIcon(storage, 'test', { addIconToStorage(storage, 'test', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,
}); });
addIcon(storage, 'not-really-missing', { addIconToStorage(storage, 'not-really-missing', {
body: '<path d="" />', body: '<path d="" />',
width: 24, width: 24,
height: 24, height: 24,
}); });
// Add another icon with reserved keyword as name // Add another icon with reserved keyword as name
addIcon(storage, 'constructor', { addIconToStorage(storage, 'constructor', {
body: '<g></g>', body: '<g></g>',
width: 24, width: 24,
height: 24, height: 24,
@ -38,7 +38,7 @@ describe('Testing storage', () => {
storage.missing['not-really-missing'] = Date.now(); storage.missing['not-really-missing'] = Date.now();
// Add invalid icon // Add invalid icon
addIcon(storage, 'invalid', {} as unknown as IconifyIcon); addIconToStorage(storage, 'invalid', {} as unknown as IconifyIcon);
// Should not include 'invalid' // Should not include 'invalid'
expect(Object.keys(storage.icons)).toEqual([ expect(Object.keys(storage.icons)).toEqual([
@ -65,7 +65,7 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
const icon = getIcon(storage, 'test'); const icon = getIconFromStorage(storage, 'test');
expect(icon).toEqual(expected); expect(icon).toEqual(expected);
// Test icon mutation // Test icon mutation
@ -88,10 +88,10 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 1, rotate: 1,
}; };
expect(getIcon(storage, 'constructor')).toEqual(expected); expect(getIconFromStorage(storage, 'constructor')).toEqual(expected);
expect(getIcon(storage, 'invalid')).toBeNull(); expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIcon(storage, 'missing')).toBeNull(); expect(getIconFromStorage(storage, 'missing')).toBeNull();
}); });
it('Adding simple icon set', () => { it('Adding simple icon set', () => {
@ -134,7 +134,7 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, 'icon1')).toEqual(expected); expect(getIconFromStorage(storage, 'icon1')).toEqual(expected);
expected = { expected = {
body: '<path d="icon2" />', body: '<path d="icon2" />',
width: 24, width: 24,
@ -145,9 +145,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, 'icon2')).toEqual(expected); expect(getIconFromStorage(storage, 'icon2')).toEqual(expected);
expect(getIcon(storage, 'invalid')).toBeNull(); expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIcon(storage, 'missing')).toBeNull(); expect(getIconFromStorage(storage, 'missing')).toBeNull();
}); });
it('Icon set with aliases that use transformations', () => { it('Icon set with aliases that use transformations', () => {
@ -190,7 +190,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, '16-chevron-left')).toEqual(expected); expect(getIconFromStorage(storage, '16-chevron-left')).toEqual(
expected
);
// Test alias // Test alias
expected = { expected = {
@ -203,7 +205,9 @@ describe('Testing storage', () => {
vFlip: false, vFlip: false,
rotate: 0, rotate: 0,
}; };
expect(getIcon(storage, '16-chevron-right')).toEqual(expected); expect(getIconFromStorage(storage, '16-chevron-right')).toEqual(
expected
);
}); });
it('List icons in a global storage', () => { it('List icons in a global storage', () => {
@ -217,7 +221,7 @@ describe('Testing storage', () => {
expect(listIcons(provider, prefix)).toEqual([]); expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon without provider // Add one icon without provider
addIcon(storage1, 'test', { addIconToStorage(storage1, 'test', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,
@ -256,7 +260,7 @@ describe('Testing storage', () => {
expect(listIcons(provider, prefix)).toEqual([]); expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon with provider // Add one icon with provider
addIcon(storage2, 'test2', { addIconToStorage(storage2, 'test2', {
body: '<path d="" />', body: '<path d="" />',
width: 20, width: 20,
height: 16, height: 16,