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.
58 lines
1.7 KiB
58 lines
1.7 KiB
/**
|
|
* 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 };
|