mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.4 KiB
44 lines
1.4 KiB
import { defaultExtendedIconProps, defaultIconDimensions } from "../icon/defaults.js";
|
|
|
|
/**
|
|
* Optional properties
|
|
*/
|
|
const optionalPropertyDefaults = {
|
|
provider: "",
|
|
aliases: {},
|
|
not_found: {},
|
|
...defaultIconDimensions
|
|
};
|
|
/**
|
|
* Check props
|
|
*/
|
|
function checkOptionalProps(item, defaults) {
|
|
for (const prop in defaults) if (prop in item && typeof item[prop] !== typeof defaults[prop]) return false;
|
|
return true;
|
|
}
|
|
/**
|
|
* Validate icon set, return it as IconifyJSON on success, null on failure
|
|
*
|
|
* Unlike validateIconSet(), this function is very basic.
|
|
* It does not throw exceptions, it does not check metadata, it does not fix stuff.
|
|
*/
|
|
function quicklyValidateIconSet(obj) {
|
|
if (typeof obj !== "object" || obj === null) return null;
|
|
const data = obj;
|
|
if (typeof data.prefix !== "string" || !obj.icons || typeof obj.icons !== "object") return null;
|
|
if (!checkOptionalProps(obj, optionalPropertyDefaults)) return null;
|
|
const icons = data.icons;
|
|
for (const name in icons) {
|
|
const icon = icons[name];
|
|
if (!name || typeof icon.body !== "string" || !checkOptionalProps(icon, defaultExtendedIconProps)) return null;
|
|
}
|
|
const aliases = data.aliases || Object.create(null);
|
|
for (const name in aliases) {
|
|
const icon = aliases[name];
|
|
const parent = icon.parent;
|
|
if (!name || typeof parent !== "string" || !icons[parent] && !aliases[parent] || !checkOptionalProps(icon, defaultExtendedIconProps)) return null;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
export { quicklyValidateIconSet };
|