UNPKG

open-icon

Version:

Primary Open Icon package with generated catalog helpers and icon access APIs.

108 lines 3.83 kB
import { OPEN_ICON_ALIAS_TO_NAME, OPEN_ICON_CATEGORIES, OPEN_ICON_CATEGORY_TO_NAMES, OPEN_ICON_KEY_TO_NAME, OPEN_ICON_NAME_TO_FILE, OPEN_ICON_NAMES, } from './generated/openIconCatalog.generated.js'; import { OPEN_ICON_SVG_LOADER_BY_NAME } from './generated/openIconLoaders.generated.js'; export const Icons = OPEN_ICON_KEY_TO_NAME; export { OPEN_ICON_ALIAS_TO_NAME, OPEN_ICON_CATEGORIES, OPEN_ICON_CATEGORY_TO_NAMES, OPEN_ICON_KEY_TO_NAME, OPEN_ICON_NAME_TO_FILE, OPEN_ICON_NAMES, }; const OPEN_ICON_NAME_SET = new Set(OPEN_ICON_NAMES); const OPEN_ICON_LOADED_SVG_BY_NAME = new Map(); const normalizeSegment = (value) => { return value .toLowerCase() .trim() .replace(/&/g, ' and ') .replace(/["'`]/g, '') .replace(/[\s_+]+/g, '-') .replace(/[^a-z0-9-]+/g, '-') .replace(/-+/g, '-') .replace(/^-|-$/g, ''); }; const normalizePath = (value) => { return value .replace(/\\/g, '/') .split('/') .map(normalizeSegment) .filter(Boolean) .join('/'); }; const stripIconPrefix = (value) => value.replace(/^icon[-_]*/i, ''); const stripPackagePathPrefixes = (value) => { const segments = value.split('/').filter(Boolean); const iconsIndex = segments.indexOf('icons'); if (iconsIndex >= 0 && iconsIndex < segments.length - 1) { return segments.slice(iconsIndex + 1).join('/'); } return value; }; const normalizeLookupValue = (value) => { const withoutExtension = value.replace(/\.svg$/i, ''); const withoutPrefix = stripPackagePathPrefixes(withoutExtension); const normalizedPath = normalizePath(withoutPrefix); if (!normalizedPath) { return ''; } const segments = normalizedPath.split('/'); const basename = segments.pop() ?? ''; const strippedBasename = normalizeSegment(stripIconPrefix(basename)); const normalizedBasename = normalizeSegment(basename); const withStripped = strippedBasename ? segments.length ? `${segments.join('/')}/${strippedBasename}` : strippedBasename : ''; return withStripped || normalizedPath || normalizedBasename; }; export const isOpenIconName = (value) => OPEN_ICON_NAME_SET.has(value); export const resolveOpenIconName = (value) => { const normalized = normalizeLookupValue(value); if (!normalized) { return null; } const direct = OPEN_ICON_ALIAS_TO_NAME[normalized]; if (direct) { return direct; } if (OPEN_ICON_NAME_SET.has(normalized)) { return normalized; } const basename = normalized.split('/').pop() ?? normalized; const fallback = OPEN_ICON_ALIAS_TO_NAME[basename]; return fallback ?? null; }; export const getOpenIconFilePath = (value) => { const iconName = resolveOpenIconName(value); if (!iconName) { return null; } return OPEN_ICON_NAME_TO_FILE[iconName] ?? null; }; export const getOpenIconImportPath = (value) => { const filePath = getOpenIconFilePath(value); if (!filePath) { return null; } return encodeURI(`open-icon-svg/${filePath}`); }; export const peekLoadedIcon = (value) => { const iconName = resolveOpenIconName(value); if (!iconName) { return null; } return OPEN_ICON_LOADED_SVG_BY_NAME.get(iconName) ?? null; }; export const loadIcon = async (value) => { const iconName = resolveOpenIconName(value); if (!iconName) { return null; } const cached = OPEN_ICON_LOADED_SVG_BY_NAME.get(iconName); if (cached) { return cached; } const loader = OPEN_ICON_SVG_LOADER_BY_NAME[iconName]; if (!loader) { return null; } const svg = await loader(); OPEN_ICON_LOADED_SVG_BY_NAME.set(iconName, svg); return svg; }; //# sourceMappingURL=runtime.js.map