2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 13:47:49 +00:00

feat(utils): async version of parseIconSet

This commit is contained in:
Vjacheslav Trushkin 2023-02-15 09:34:29 +02:00
parent 78f71407d5
commit f3b58a75ad
3 changed files with 59 additions and 11 deletions

View File

@ -10,7 +10,12 @@ import { getIconsTree } from './tree';
export type SplitIconSetCallback = ( export type SplitIconSetCallback = (
name: string, name: string,
data: ExtendedIconifyIcon | null data: ExtendedIconifyIcon | null
) => void; ) => unknown;
export type SplitIconSetAsyncCallback = (
name: string,
data: ExtendedIconifyIcon | null
) => Promise<unknown>;
/** /**
* Extract icons from an icon set * Extract icons from an icon set
@ -49,3 +54,40 @@ export function parseIconSet(
return names; return names;
} }
/**
* Async version of parseIconSet()
*/
export async function parseIconSetAsync(
data: IconifyJSON,
callback: SplitIconSetAsyncCallback
): Promise<string[]> {
// List of icon names
const names: string[] = [];
// Must be an object and must have 'icons' property
if (typeof data !== 'object' || typeof data.icons !== 'object') {
return names;
}
// Check for missing icons list returned by API
if (data.not_found instanceof Array) {
for (let i = 0; i < data.not_found.length; i++) {
const name = data.not_found[i];
await callback(name, null);
names.push(name);
}
}
// Get tree
const tree = getIconsTree(data);
for (const name in tree) {
const item = tree[name];
if (item) {
await callback(name, internalGetIconData(data, name, item));
names.push(name);
}
}
return names;
}

View File

@ -39,7 +39,7 @@ export type {
// Icon set functions // Icon set functions
export { getIconsTree } from './icon-set/tree'; export { getIconsTree } from './icon-set/tree';
export type { ParentIconsList, ParentIconsTree } from './icon-set/tree'; export type { ParentIconsList, ParentIconsTree } from './icon-set/tree';
export { parseIconSet } from './icon-set/parse'; export { parseIconSet, parseIconSetAsync } from './icon-set/parse';
export { validateIconSet } from './icon-set/validate'; export { validateIconSet } from './icon-set/validate';
export { quicklyValidateIconSet } from './icon-set/validate-basic'; export { quicklyValidateIconSet } from './icon-set/validate-basic';
export { expandIconSet } from './icon-set/expand'; export { expandIconSet } from './icon-set/expand';

View File

@ -1,5 +1,5 @@
import type { ExtendedIconifyIcon } from '@iconify/types'; import type { ExtendedIconifyIcon } from '@iconify/types';
import { parseIconSet } from '../lib/icon-set/parse'; import { parseIconSet, parseIconSetAsync } from '../lib/icon-set/parse';
describe('Testing parsing icon set', () => { describe('Testing parsing icon set', () => {
test('Simple icon set', () => { test('Simple icon set', () => {
@ -137,7 +137,7 @@ describe('Testing parsing icon set', () => {
expect(parsedNames).toEqual(expectedNames); expect(parsedNames).toEqual(expectedNames);
}); });
test('Nested aliases', () => { test('Nested aliases', async () => {
// Names list // Names list
let names: string[] = [ let names: string[] = [
'icon1', 'icon1',
@ -234,7 +234,7 @@ describe('Testing parsing icon set', () => {
}; };
// Do stuff // Do stuff
const parsedNames = parseIconSet( const parsedNames = await parseIconSetAsync(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
@ -298,13 +298,19 @@ describe('Testing parsing icon set', () => {
height: 24, height: 24,
}, },
(name, data) => { (name, data) => {
// Make sure name exists in array of pending names return new Promise((fulfill) => {
const index = names.indexOf(name); // Make sure name exists in array of pending names
expect(index).not.toBe(-1); const index = names.indexOf(name);
names = names.slice(0, index).concat(names.slice(index + 1)); expect(index).not.toBe(-1);
names = names
.slice(0, index)
.concat(names.slice(index + 1));
// Check icon data // Check icon data
expect(data).toEqual(expected[name]); expect(data).toEqual(expected[name]);
fulfill(void 0);
});
} }
); );