@iconify/utils
Advanced tools
Comparing version 1.0.12 to 1.0.13
@@ -6,3 +6,20 @@ "use strict"; | ||
const maxDisplayHeight = 24; | ||
const maxSamplesCount = 3; | ||
/** | ||
* Check if displayHeight value is valid, returns 0 on failure | ||
*/ | ||
function validateDisplayHeight(value) { | ||
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 | ||
@@ -40,34 +57,57 @@ */ | ||
} | ||
// Generate data | ||
const result = { | ||
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, | ||
}; | ||
// 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; | ||
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; | ||
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 = []; | ||
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; | ||
} | ||
} | ||
@@ -79,3 +119,3 @@ // Add height | ||
if (num > 0) { | ||
result.height = num; | ||
info.height = num; | ||
} | ||
@@ -87,31 +127,23 @@ } | ||
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.length) { | ||
switch (info.height.length) { | ||
case 0: | ||
delete result.height; | ||
delete info.height; | ||
break; | ||
case 1: | ||
result.height = result.height[0]; | ||
info.height = info.height[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; | ||
const displayHeight = validateDisplayHeight(info.height); | ||
if (displayHeight) { | ||
info.displayHeight = displayHeight; | ||
} | ||
while (result.displayHeight > maxDisplayHeight) { | ||
result.displayHeight /= 2; | ||
} | ||
if (result.displayHeight !== Math.round(result.displayHeight) || | ||
result.displayHeight < minDisplayHeight || | ||
result.displayHeight > maxDisplayHeight) { | ||
delete result.displayHeight; | ||
} | ||
} | ||
@@ -122,23 +154,30 @@ ['samplesHeight', 'displayHeight'].forEach((prop) => { | ||
// Convert from source.displayHeight or source.samplesHeight | ||
const num = parseInt(value); | ||
if (num >= minDisplayHeight && | ||
num <= maxDisplayHeight && | ||
Math.round(num) === num) { | ||
result.displayHeight = num; | ||
const displayHeight = validateDisplayHeight(parseInt(value)); | ||
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) => { | ||
@@ -152,16 +191,16 @@ const value = source[key]; | ||
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; | ||
} | ||
exports.convertIconSetInfo = convertIconSetInfo; |
@@ -5,3 +5,3 @@ { | ||
"author": "Vjacheslav Trushkin", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"license": "MIT", | ||
@@ -108,3 +108,3 @@ "bugs": "https://github.com/iconify/iconify/issues", | ||
"dependencies": { | ||
"@iconify/types": "^1.0.7" | ||
"@iconify/types": "^1.0.10" | ||
}, | ||
@@ -111,0 +111,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90293
2924
Updated@iconify/types@^1.0.10