From 5f285fcf1401740531ea381f2b1712166f19793d Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 21 Dec 2020 17:15:45 +0200 Subject: [PATCH] Split common storage functions into separate file with reusable interface --- .../tests/20-renderer-v1-test.ts | 6 +- .../tests/20-renderer-v2-test.ts | 6 +- .../browser-tests/tests/20-scan-dom-test.ts | 2 +- .../tests/21-observe-dom-test.ts | 2 +- packages/core/src/api/callbacks.ts | 2 +- packages/core/src/api/index.ts | 2 +- packages/core/src/icon/sort.ts | 2 +- .../{cache/storage.ts => storage/browser.ts} | 2 +- packages/core/src/storage/functions.ts | 115 +++++++++++++++++ .../core/src/storage/{index.ts => storage.ts} | 0 packages/core/tests/10-basic/storage-test.ts | 2 +- .../core/tests/30-api/10-callbacks-test.ts | 2 +- packages/core/tests/30-api/10-modules-test.ts | 4 +- packages/core/tests/30-api/20-loading-test.ts | 26 ++-- packages/core/tests/30-cache/10-basic-test.ts | 4 +- .../core/tests/30-cache/20-loading-test.ts | 8 +- .../core/tests/30-cache/30-saving-test.ts | 6 +- packages/core/tests/30-cache/fake_cache.ts | 2 +- .../core/tests/40-modules/10-storage-test.ts | 51 ++++++++ packages/iconify/src/common.ts | 120 +----------------- packages/iconify/src/iconify.ts | 29 +++-- packages/iconify/src/iconify.without-api.ts | 23 +++- packages/iconify/src/modules/scanner.ts | 2 +- packages/react-with-api/src/icon.ts | 106 +++++----------- 24 files changed, 280 insertions(+), 244 deletions(-) rename packages/core/src/{cache/storage.ts => storage/browser.ts} (99%) create mode 100644 packages/core/src/storage/functions.ts rename packages/core/src/storage/{index.ts => storage.ts} (100%) create mode 100644 packages/core/tests/40-modules/10-storage-test.ts diff --git a/packages/browser-tests/tests/20-renderer-v1-test.ts b/packages/browser-tests/tests/20-renderer-v1-test.ts index cd48b22..57bb52b 100644 --- a/packages/browser-tests/tests/20-renderer-v1-test.ts +++ b/packages/browser-tests/tests/20-renderer-v1-test.ts @@ -8,7 +8,11 @@ import { } from '@iconify/iconify/lib/modules/finder'; import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify-v1'; import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-v1-icon'; -import { getStorage, addIconSet, getIcon } from '@iconify/core/lib/storage'; +import { + getStorage, + addIconSet, + getIcon, +} from '@iconify/core/lib/storage/storage'; import { renderIcon } from '@iconify/iconify/lib/modules/render'; import { stringToIcon } from '@iconify/core/lib/icon/name'; import { IconifyElement } from '@iconify/iconify/lib/modules/element'; diff --git a/packages/browser-tests/tests/20-renderer-v2-test.ts b/packages/browser-tests/tests/20-renderer-v2-test.ts index 595f9d8..d37bbb1 100644 --- a/packages/browser-tests/tests/20-renderer-v2-test.ts +++ b/packages/browser-tests/tests/20-renderer-v2-test.ts @@ -8,7 +8,11 @@ import { } from '@iconify/iconify/lib/modules/finder'; import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify'; import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon'; -import { getStorage, addIconSet, getIcon } from '@iconify/core/lib/storage'; +import { + getStorage, + addIconSet, + getIcon, +} from '@iconify/core/lib/storage/storage'; import { renderIcon } from '@iconify/iconify/lib/modules/render'; import { stringToIcon } from '@iconify/core/lib/icon/name'; import { IconifyElement } from '@iconify/iconify/lib/modules/element'; diff --git a/packages/browser-tests/tests/20-scan-dom-test.ts b/packages/browser-tests/tests/20-scan-dom-test.ts index 364656f..520f5c7 100644 --- a/packages/browser-tests/tests/20-scan-dom-test.ts +++ b/packages/browser-tests/tests/20-scan-dom-test.ts @@ -5,7 +5,7 @@ import { getNode, setRoot } from './node'; import { addFinder } from '@iconify/iconify/lib/modules/finder'; import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify'; import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon'; -import { getStorage, addIconSet } from '@iconify/core/lib/storage'; +import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage'; import { listRootNodes } from '@iconify/iconify/lib/modules/root'; import { scanDOM, scanElement } from '@iconify/iconify/lib/modules/scanner'; import { removeObservedNode } from '@iconify/iconify/lib/modules/observer'; diff --git a/packages/browser-tests/tests/21-observe-dom-test.ts b/packages/browser-tests/tests/21-observe-dom-test.ts index 87544b6..e821988 100644 --- a/packages/browser-tests/tests/21-observe-dom-test.ts +++ b/packages/browser-tests/tests/21-observe-dom-test.ts @@ -5,7 +5,7 @@ import { getNode, setRoot } from './node'; import { addFinder } from '@iconify/iconify/lib/modules/finder'; import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify'; import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon'; -import { getStorage, addIconSet } from '@iconify/core/lib/storage'; +import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage'; import { listRootNodes } from '@iconify/iconify/lib/modules/root'; import { scanDOM } from '@iconify/iconify/lib/modules/scanner'; import { diff --git a/packages/core/src/api/callbacks.ts b/packages/core/src/api/callbacks.ts index 8fc2973..67635b3 100644 --- a/packages/core/src/api/callbacks.ts +++ b/packages/core/src/api/callbacks.ts @@ -2,7 +2,7 @@ import { IconifyIconLoaderCallback, IconifyIconLoaderAbort, } from '../interfaces/loader'; -import { getStorage } from '../storage'; +import { getStorage } from '../storage/storage'; import { SortedIcons } from '../icon/sort'; import { IconifyIconSource } from '../icon/name'; diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index 53d1079..1918af9 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -13,7 +13,7 @@ import { IsPending, IconifyAPI } from '../interfaces/api'; import { storeCallback, updateCallbacks } from './callbacks'; import { getAPIModule } from './modules'; import { getAPIConfig, IconifyAPIConfig } from './config'; -import { getStorage, addIconSet } from '../storage'; +import { getStorage, addIconSet } from '../storage/storage'; import { coreModules } from '../modules'; import { IconifyIconName, IconifyIconSource } from '../icon/name'; import { listToIcons } from '../icon/list'; diff --git a/packages/core/src/icon/sort.ts b/packages/core/src/icon/sort.ts index fc85325..7dfb31d 100644 --- a/packages/core/src/icon/sort.ts +++ b/packages/core/src/icon/sort.ts @@ -1,4 +1,4 @@ -import { getStorage, IconStorage } from '../storage'; +import { getStorage, IconStorage } from '../storage/storage'; import { IconifyIconName } from './name'; /** diff --git a/packages/core/src/cache/storage.ts b/packages/core/src/storage/browser.ts similarity index 99% rename from packages/core/src/cache/storage.ts rename to packages/core/src/storage/browser.ts index 00a28ac..09551bc 100644 --- a/packages/core/src/cache/storage.ts +++ b/packages/core/src/storage/browser.ts @@ -1,6 +1,6 @@ import { IconifyJSON } from '@iconify/types'; import { CacheIcons, LoadIconsCache } from '../interfaces/cache'; -import { getStorage, addIconSet } from '../storage'; +import { getStorage, addIconSet } from './storage'; interface StorageType { local: T; diff --git a/packages/core/src/storage/functions.ts b/packages/core/src/storage/functions.ts new file mode 100644 index 0000000..4749c70 --- /dev/null +++ b/packages/core/src/storage/functions.ts @@ -0,0 +1,115 @@ +import { IconifyJSON } from '@iconify/types'; +import { FullIconifyIcon, IconifyIcon } from '../icon'; +import { IconifyIconName, stringToIcon, validateIcon } from '../icon/name'; +import { merge } from '../misc/merge'; +import { getStorage, getIcon, listIcons, addIcon, addIconSet } from './storage'; + +/** + * Interface for exported storage functions + */ +export interface IconifyStorageFunctions { + /** + * Check if icon exists + */ + iconExists: (name: string) => boolean; + + /** + * Get icon data with all properties + */ + getIcon: (name: string) => Required | null; + + /** + * List all available icons + */ + listIcons: (provider?: string, prefix?: string) => string[]; + + /* Add icons */ + /** + * Add icon to storage + */ + addIcon: (name: string, data: IconifyIcon) => boolean; + + /** + * Add icon set to storage + */ + addCollection: (data: IconifyJSON, provider?: string) => boolean; +} + +/** + * Get icon name + */ +export function getIconName(name: string): IconifyIconName | null { + const icon = stringToIcon(name); + if (!validateIcon(icon)) { + return null; + } + return icon; +} + +/** + * Get icon data + */ +export function getIconData( + name: string | IconifyIconName +): FullIconifyIcon | null { + const icon = typeof name === 'string' ? getIconName(name) : name; + return icon + ? getIcon(getStorage(icon.provider, icon.prefix), icon.name) + : null; +} + +/** + * Add icon set + */ +export function addCollection(data: IconifyJSON, provider?: string): boolean { + if (typeof provider !== 'string') { + provider = typeof data.provider === 'string' ? data.provider : ''; + } + + if ( + typeof data !== 'object' || + // Prefix must be present + typeof data.prefix !== 'string' || + // Validate provider and prefix + !validateIcon({ + provider, + prefix: data.prefix, + name: 'a', + }) + ) { + return false; + } + + const storage = getStorage(provider, data.prefix); + return !!addIconSet(storage, data); +} + +/** + * Export + */ +export const storageFunctions: IconifyStorageFunctions = { + // Check if icon exists + iconExists: (name) => getIconData(name) !== null, + + // Get raw icon data + getIcon: (name) => { + const result = getIconData(name); + return result ? merge(result) : null; + }, + + // List icons + listIcons, + + // Add icon + addIcon: (name, data) => { + const icon = getIconName(name); + if (!icon) { + return false; + } + const storage = getStorage(icon.provider, icon.prefix); + return addIcon(storage, icon.name, data); + }, + + // Add icon set + addCollection, +}; diff --git a/packages/core/src/storage/index.ts b/packages/core/src/storage/storage.ts similarity index 100% rename from packages/core/src/storage/index.ts rename to packages/core/src/storage/storage.ts diff --git a/packages/core/tests/10-basic/storage-test.ts b/packages/core/tests/10-basic/storage-test.ts index bce2718..e02a491 100644 --- a/packages/core/tests/10-basic/storage-test.ts +++ b/packages/core/tests/10-basic/storage-test.ts @@ -9,7 +9,7 @@ import { addIconSet, getStorage, listIcons, -} from '../../lib/storage'; +} from '../../lib/storage/storage'; import { FullIconifyIcon, IconifyIcon } from '../../lib/icon'; describe('Testing storage', () => { diff --git a/packages/core/tests/30-api/10-callbacks-test.ts b/packages/core/tests/30-api/10-callbacks-test.ts index 3c1cb64..dc1e794 100644 --- a/packages/core/tests/30-api/10-callbacks-test.ts +++ b/packages/core/tests/30-api/10-callbacks-test.ts @@ -8,7 +8,7 @@ import { storeCallback, } from '../../lib/api/callbacks'; import { sortIcons } from '../../lib/icon/sort'; -import { getStorage, addIconSet } from '../../lib/storage'; +import { getStorage, addIconSet } from '../../lib/storage/storage'; describe('Testing API callbacks', () => { let prefixCounter = 0; diff --git a/packages/core/tests/30-api/10-modules-test.ts b/packages/core/tests/30-api/10-modules-test.ts index 876986c..2592484 100644 --- a/packages/core/tests/30-api/10-modules-test.ts +++ b/packages/core/tests/30-api/10-modules-test.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import 'mocha'; import { expect } from 'chai'; -import { RedundancyPendingItem } from '@cyberalien/redundancy'; +import { PendingQueryItem } from '@cyberalien/redundancy'; import { setAPIConfig, getAPIConfig, @@ -40,7 +40,7 @@ describe('Testing API modules', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { throw new Error('Unexpected API call'); }; diff --git a/packages/core/tests/30-api/20-loading-test.ts b/packages/core/tests/30-api/20-loading-test.ts index f920918..e350396 100644 --- a/packages/core/tests/30-api/20-loading-test.ts +++ b/packages/core/tests/30-api/20-loading-test.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import 'mocha'; import { expect } from 'chai'; -import { RedundancyPendingItem } from '@cyberalien/redundancy'; +import { PendingQueryItem } from '@cyberalien/redundancy'; import { setAPIConfig } from '../../lib/api/config'; import { setAPIModule, APIQueryParams } from '../../lib/api/modules'; import { API } from '../../lib/api/'; @@ -56,7 +56,7 @@ describe('Testing API loadIcons', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { // This callback should be called after prepareQuery expect(asyncCounter).to.be.equal(2); @@ -72,7 +72,7 @@ describe('Testing API loadIcons', () => { expect(params).to.be.eql(expected); // Send data - status.done({ + item.done({ prefix, icons: { icon1: { @@ -188,7 +188,7 @@ describe('Testing API loadIcons', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { // Test input expect(host).to.be.equal('https://api1.local'); @@ -209,7 +209,7 @@ describe('Testing API loadIcons', () => { body: '', }; }); - status.done({ + item.done({ prefix, icons, }); @@ -280,7 +280,7 @@ describe('Testing API loadIcons', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { queryCounter++; switch (queryCounter) { @@ -296,7 +296,7 @@ describe('Testing API loadIcons', () => { expect(host).to.be.equal('https://api2.local'); // Return result - status.done({ + item.done({ prefix, icons: { icon1: { @@ -382,7 +382,7 @@ describe('Testing API loadIcons', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { queryCounter++; switch (queryCounter) { @@ -400,7 +400,7 @@ describe('Testing API loadIcons', () => { expect(host).to.be.equal('https://api2.local'); // Return result - status.done({ + item.done({ prefix, icons: { icon1: { @@ -419,7 +419,7 @@ describe('Testing API loadIcons', () => { expect(host).to.be.equal('https://api2.local'); // Return result - status.done({ + item.done({ prefix, icons: { icon3: { @@ -538,7 +538,7 @@ describe('Testing API loadIcons', () => { const sendQuery = ( host: string, params: APIQueryParams, - status: RedundancyPendingItem + item: PendingQueryItem ): void => { queryCounter++; switch (queryCounter) { @@ -558,7 +558,7 @@ describe('Testing API loadIcons', () => { expect(host).to.be.equal('https://api2.local'); // Return result - status.done({ + item.done({ prefix: params.prefix, icons: { icon1: { @@ -578,7 +578,7 @@ describe('Testing API loadIcons', () => { expect(host).to.be.equal('https://api2.local'); // Return result - status.done({ + item.done({ prefix: params.prefix, icons: { icon2: { diff --git a/packages/core/tests/30-cache/10-basic-test.ts b/packages/core/tests/30-cache/10-basic-test.ts index 75fbaf8..7ef5a2e 100644 --- a/packages/core/tests/30-cache/10-basic-test.ts +++ b/packages/core/tests/30-cache/10-basic-test.ts @@ -1,8 +1,6 @@ -/* eslint-disable @typescript-eslint/no-unused-vars-experimental */ -/* eslint-disable @typescript-eslint/no-unused-vars */ import 'mocha'; import { expect } from 'chai'; -import { count, config, loadCache } from '../../lib/cache/storage'; +import { count, config, loadCache } from '../../lib/storage/browser'; import { nextPrefix, createCache, diff --git a/packages/core/tests/30-cache/20-loading-test.ts b/packages/core/tests/30-cache/20-loading-test.ts index 2cfaba0..74bfa63 100644 --- a/packages/core/tests/30-cache/20-loading-test.ts +++ b/packages/core/tests/30-cache/20-loading-test.ts @@ -1,5 +1,3 @@ -/* eslint-disable @typescript-eslint/no-unused-vars-experimental */ -/* eslint-disable @typescript-eslint/no-unused-vars */ import 'mocha'; import { expect } from 'chai'; import { @@ -8,8 +6,8 @@ import { config, emptyList, StoredItem, -} from '../../lib/cache/storage'; -import { getStorage, iconExists, getIcon } from '../../lib/storage'; +} from '../../lib/storage/browser'; +import { getStorage, iconExists } from '../../lib/storage/storage'; import { nextPrefix, createCache, @@ -21,7 +19,7 @@ import { hour, cacheExpiration, } from './fake_cache'; -import { IconifyIcon, IconifyJSON } from '@iconify/types'; +import { IconifyJSON } from '@iconify/types'; describe('Testing loading from localStorage', () => { const provider = ''; diff --git a/packages/core/tests/30-cache/30-saving-test.ts b/packages/core/tests/30-cache/30-saving-test.ts index 12a5ccb..fa0ef9a 100644 --- a/packages/core/tests/30-cache/30-saving-test.ts +++ b/packages/core/tests/30-cache/30-saving-test.ts @@ -1,5 +1,3 @@ -/* eslint-disable @typescript-eslint/no-unused-vars-experimental */ -/* eslint-disable @typescript-eslint/no-unused-vars */ import 'mocha'; import { expect } from 'chai'; import { @@ -9,8 +7,8 @@ import { config, emptyList, StoredItem, -} from '../../lib/cache/storage'; -import { getStorage, iconExists } from '../../lib/storage'; +} from '../../lib/storage/browser'; +import { getStorage, iconExists } from '../../lib/storage/storage'; import { nextPrefix, createCache, diff --git a/packages/core/tests/30-cache/fake_cache.ts b/packages/core/tests/30-cache/fake_cache.ts index 796022c..ac486b2 100644 --- a/packages/core/tests/30-cache/fake_cache.ts +++ b/packages/core/tests/30-cache/fake_cache.ts @@ -1,4 +1,4 @@ -import { mock, count, config, emptyList } from '../../lib/cache/storage'; +import { mock, count, config, emptyList } from '../../lib/storage/browser'; /** * Get next icon set prefix for testing diff --git a/packages/core/tests/40-modules/10-storage-test.ts b/packages/core/tests/40-modules/10-storage-test.ts new file mode 100644 index 0000000..11ebdb8 --- /dev/null +++ b/packages/core/tests/40-modules/10-storage-test.ts @@ -0,0 +1,51 @@ +import 'mocha'; +import { expect } from 'chai'; +import { getIconName, storageFunctions } from '../../lib/storage/functions'; +import { IconifyIconName } from '../../lib/icon/name'; + +describe('Testing IconifyStorageFunctions', () => { + let count = 0; + + function nextProvider(): string { + return 'storage-test-' + count++; + } + + it('Getting icon name', () => { + let expected: IconifyIconName; + + expected = { + provider: '', + prefix: 'mdi', + name: 'home', + }; + expect(getIconName('mdi:home')).to.be.eql(expected); + + expected = { + provider: 'local-test', + prefix: 'mdi', + name: 'home', + }; + expect(getIconName('@local-test:mdi:home')).to.be.eql(expected); + + expect(getIconName('test')).to.be.equal(null); + }); + + it('Storage functions', () => { + const provider = nextProvider(); + const testName = `@${provider}:foo:bar`; + + // Empty + expect(storageFunctions.iconExists(testName)).to.be.equal(false); + expect(storageFunctions.getIcon(testName)).to.be.equal(null); + expect(storageFunctions.listIcons(provider)).to.be.eql([]); + + // Add and test one icon + expect( + storageFunctions.addIcon(testName, { + body: '', + }) + ).to.be.equal(true); + expect(storageFunctions.iconExists(testName)).to.be.equal(true); + expect(storageFunctions.listIcons(provider)).to.be.eql([testName]); + }); +}); diff --git a/packages/iconify/src/common.ts b/packages/iconify/src/common.ts index 27c64e3..d15053d 100644 --- a/packages/iconify/src/common.ts +++ b/packages/iconify/src/common.ts @@ -1,22 +1,13 @@ import { IconifyJSON } from '@iconify/types'; -import { merge } from '@iconify/core/lib/misc/merge'; -import { - stringToIcon, - validateIcon, - IconifyIconName, -} from '@iconify/core/lib/icon/name'; -import { IconifyIcon, FullIconifyIcon } from '@iconify/core/lib/icon'; +import { stringToIcon } from '@iconify/core/lib/icon/name'; import { IconifyIconCustomisations, fullCustomisations, } from '@iconify/core/lib/customisations'; import { - getStorage, - getIcon, - addIcon, - addIconSet, - listIcons, -} from '@iconify/core/lib/storage'; + storageFunctions, + getIconData, +} from '@iconify/core/lib/storage/functions'; import { iconToSVG, IconifyIconBuildResult } from '@iconify/core/lib/builder'; import { replaceIDs } from '@iconify/core/lib/builder/ids'; import { renderIcon } from './modules/render'; @@ -35,27 +26,6 @@ import { finder as iconifyFinder } from './finders/iconify'; import { findRootNode, addBodyNode } from './modules/root'; // import { finder as iconifyIconFinder } from './finders/iconify-icon'; -/** - * Get icon name - */ -function getIconName(name: string): IconifyIconName | null { - const icon = stringToIcon(name); - if (!validateIcon(icon)) { - return null; - } - return icon; -} - -/** - * Get icon data - */ -function getIconData(name: string): FullIconifyIcon | null { - const icon = getIconName(name); - return icon - ? getIcon(getStorage(icon.provider, icon.prefix), icon.name) - : null; -} - /** * Get SVG data */ @@ -107,67 +77,16 @@ function generateIcon( ) as unknown) as SVGElement | string | null; } -/** - * Add icon set - */ -export function addCollection(data: IconifyJSON, provider?: string) { - if (typeof provider !== 'string') { - provider = typeof data.provider === 'string' ? data.provider : ''; - } - - if ( - typeof data !== 'object' || - typeof data.prefix !== 'string' || - !validateIcon({ - provider, - prefix: data.prefix, - name: 'a', - }) - ) { - return false; - } - - const storage = getStorage(provider, data.prefix); - return !!addIconSet(storage, data); -} - /** * Iconify interface */ -export interface IconifyGlobal { +export interface IconifyCommonFunctions { /* General section */ /** * Get version */ getVersion: () => string; - /* Getting icons */ - /** - * Check if icon exists - */ - iconExists: (name: string) => boolean; - - /** - * Get icon data with all properties - */ - getIcon: (name: string) => Required | null; - - /** - * List all available icons - */ - listIcons: (provider?: string, prefix?: string) => string[]; - - /* Add icons */ - /** - * Add icon to storage - */ - addIcon: (name: string, data: IconifyIcon) => boolean; - - /** - * Add icon set to storage - */ - addCollection: (data: IconifyJSON, provider?: string) => boolean; - /* Render icons */ /** * Render icons @@ -226,35 +145,10 @@ export interface IconifyGlobal { /** * Global variable */ -export const IconifyCommon: IconifyGlobal = { +export const commonFunctions: IconifyCommonFunctions = { // Version getVersion: () => '__iconify_version__', - // Check if icon exists - iconExists: (name) => getIconData(name) !== null, - - // Get raw icon data - getIcon: (name) => { - const result = getIconData(name); - return result ? merge(result) : null; - }, - - // List icons - listIcons, - - // Add icon - addIcon: (name, data) => { - const icon = getIconName(name); - if (!icon) { - return false; - } - const storage = getStorage(icon.provider, icon.prefix); - return addIcon(storage, icon.name, data); - }, - - // Add icon set - addCollection, - // Render SVG renderSVG: (name: string, customisations: IconifyIconCustomisations) => { return generateIcon(name, customisations, false) as SVGElement | null; @@ -350,7 +244,7 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') { typeof item.icons !== 'object' || typeof item.prefix !== 'string' || // Add icon set - !addCollection(item) + !storageFunctions.addCollection(item) ) { console.error(err); } diff --git a/packages/iconify/src/iconify.ts b/packages/iconify/src/iconify.ts index 5c1c459..533b32a 100644 --- a/packages/iconify/src/iconify.ts +++ b/packages/iconify/src/iconify.ts @@ -10,12 +10,20 @@ import { } from '@iconify/core/lib/customisations'; import { IconifyIconBuildResult } from '@iconify/core/lib/builder'; import { calcSize } from '@iconify/core/lib/builder/calc-size'; +import { + IconifyStorageFunctions, + storageFunctions, +} from '@iconify/core/lib/storage/functions'; // Modules import { coreModules } from '@iconify/core/lib/modules'; // Cache -import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage'; +import { + storeCache, + loadCache, + config, +} from '@iconify/core/lib/storage/browser'; // API import { @@ -51,7 +59,7 @@ import { // Other import { IconifyExposedCommonInternals } from './internals'; -import { IconifyGlobal as IconifyGlobal1, IconifyCommon } from './common'; +import { IconifyCommonFunctions, commonFunctions } from './common'; /** * Export required types @@ -97,7 +105,7 @@ export interface IconifyExposedInternals /** * Exported functions */ -export interface IconifyGlobal2 extends IconifyAPI { +export interface IconifyFunctions extends IconifyAPI { /** * Expose internal functions */ @@ -107,7 +115,10 @@ export interface IconifyGlobal2 extends IconifyAPI { /** * Iconify interface */ -export interface IconifyGlobal extends IconifyGlobal1, IconifyGlobal2 {} +export interface IconifyGlobal + extends IconifyStorageFunctions, + IconifyCommonFunctions, + IconifyFunctions {} // Export dependencies export { IconifyGlobal as IconifyGlobalCommon, IconifyAPI }; @@ -159,12 +170,14 @@ const Iconify: IconifyGlobal = ({ // Get API module setAPIModule, }, -} as IconifyGlobal2) as IconifyGlobal; +} as IconifyFunctions) as IconifyGlobal; // Merge with common functions -for (const key in IconifyCommon) { - Iconify[key] = IconifyCommon[key]; -} +[storageFunctions, commonFunctions].forEach((list) => { + for (const key in list) { + Iconify[key] = list[key]; + } +}); /** * Initialise stuff diff --git a/packages/iconify/src/iconify.without-api.ts b/packages/iconify/src/iconify.without-api.ts index 02a494f..2a911d5 100644 --- a/packages/iconify/src/iconify.without-api.ts +++ b/packages/iconify/src/iconify.without-api.ts @@ -10,10 +10,14 @@ import { } from '@iconify/core/lib/customisations'; import { IconifyIconBuildResult } from '@iconify/core/lib/builder'; import { calcSize } from '@iconify/core/lib/builder/calc-size'; +import { + IconifyStorageFunctions, + storageFunctions, +} from '@iconify/core/lib/storage/functions'; // Local code import { IconifyExposedCommonInternals } from './internals'; -import { IconifyGlobal as IconifyGlobal1, IconifyCommon } from './common'; +import { IconifyCommonFunctions, commonFunctions } from './common'; /** * Export required types @@ -45,7 +49,7 @@ export interface IconifyExposedInternals /** * Exported functions */ -export interface IconifyGlobal2 { +export interface IconifyFunctions { /** * Expose internal functions */ @@ -55,7 +59,10 @@ export interface IconifyGlobal2 { /** * Iconify interface */ -export interface IconifyGlobal extends IconifyGlobal1, IconifyGlobal2 {} +export interface IconifyGlobal + extends IconifyStorageFunctions, + IconifyCommonFunctions, + IconifyFunctions {} // Export dependencies export { IconifyGlobal as IconifyGlobalCommon }; @@ -69,11 +76,13 @@ const Iconify: IconifyGlobal = ({ // Calculate size calculateSize: calcSize, }, -} as IconifyGlobal2) as IconifyGlobal; +} as IconifyFunctions) as IconifyGlobal; // Merge with common functions -for (const key in IconifyCommon) { - Iconify[key] = IconifyCommon[key]; -} +[storageFunctions, commonFunctions].forEach((list) => { + for (const key in list) { + Iconify[key] = list[key]; + } +}); export default Iconify; diff --git a/packages/iconify/src/modules/scanner.ts b/packages/iconify/src/modules/scanner.ts index 859d804..18fe166 100644 --- a/packages/iconify/src/modules/scanner.ts +++ b/packages/iconify/src/modules/scanner.ts @@ -1,5 +1,5 @@ import { IconifyIconName } from '@iconify/core/lib/icon/name'; -import { getStorage, getIcon } from '@iconify/core/lib/storage'; +import { getStorage, getIcon } from '@iconify/core/lib/storage/storage'; import { coreModules } from '@iconify/core/lib/modules'; import { FullIconifyIcon } from '@iconify/core/lib/icon'; import { findPlaceholders } from './finder'; diff --git a/packages/react-with-api/src/icon.ts b/packages/react-with-api/src/icon.ts index 55b47df..27364ff 100644 --- a/packages/react-with-api/src/icon.ts +++ b/packages/react-with-api/src/icon.ts @@ -9,11 +9,7 @@ import { } from '@iconify/react/lib/icon'; // Core -import { - stringToIcon, - validateIcon, - IconifyIconName, -} from '@iconify/core/lib/icon/name'; +import { IconifyIconName } from '@iconify/core/lib/icon/name'; import { IconifyIconCustomisations, IconifyIconSize, @@ -21,14 +17,12 @@ import { IconifyVerticalIconAlignment, } from '@iconify/core/lib/customisations'; import { - getStorage, - getIcon as _getIcon, - addIcon as addIconToStorage, - addIconSet, - listIcons, -} from '@iconify/core/lib/storage'; + storageFunctions, + getIconData, + getIconName, +} from '@iconify/core/lib/storage/functions'; import { calcSize } from '@iconify/core/lib/builder/calc-size'; -import { IconifyIcon, FullIconifyIcon } from '@iconify/core/lib/icon'; +import { IconifyIcon } from '@iconify/core/lib/icon'; // Modules import { coreModules } from '@iconify/core/lib/modules'; @@ -62,7 +56,11 @@ import { } from '@iconify/core/lib/interfaces/loader'; // Cache -import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage'; +import { + storeCache, + loadCache, + config, +} from '@iconify/core/lib/storage/browser'; /** * Export required types @@ -155,81 +153,35 @@ export function disableCache(storage: IconifyCacheType): void { } /** - * Get icon name + * Check if icon exists */ -function _getIconName(name: string): IconifyIconName | null { - const icon = stringToIcon(name); - if (!validateIcon(icon)) { - return null; - } - return icon; -} +export const iconExists = storageFunctions.iconExists; /** * Get icon data */ -function _getIconData(icon: IconifyIconName): FullIconifyIcon | null { - return _getIcon(getStorage(icon.provider, icon.prefix), icon.name); -} - -export function getIcon(name: string): Required | null { - const icon = _getIconName(name); - return icon ? _getIconData(icon) : null; -} +export const getIcon = storageFunctions.getIcon; /** - * Check if icon exists + * List available icons */ -export function iconExists(name: string): boolean { - return getIcon(name) !== null; -} +export const listIcons = storageFunctions.listIcons; + +/** + * Add one icon + */ +export const addIcon = storageFunctions.addIcon; + +/** + * Add icon set + */ +export const addCollection = storageFunctions.addCollection; /** * Load icons */ export const loadIcons: IconifyLoadIcons = API.loadIcons; -/** - * List available icons - */ -export { listIcons }; - -/** - * Add one icon - */ -export function addIcon(name: string, data: IconifyIcon): boolean { - const icon = _getIconName(name); - if (!icon) { - return false; - } - const storage = getStorage(icon.provider, icon.prefix); - return addIconToStorage(storage, icon.name, data); -} - -/** - * Add icon set - */ -export function addCollection(data: IconifyJSON, provider?: string) { - if (typeof provider !== 'string') { - provider = typeof data.provider === 'string' ? data.provider : ''; - } - - if ( - typeof data !== 'object' || - typeof data.prefix !== 'string' || - !validateIcon({ - provider, - prefix: data.prefix, - name: 'a', - }) - ) { - return false; - } - - const storage = getStorage(provider, data.prefix); - return !!addIconSet(storage, data); -} - /** * Add API provider */ @@ -318,7 +270,7 @@ class DynamicComponent extends Component { */ _loaded(): boolean { if (!this.state.loaded) { - const data = _getIconData(this.props.icon); + const data = getIconData(this.props.icon); if (data) { this._abort = null; this.setState({ @@ -350,12 +302,12 @@ const component = (props: IconProps, func: typeof ReactIcon): JSX.Element => { // Get icon data if (typeof props.icon === 'string') { - const iconName = _getIconName(props.icon); + const iconName = getIconName(props.icon); if (!iconName) { return null; } - iconData = _getIconData(iconName); + iconData = getIconData(iconName); if (iconData) { let staticProps: IconProps = {} as IconProps;