UNPKG

2.18 kBJavaScriptView Raw
1import { g as getAssetPath } from './index-468d974f.js';
2
3let CACHED_MAP;
4const getIconMap = () => {
5 if (typeof window === 'undefined') {
6 return new Map();
7 }
8 else {
9 if (!CACHED_MAP) {
10 const win = window;
11 win.Ionicons = win.Ionicons || {};
12 CACHED_MAP = win.Ionicons.map = win.Ionicons.map || new Map();
13 }
14 return CACHED_MAP;
15 }
16};
17const addIcons = (icons) => {
18 const map = getIconMap();
19 Object.keys(icons).forEach(name => map.set(name, icons[name]));
20};
21const getUrl = (i) => {
22 let url = getSrc(i.src);
23 if (url) {
24 return url;
25 }
26 url = getName(i.name, i.icon, i.mode, i.ios, i.md);
27 if (url) {
28 return getNamedUrl(url);
29 }
30 if (i.icon) {
31 url = getSrc(i.icon);
32 if (url) {
33 return url;
34 }
35 url = getSrc(i.icon[i.mode]);
36 if (url) {
37 return url;
38 }
39 }
40 return null;
41};
42const getNamedUrl = (iconName) => {
43 const url = getIconMap().get(iconName);
44 if (url) {
45 return url;
46 }
47 return getAssetPath(`svg/${iconName}.svg`);
48};
49const getName = (iconName, icon, mode, ios, md) => {
50 // default to "md" if somehow the mode wasn't set
51 mode = (mode && toLower(mode)) === 'ios' ? 'ios' : 'md';
52 // if an icon was passed in using the ios or md attributes
53 // set the iconName to whatever was passed in
54 if (ios && mode === 'ios') {
55 iconName = toLower(ios);
56 }
57 else if (md && mode === 'md') {
58 iconName = toLower(md);
59 }
60 else {
61 if (!iconName && icon && !isSrc(icon)) {
62 iconName = icon;
63 }
64 if (isStr(iconName)) {
65 iconName = toLower(iconName);
66 }
67 }
68 if (!isStr(iconName) || iconName.trim() === '') {
69 return null;
70 }
71 // only allow alpha characters and dash
72 const invalidChars = iconName.replace(/[a-z]|-|\d/gi, '');
73 if (invalidChars !== '') {
74 return null;
75 }
76 return iconName;
77};
78const getSrc = (src) => {
79 if (isStr(src)) {
80 src = src.trim();
81 if (isSrc(src)) {
82 return src;
83 }
84 }
85 return null;
86};
87const isSrc = (str) => str.length > 0 && /(\/|\.)/.test(str);
88const isStr = (val) => typeof val === 'string';
89const toLower = (val) => val.toLowerCase();
90
91export { addIcons as a, getName as b, getUrl as g, isStr as i };