2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Make few icon set info fields optional, clean up code for converting icon set info

This commit is contained in:
Vjacheslav Trushkin 2021-10-05 22:37:57 +03:00
parent fe1d673603
commit c4dde40fc8
8 changed files with 153 additions and 152 deletions

View File

@ -1,21 +1,21 @@
{
"name": "@iconify/types",
"version": "1.0.9",
"version": "1.0.10",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@iconify/types",
"version": "1.0.9",
"version": "1.0.10",
"license": "(Apache-2.0 OR GPL-2.0)",
"devDependencies": {
"typescript": "^4.1.2"
"typescript": "^4.4.3"
}
},
"node_modules/typescript": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz",
"integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==",
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz",
"integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
@ -28,9 +28,9 @@
},
"dependencies": {
"typescript": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz",
"integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==",
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz",
"integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==",
"dev": true
}
}

View File

@ -1,7 +1,7 @@
{
"name": "@iconify/types",
"description": "Types for Iconify data",
"version": "1.0.9",
"version": "1.0.10",
"author": "Vjacheslav Trushkin",
"license": "(Apache-2.0 OR GPL-2.0)",
"main": "./index.js",
@ -17,6 +17,6 @@
"directory": "packages/types"
},
"devDependencies": {
"typescript": "^4.1.2"
"typescript": "^4.4.3"
}
}

View File

@ -144,7 +144,7 @@ export interface IconifyInfo {
};
// Array of icons that should be used for samples in icon sets list.
samples: string[];
samples?: string[];
// Icon grid: number or array of numbers.
height?: number | number[];
@ -156,8 +156,8 @@ export interface IconifyInfo {
category?: string;
// Palette status. True if icons have predefined color scheme, false if icons use currentColor.
// Icon set should not mix icons with and without palette to simplify search.
palette: boolean;
// Ideally, icon set should not mix icons with and without palette to simplify search.
palette?: boolean;
}
/**

View File

@ -1,15 +1,15 @@
{
"name": "@iconify/utils",
"version": "1.0.10",
"version": "1.0.13",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@iconify/utils",
"version": "1.0.10",
"version": "1.0.13",
"license": "MIT",
"dependencies": {
"@iconify/types": "^1.0.7"
"@iconify/types": "^1.0.10"
},
"devDependencies": {
"@iconify/library-builder": "^1.0.3",
@ -723,9 +723,9 @@
}
},
"node_modules/@iconify/types": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@iconify/types/-/types-1.0.7.tgz",
"integrity": "sha512-zdwkVc67O9SqI/svsogD2s+XB3Uk4Xx+PrMEn5Qm3ktKSXFveZL0iP3yzqbrFg1KRRd93nRs4QycLaIRMhA0Pg=="
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/@iconify/types/-/types-1.0.10.tgz",
"integrity": "sha512-SN3z6einVeUckDQiE8p4POF7X4hk4/y2+a7a4ogJOCxX5XT6z1zXNN8dwS5O1vloXpc6mkHizRZm2qPnhK6NnQ=="
},
"node_modules/@istanbuljs/load-nyc-config": {
"version": "1.1.0",
@ -5832,9 +5832,9 @@
}
},
"@iconify/types": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@iconify/types/-/types-1.0.7.tgz",
"integrity": "sha512-zdwkVc67O9SqI/svsogD2s+XB3Uk4Xx+PrMEn5Qm3ktKSXFveZL0iP3yzqbrFg1KRRd93nRs4QycLaIRMhA0Pg=="
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/@iconify/types/-/types-1.0.10.tgz",
"integrity": "sha512-SN3z6einVeUckDQiE8p4POF7X4hk4/y2+a7a4ogJOCxX5XT6z1zXNN8dwS5O1vloXpc6mkHizRZm2qPnhK6NnQ=="
},
"@istanbuljs/load-nyc-config": {
"version": "1.1.0",

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.12",
"version": "1.0.13",
"license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://iconify.design/",
@ -104,7 +104,7 @@
}
},
"dependencies": {
"@iconify/types": "^1.0.7"
"@iconify/types": "^1.0.10"
},
"devDependencies": {
"@iconify/library-builder": "^1.0.3",

View File

@ -14,20 +14,12 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 0,
author: {
name: 'Unknown',
url: '',
name: '',
},
license: {
title: 'Unknown',
spdx: '',
url: '',
title: '',
},
samples: [],
palette: false,
category: '',
};
expect(result).toEqual(expected);
@ -67,8 +59,6 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 0,
author: {
name: 'Author',
url: 'https://localhost/',
@ -82,7 +72,6 @@ describe('Testing convertIconSetInfo', () => {
samples: ['arrow-left', 'arrow-right', 'arrow-up'],
displayHeight: 24,
palette: true,
category: '',
};
expect(result).toEqual(expected);
@ -106,21 +95,15 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 100,
author: {
name: 'Author',
url: '',
},
license: {
title: 'BSD',
spdx: '',
url: '',
},
height: [16, 18],
samples: [],
palette: true,
category: '',
};
expect(result).toEqual(expected);
@ -182,7 +165,6 @@ describe('Testing convertIconSetInfo', () => {
const result = convertIconSetInfo(raw);
const expected = {
name: 'Ant Design Icons',
version: '',
total: 728,
author: {
name: 'HeskeyBaozi',
@ -190,8 +172,6 @@ describe('Testing convertIconSetInfo', () => {
},
license: {
title: 'MIT',
spdx: '',
url: '',
},
height: 16,
samples: ['pushpin', 'pie-chart-outline', 'user-add-outline'],
@ -227,7 +207,6 @@ describe('Testing convertIconSetInfo', () => {
},
license: {
title: 'Open Font License',
spdx: '',
url: 'http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL',
},
samples: ['wrench', 'bell-o', 'user-o'],
@ -248,7 +227,6 @@ describe('Testing convertIconSetInfo', () => {
},
license: {
title: 'Open Font License',
spdx: '',
url: 'http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL',
},
samples: ['wrench', 'bell-o', 'user-o'],

View File

@ -2,6 +2,7 @@ import type { IconifyInfo } from '@iconify/types';
const minDisplayHeight = 16;
const maxDisplayHeight = 24;
const maxSamplesCount = 3;
/**
* Item provided by API or loaded from collections.json, slightly different from IconifyInfo
@ -38,6 +39,24 @@ export interface LegacyIconifyInfo {
palette?: 'Colorless' | 'Colorful';
}
/**
* Check if displayHeight value is valid, returns 0 on failure
*/
function validateDisplayHeight(value: number): number {
while (value < minDisplayHeight) {
value *= 2;
}
while (value > maxDisplayHeight) {
value /= 2;
}
return value === Math.round(value) &&
value >= minDisplayHeight &&
value <= maxDisplayHeight
? value
: 0;
}
/**
* Convert data to valid CollectionInfo
*/
@ -83,48 +102,75 @@ export function convertIconSetInfo(
return null;
}
// Generate data
const result: IconifyInfo = {
name: name,
total: typeof source.total === 'number' ? source.total : 0,
version: typeof source.version === 'string' ? source.version : '',
author: {
name: getSourceNestedString(
'author',
'name',
typeof source.author === 'string' ? source.author : 'Unknown'
),
url: getSourceNestedString('author', 'url', ''),
},
license: {
title: getSourceNestedString(
'license',
'title',
typeof source.license === 'string' ? source.license : 'Unknown'
),
spdx: getSourceNestedString('license', 'spdx', ''),
url: getSourceNestedString('license', 'url', ''),
},
samples: [],
category: typeof source.category === 'string' ? source.category : '',
palette: typeof source.palette === 'boolean' ? source.palette : false,
};
// Generate basic info
const info = {
name,
} as IconifyInfo;
// Total as string
if (typeof source.total === 'string') {
const num = parseInt(source.total);
if (num > 0) {
result.total = num;
// Number of icons
switch (typeof source.total) {
case 'number':
info.total = source.total;
break;
case 'string': {
const num = parseInt(source.total);
if (num > 0) {
info.total = num;
}
break;
}
}
// Add samples
// Version
if (typeof source.version === 'string') {
info.version = source.version;
}
// Author
info.author = {
name: getSourceNestedString(
'author',
'name',
typeof source.author === 'string' ? source.author : ''
),
};
if (typeof source.author === 'object') {
const sourceAuthor = source.author as Record<string, string>;
if (typeof sourceAuthor.url === 'string') {
info.author.url = sourceAuthor.url;
}
}
// License
info.license = {
title: getSourceNestedString(
'license',
'title',
typeof source.license === 'string' ? source.license : ''
),
};
if (typeof source.license === 'object') {
const sourceLicense = source.license as Record<string, string>;
if (typeof sourceLicense.spdx === 'string') {
info.license.spdx = sourceLicense.spdx;
}
if (typeof sourceLicense.url === 'string') {
info.license.url = sourceLicense.url;
}
}
// Samples
if (source.samples instanceof Array) {
const samples: string[] = [];
source.samples.forEach((item) => {
if (result.samples.length < 3 && typeof item === 'string') {
result.samples.push(item);
if (typeof item === 'string' && samples.length < maxSamplesCount) {
samples.push(item);
}
});
if (samples.length) {
info.samples = samples;
}
}
// Add height
@ -134,7 +180,7 @@ export function convertIconSetInfo(
) {
const num = parseInt(source.height as string);
if (num > 0) {
result.height = num;
info.height = num;
}
}
@ -142,40 +188,29 @@ export function convertIconSetInfo(
source.height.forEach((item) => {
const num = parseInt(item);
if (num > 0) {
if (!(result.height instanceof Array)) {
result.height = [];
if (!(info.height instanceof Array)) {
info.height = [];
}
result.height.push(num);
info.height.push(num);
}
});
switch ((result.height as number[]).length) {
switch ((info.height as number[]).length) {
case 0:
delete result.height;
delete info.height;
break;
case 1:
result.height = (result.height as number[])[0];
info.height = (info.height as number[])[0];
}
}
// Add display height
if (typeof result.height === 'number') {
if (typeof info.height === 'number') {
// Convert from height
result.displayHeight = result.height;
while (result.displayHeight < minDisplayHeight) {
result.displayHeight *= 2;
}
while (result.displayHeight > maxDisplayHeight) {
result.displayHeight /= 2;
}
if (
result.displayHeight !== Math.round(result.displayHeight) ||
result.displayHeight < minDisplayHeight ||
result.displayHeight > maxDisplayHeight
) {
delete result.displayHeight;
const displayHeight = validateDisplayHeight(info.height);
if (displayHeight) {
info.displayHeight = displayHeight;
}
}
@ -183,32 +218,41 @@ export function convertIconSetInfo(
const value = source[prop];
if (typeof value === 'number' || typeof value === 'string') {
// Convert from source.displayHeight or source.samplesHeight
const num = parseInt(value as string);
if (
num >= minDisplayHeight &&
num <= maxDisplayHeight &&
Math.round(num) === num
) {
result.displayHeight = num;
const displayHeight = validateDisplayHeight(
parseInt(value as string)
);
if (displayHeight) {
info.displayHeight = displayHeight;
}
}
});
// Convert palette from string value
if (typeof source.palette === 'string') {
switch (source.palette.toLowerCase()) {
case 'colorless': // Iconify v1
case 'false': // Boolean as string
result.palette = false;
break;
case 'colorful': // Iconify v1
case 'true': // Boolean as string
result.palette = true;
}
// Category
if (typeof source.category === 'string') {
info.category = source.category;
}
// Parse all old keys
// Palette
switch (typeof source.palette) {
case 'boolean':
info.palette = source.palette;
break;
case 'string':
switch (source.palette.toLowerCase()) {
case 'colorless': // Iconify v1
case 'false': // Boolean as string
info.palette = false;
break;
case 'colorful': // Iconify v1
case 'true': // Boolean as string
info.palette = true;
}
break;
}
// Parse all old strings
Object.keys(source).forEach((key) => {
const value = source[key];
if (typeof value !== 'string') {
@ -217,20 +261,20 @@ export function convertIconSetInfo(
switch (key) {
case 'url':
case 'uri':
result.author.url = value;
info.author.url = value;
break;
case 'licenseURL':
case 'licenseURI':
result.license.url = value;
info.license.url = value;
break;
case 'licenseID':
case 'licenseSPDX':
result.license.spdx = value;
info.license.spdx = value;
break;
}
});
return result;
return info;
}

View File

@ -15,20 +15,12 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 0,
author: {
name: 'Unknown',
url: '',
name: '',
},
license: {
title: 'Unknown',
spdx: '',
url: '',
title: '',
},
samples: [],
palette: false,
category: '',
};
expect(result).toEqual(expected);
@ -68,8 +60,6 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 0,
author: {
name: 'Author',
url: 'https://localhost/',
@ -83,7 +73,6 @@ describe('Testing convertIconSetInfo', () => {
samples: ['arrow-left', 'arrow-right', 'arrow-up'],
displayHeight: 24,
palette: true,
category: '',
};
expect(result).toEqual(expected);
@ -107,21 +96,15 @@ describe('Testing convertIconSetInfo', () => {
});
expected = {
name: 'Foo',
version: '',
total: 100,
author: {
name: 'Author',
url: '',
},
license: {
title: 'BSD',
spdx: '',
url: '',
},
height: [16, 18],
samples: [],
palette: true,
category: '',
};
expect(result).toEqual(expected);
@ -183,7 +166,6 @@ describe('Testing convertIconSetInfo', () => {
const result = convertIconSetInfo(raw);
const expected: IconifyInfo = {
name: 'Ant Design Icons',
version: '',
total: 728,
author: {
name: 'HeskeyBaozi',
@ -191,8 +173,6 @@ describe('Testing convertIconSetInfo', () => {
},
license: {
title: 'MIT',
spdx: '',
url: '',
},
height: 16,
samples: ['pushpin', 'pie-chart-outline', 'user-add-outline'],
@ -228,7 +208,6 @@ describe('Testing convertIconSetInfo', () => {
},
license: {
title: 'Open Font License',
spdx: '',
url: 'http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL',
},
samples: ['wrench', 'bell-o', 'user-o'],