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

Few more changes to parseIconSet function to make it cleaner

This commit is contained in:
Vjacheslav Trushkin 2021-10-12 18:19:46 +03:00
parent 0691bb6836
commit c1b22647eb
11 changed files with 46 additions and 78 deletions

View File

@ -1,17 +1,17 @@
{
"name": "@iconify/core",
"version": "1.2.0-beta.3",
"version": "1.2.0-beta.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@iconify/core",
"version": "1.2.0-beta.3",
"version": "1.2.0-beta.4",
"license": "(Apache-2.0 OR GPL-2.0)",
"dependencies": {
"@iconify/api-redundancy": "^1.0.2",
"@iconify/types": "^1.0.10",
"@iconify/utils": "^1.0.15",
"@iconify/utils": "^1.0.16",
"cross-fetch": "^3.1.4"
},
"devDependencies": {
@ -737,9 +737,9 @@
"integrity": "sha512-SN3z6einVeUckDQiE8p4POF7X4hk4/y2+a7a4ogJOCxX5XT6z1zXNN8dwS5O1vloXpc6mkHizRZm2qPnhK6NnQ=="
},
"node_modules/@iconify/utils": {
"version": "1.0.15",
"resolved": "https://registry.npmjs.org/@iconify/utils/-/utils-1.0.15.tgz",
"integrity": "sha512-ENcZMIaU6niedrY9rNMPofYOlN+d/7k4sDxxQbkcuu/MUyUrVugDLUeNDln9CmNcrwbHzBAoItkGQ21H5gXiPw==",
"version": "1.0.16",
"resolved": "https://registry.npmjs.org/@iconify/utils/-/utils-1.0.16.tgz",
"integrity": "sha512-D72Efq/6GbClt/UQOtMgYfCaV7VWdb4hmICsD4Z219yKiW2D71v+29Um5CsuBBEaHz/y0MY2q2gUTNVkOEc4mA==",
"dependencies": {
"@iconify/types": "^1.0.10"
}
@ -5915,9 +5915,9 @@
"integrity": "sha512-SN3z6einVeUckDQiE8p4POF7X4hk4/y2+a7a4ogJOCxX5XT6z1zXNN8dwS5O1vloXpc6mkHizRZm2qPnhK6NnQ=="
},
"@iconify/utils": {
"version": "1.0.15",
"resolved": "https://registry.npmjs.org/@iconify/utils/-/utils-1.0.15.tgz",
"integrity": "sha512-ENcZMIaU6niedrY9rNMPofYOlN+d/7k4sDxxQbkcuu/MUyUrVugDLUeNDln9CmNcrwbHzBAoItkGQ21H5gXiPw==",
"version": "1.0.16",
"resolved": "https://registry.npmjs.org/@iconify/utils/-/utils-1.0.16.tgz",
"integrity": "sha512-D72Efq/6GbClt/UQOtMgYfCaV7VWdb4hmICsD4Z219yKiW2D71v+29Um5CsuBBEaHz/y0MY2q2gUTNVkOEc4mA==",
"requires": {
"@iconify/types": "^1.0.10"
}

View File

@ -102,7 +102,7 @@
"dependencies": {
"@iconify/api-redundancy": "^1.0.2",
"@iconify/types": "^1.0.10",
"@iconify/utils": "^1.0.15",
"@iconify/utils": "^1.0.16",
"cross-fetch": "^3.1.4"
},
"devDependencies": {

View File

@ -110,7 +110,7 @@ describe('Testing storage', () => {
},
height: 24,
})
).toBe(true);
).toEqual(['icon1', 'icon2']);
expect(Object.keys(storage.icons)).toEqual(['icon1', 'icon2']);
@ -169,7 +169,7 @@ describe('Testing storage', () => {
width: 128,
height: 128,
})
).toBe(true);
).toEqual(['16-chevron-left', '16-chevron-right']);
expect(Object.keys(storage.icons)).toEqual([
'16-chevron-left',
@ -246,7 +246,7 @@ describe('Testing storage', () => {
width: 128,
height: 128,
})
).toBe(true);
).toEqual(['16-chevron-left', '16-chevron-right']);
// List icons
expect(listIcons('', prefix)).toEqual([

View File

@ -186,18 +186,17 @@ function loadNewIcons(provider: string, prefix: string, icons: string[]): void {
} else {
// Add icons to storage
try {
const added = addIconSet(
const parsed = addIconSet(
storage,
data as IconifyJSON,
'all'
data as IconifyJSON
);
if (typeof added === 'boolean') {
if (!parsed.length) {
return;
}
// Remove added icons from pending list
const pending = providerPendingIcons[prefix];
added.forEach((name) => {
parsed.forEach((name) => {
delete pending[name];
});

View File

@ -211,7 +211,7 @@ export const loadCache: LoadIconsCache = (): void => {
const provider = data.provider;
const prefix = data.data.prefix;
const storage = getStorage(provider, prefix);
valid = addIconSet(storage, data.data) as boolean;
valid = addIconSet(storage, data.data).length > 0;
}
} catch (err) {
valid = false;

View File

@ -1,7 +1,6 @@
import type { IconifyJSON, IconifyIcon } from '@iconify/types';
import type { FullIconifyIcon } from '@iconify/utils/lib/icon';
import { fullIcon } from '@iconify/utils/lib/icon';
import type { ParseIconSetTracking } from '@iconify/utils/lib/icon-set/parse';
import { parseIconSet } from '@iconify/utils/lib/icon-set/parse';
/**
@ -55,25 +54,17 @@ export function getStorage(provider: string, prefix: string): IconStorage {
/**
* Add icon set to storage
*
* Returns array of added icons if 'list' is true and icons were added successfully
* Returns array of added icons
*/
export function addIconSet(
storage: IconStorage,
data: IconifyJSON,
list: ParseIconSetTracking = 'none'
): boolean | string[] {
export function addIconSet(storage: IconStorage, data: IconifyJSON): string[] {
const t = Date.now();
return parseIconSet(
data,
(name, icon: FullIconifyIcon | null) => {
if (icon) {
storage.icons[name] = icon;
} else {
storage.missing[name] = t;
}
},
{ list }
);
return parseIconSet(data, (name, icon: FullIconifyIcon | null) => {
if (icon) {
storage.icons[name] = icon;
} else {
storage.missing[name] = t;
}
});
}
/**

View File

@ -113,7 +113,7 @@ describe('Testing storage', () => {
},
height: 24,
})
).toBe(true);
).toEqual(['icon1', 'icon2']);
expect(Object.keys(storage.icons)).toEqual(['icon1', 'icon2']);
@ -172,7 +172,7 @@ describe('Testing storage', () => {
width: 128,
height: 128,
})
).toBe(true);
).toEqual(['16-chevron-left', '16-chevron-right']);
expect(Object.keys(storage.icons)).toEqual([
'16-chevron-left',
@ -249,7 +249,7 @@ describe('Testing storage', () => {
width: 128,
height: 128,
})
).toBe(true);
).toEqual(['16-chevron-left', '16-chevron-right']);
// List icons
expect(listIcons('', prefix)).toEqual([

View File

@ -2,7 +2,7 @@
"name": "@iconify/utils",
"description": "Common functions for working with Iconify icon sets used by various packages.",
"author": "Vjacheslav Trushkin",
"version": "1.0.15",
"version": "1.0.16",
"license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://iconify.design/",

View File

@ -57,7 +57,7 @@ describe('Testing parsing icon set', () => {
expect(data).toEqual(expected[name]);
}
)
).toBe(true);
).toEqual(['missing', 'icon1', 'icon2']);
// All names should have been parsed
expect(names).toEqual([]);

View File

@ -3,15 +3,6 @@ import { FullIconifyIcon, iconDefaults } from '../icon';
import { getIconData } from './get-icon';
import { IconSetValidationOptions, validateIconSet } from './validate';
/**
* What to track when parsing icon set:
*
* none - do not track anything, return true on success
* valid - track valid icons, return list of valid icons on success
* all - track valid and missing icons, return full list on success
*/
export type ParseIconSetTracking = 'none' | 'valid' | 'all';
/**
* Which aliases to parse:
*
@ -45,25 +36,30 @@ export function isVariation(item: IconifyAlias): boolean {
export interface ParseIconSetOptions {
validate?: boolean | IconSetValidationOptions;
list?: ParseIconSetTracking;
aliases?: ParseIconSetAliases;
}
/**
* Extract icons from an icon set
*
* Returns list of icons that were found in icon set
*/
export function parseIconSet(
data: IconifyJSON,
callback: SplitIconSetCallback,
options?: ParseIconSetOptions
): boolean | string[] {
): string[] {
options = options || {};
// List of icon names
const names: string[] = [];
// Options
options = options || {};
const list = options.list || 'none';
// Must be an object and must have 'icons' property
if (typeof data !== 'object' || typeof data.icons !== 'object') {
return names;
}
// Validate icon set
const validate = options.validate;
if (validate !== false) {
// Validate icon set
@ -73,30 +69,18 @@ export function parseIconSet(
typeof validate === 'object' ? validate : { fix: true }
);
} catch (err) {
return list === 'none' ? false : [];
return names;
}
}
// Must be an object
if (typeof data !== 'object') {
return list === 'none' ? false : names;
}
// Check for missing icons list returned by API
if (data.not_found instanceof Array) {
data.not_found.forEach((name) => {
callback(name, null);
if (list === 'all') {
names.push(name);
}
names.push(name);
});
}
// Must have 'icons' object
if (typeof data.icons !== 'object') {
return list === 'none' ? false : names;
}
// Get icons
const icons = data.icons;
Object.keys(icons).forEach((name) => {
@ -125,5 +109,5 @@ export function parseIconSet(
});
}
return list === 'none' ? names.length > 0 : names;
return names;
}

View File

@ -58,7 +58,7 @@ describe('Testing parsing icon set', () => {
expect(data).toEqual(expected[name]);
}
)
).toBe(true);
).toEqual(['missing', 'icon1', 'icon2']);
// All names should have been parsed
expect(names).toEqual([]);
@ -149,9 +149,6 @@ describe('Testing parsing icon set', () => {
// Check icon data
expect(data).toEqual(expected[name]);
},
{
list: 'all',
}
)
).toEqual(namesCopy);
@ -349,9 +346,6 @@ describe('Testing parsing icon set', () => {
// Check icon data
expect(data).toEqual(expected[name]);
},
{
list: 'valid',
}
)
).toEqual(namesCopy);