@iconify/utils
Version:
Common functions for working with Iconify icon sets used by various packages.
63 lines (60 loc) • 1.59 kB
JavaScript
import { defaultIconDimensions, defaultExtendedIconProps } from '../icon/defaults.mjs';
const optionalPropertyDefaults = {
provider: "",
aliases: {},
not_found: {},
...defaultIconDimensions
};
function checkOptionalProps(item, defaults) {
for (const prop in defaults) {
if (prop in item && typeof item[prop] !== typeof defaults[prop]) {
return false;
}
}
return true;
}
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 cannot be empty
!name || // Must have body
typeof icon.body !== "string" || // Check other props
!checkOptionalProps(
icon,
defaultExtendedIconProps
)
) {
return null;
}
}
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
for (const name in aliases) {
const icon = aliases[name];
const parent = icon.parent;
if (
// Name cannot be empty
!name || // Parent must be set and point to existing icon
typeof parent !== "string" || !icons[parent] && !aliases[parent] || // Check other props
!checkOptionalProps(
icon,
defaultExtendedIconProps
)
) {
return null;
}
}
return data;
}
export { quicklyValidateIconSet };