Files
infocom-systems-design/node_modules/@iconify/utils/lib/icon/name.js
2025-10-03 22:27:28 +03:00

58 lines
1.7 KiB
JavaScript

/**
* Expression to test part of icon name.
*
* Used when loading icons from Iconify API due to project naming convension.
* Ignored when using custom icon sets - convension does not apply.
*/
const matchIconName = /^[a-z0-9]+(-[a-z0-9]+)*$/;
/**
* Convert string icon name to IconifyIconName object.
*/
const stringToIcon = (value, validate, allowSimpleName, provider = "") => {
const colonSeparated = value.split(":");
if (value.slice(0, 1) === "@") {
if (colonSeparated.length < 2 || colonSeparated.length > 3) return null;
provider = colonSeparated.shift().slice(1);
}
if (colonSeparated.length > 3 || !colonSeparated.length) return null;
if (colonSeparated.length > 1) {
const name$1 = colonSeparated.pop();
const prefix = colonSeparated.pop();
const result = {
provider: colonSeparated.length > 0 ? colonSeparated[0] : provider,
prefix,
name: name$1
};
return validate && !validateIconName(result) ? null : result;
}
const name = colonSeparated[0];
const dashSeparated = name.split("-");
if (dashSeparated.length > 1) {
const result = {
provider,
prefix: dashSeparated.shift(),
name: dashSeparated.join("-")
};
return validate && !validateIconName(result) ? null : result;
}
if (allowSimpleName && provider === "") {
const result = {
provider,
prefix: "",
name
};
return validate && !validateIconName(result, allowSimpleName) ? null : result;
}
return null;
};
/**
* Check if icon is valid.
*
* This function is not part of stringToIcon because validation is not needed for most code.
*/
const validateIconName = (icon, allowSimpleName) => {
if (!icon) return false;
return !!((allowSimpleName && icon.prefix === "" || !!icon.prefix) && !!icon.name);
};
export { matchIconName, stringToIcon, validateIconName };