2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-12 01:45:41 +00:00

Move listIcons to core, add unit tests

This commit is contained in:
Vjacheslav Trushkin 2020-08-14 12:01:47 +03:00
parent 84a73d0c22
commit 19312fe1dd
5 changed files with 121 additions and 93 deletions

View File

@ -63,20 +63,6 @@ export function getStorage(provider: string, prefix: string): IconStorage {
return providerStorage[prefix];
}
/**
* Get all providers
*/
export function listStoredProviders(): string[] {
return Object.keys(storage);
}
/**
* Get all prefixes
*/
export function listStoredPrefixes(provider: string): string[] {
return storage[provider] === void 0 ? [] : Object.keys(storage[provider]);
}
/**
* Resolve alias
*/
@ -232,3 +218,49 @@ export function getIcon(
const value = storage.icons[name];
return value === void 0 ? null : value;
}
/**
* List available icons
*/
export function listIcons(provider?: string, prefix?: string): string[] {
let allIcons: string[] = [];
// Get providers
let providers: string[];
if (typeof provider === 'string') {
providers = [provider];
} else {
providers = Object.keys(storage);
}
console.log('Listing icons for providers:', providers);
// Get all icons
providers.forEach((provider) => {
let prefixes: string[];
if (typeof provider === 'string' && typeof prefix === 'string') {
prefixes = [prefix];
} else {
prefixes =
storage[provider] === void 0
? []
: Object.keys(storage[provider]);
}
console.log('Listing icons for prefixes:', prefixes);
prefixes.forEach((prefix) => {
const storage = getStorage(provider, prefix);
const icons = Object.keys(storage.icons).map(
(name) =>
(provider !== '' ? '@' + provider + ':' : '') +
prefix +
':' +
name
);
console.log('Found some icons:', icons);
allIcons = allIcons.concat(icons);
});
});
return allIcons;
}

View File

@ -7,6 +7,8 @@ import {
iconExists,
getIcon,
addIconSet,
getStorage,
listIcons,
} from '../../lib/storage';
import { FullIconifyIcon, IconifyIcon } from '../../lib/icon';
import { IconifyJSON } from '@iconify/types';
@ -456,4 +458,71 @@ describe('Testing storage', () => {
};
expect(getIcon(storage, '16-chevron-right')).to.be.eql(expected);
});
it('List icons in a global storage', () => {
const provider = 'test-provider';
const prefix = 'global-storage-test';
const storage1 = getStorage('', prefix);
const storage2 = getStorage(provider, prefix);
// List icons
expect(listIcons('', prefix)).to.be.eql([]);
expect(listIcons(provider, prefix)).to.be.eql([]);
// Add one icon without provider
addIcon(storage1, 'test', {
body: '<path d="" />',
width: 20,
height: 16,
});
// List icons
expect(listIcons('', prefix)).to.be.eql([prefix + ':test']);
expect(listIcons(provider, prefix)).to.be.eql([]);
// Add icon set without provider
expect(
addIconSet(storage1, {
prefix,
icons: {
'16-chevron-left': {
body: '<path d="" />',
},
},
aliases: {
'16-chevron-right': {
parent: '16-chevron-left',
hFlip: true,
},
},
width: 128,
height: 128,
})
).to.be.equal(true);
// List icons
expect(listIcons('', prefix)).to.be.eql([
prefix + ':test',
prefix + ':16-chevron-left',
prefix + ':16-chevron-right',
]);
expect(listIcons(provider, prefix)).to.be.eql([]);
// Add one icon with provider
addIcon(storage2, 'test2', {
body: '<path d="" />',
width: 20,
height: 16,
});
// List icons
expect(listIcons('', prefix)).to.be.eql([
prefix + ':test',
prefix + ':16-chevron-left',
prefix + ':16-chevron-right',
]);
expect(listIcons(provider, prefix)).to.be.eql([
'@' + provider + ':' + prefix + ':test2',
]);
});
});

View File

@ -3,7 +3,7 @@
import 'mocha';
import { expect } from 'chai';
import { RedundancyPendingItem } from '@cyberalien/redundancy';
import { setAPIConfig, IconifyAPIConfig } from '../../lib/api/config';
import { setAPIConfig } from '../../lib/api/config';
import { setAPIModule, APIQueryParams } from '../../lib/api/modules';
import { API } from '../../lib/api/';

View File

@ -15,8 +15,7 @@ import {
getIcon,
addIcon,
addIconSet,
listStoredProviders,
listStoredPrefixes,
listIcons,
} from '@iconify/core/lib/storage';
import { iconToSVG, IconifyIconBuildResult } from '@iconify/core/lib/builder';
import { replaceIDs } from '@iconify/core/lib/builder/ids';
@ -33,8 +32,7 @@ import { scanDOM, scanElement } from './modules/scanner';
// Finders
import { addFinder } from './modules/finder';
import { finder as iconifyFinder } from './finders/iconify';
import { findRootNode, addRootNode, addBodyNode } from './modules/root';
import { onReady } from './modules/ready';
import { findRootNode, addBodyNode } from './modules/root';
// import { finder as iconifyIconFinder } from './finders/iconify-icon';
/**
@ -242,42 +240,7 @@ export const IconifyCommon: IconifyGlobal = {
},
// List icons
listIcons: (provider?: string, prefix?: string) => {
let allIcons = [];
// Get providers
let providers: string[];
if (typeof provider === 'string') {
providers = [provider];
} else {
providers = listStoredProviders();
}
// Get all icons
providers.forEach((provider) => {
let prefixes: string[];
if (typeof provider === 'string' && typeof prefix === 'string') {
prefixes = [prefix];
} else {
prefixes = listStoredPrefixes(provider);
}
prefixes.forEach((prefix) => {
const storage = getStorage(provider, prefix);
const icons = Object.keys(storage.icons).map(
(name) =>
(provider !== '' ? '@' + provider + ':' : '') +
prefix +
':' +
name
);
allIcons = allIcons.concat(icons);
});
});
return allIcons;
},
listIcons,
// Add icon
addIcon: (name, data) => {

View File

@ -25,8 +25,7 @@ import {
getIcon,
addIcon as addIconToStorage,
addIconSet,
listStoredProviders,
listStoredPrefixes,
listIcons,
} from '@iconify/core/lib/storage';
import { calcSize } from '@iconify/core/lib/builder/calc-size';
import { IconifyIcon, FullIconifyIcon } from '@iconify/core/lib/icon';
@ -193,42 +192,7 @@ export const loadIcons: IconifyLoadIcons = API.loadIcons;
/**
* List available icons
*/
export function listIcons(provider?: string, prefix?: string): string[] {
let allIcons = [];
// Get providers
let providers: string[];
if (typeof provider === 'string') {
providers = [provider];
} else {
providers = listStoredProviders();
}
// Get all icons
providers.forEach((provider) => {
let prefixes: string[];
if (typeof provider === 'string' && typeof prefix === 'string') {
prefixes = [prefix];
} else {
prefixes = listStoredPrefixes(provider);
}
prefixes.forEach((prefix) => {
const storage = getStorage(provider, prefix);
const icons = Object.keys(storage.icons).map(
(name) =>
(provider !== '' ? '@' + provider + ':' : '') +
prefix +
':' +
name
);
allIcons = allIcons.concat(icons);
});
});
return allIcons;
}
export { listIcons };
/**
* Add one icon