Skip to content

Commit 077168d

Browse files
MasonVXNico Tasche
and
Nico Tasche
authored
fix(icon): icon names with numbers are correctly converted to kebab case (#1339)
resolves #1338 --------- Co-authored-by: Nico Tasche <[email protected]>
1 parent de8b11e commit 077168d

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

Diff for: src/components/icon/test/utils.spec.ts

+39-29
Original file line numberDiff line numberDiff line change
@@ -86,78 +86,88 @@ describe('getName', () => {
8686
describe('addIcons', () => {
8787
it('should add an svg to the icon cache', () => {
8888
const testData = 'stubbed data';
89-
89+
9090
expect(getIconMap().get('logo-ionic')).toEqual(undefined);
91-
91+
9292
addIcons({ 'logo-ionic': 'stubbed data' });
93-
93+
9494
expect(getIconMap().get('logo-ionic')).toEqual(testData);
9595
});
96-
96+
9797
it('should add kebab and camel case names to the icon cache', () => {
9898
const logoIonitron = 'stubbed data';
99-
99+
100100
expect(getIconMap().get('logo-ionitron')).toEqual(undefined);
101101
expect(getIconMap().get('logoIonitron')).toEqual(undefined);
102-
102+
103103
addIcons({ logoIonitron });
104-
104+
105105
expect(getIconMap().get('logo-ionitron')).toEqual(logoIonitron);
106106
expect(getIconMap().get('logoIonitron')).toEqual(logoIonitron);
107+
108+
const logoIonitron0 = 'stubbed data 0'
109+
110+
expect(getIconMap().get('logo-ionitron-0')).toEqual(undefined);
111+
expect(getIconMap().get('logoIonitron0')).toEqual(undefined);
112+
113+
addIcons({ logoIonitron0 });
114+
115+
expect(getIconMap().get('logo-ionitron-0')).toEqual(logoIonitron0);
116+
expect(getIconMap().get('logoIonitron0')).toEqual(logoIonitron0);
107117
});
108-
118+
109119
it('should map to a name that does not match the svg', () => {
110120
const logoIonitron = 'stubbed data';
111-
121+
112122
expect(getIconMap().get('my-fun-icon')).toEqual(undefined);
113-
123+
114124
addIcons({ 'my-fun-icon': logoIonitron });
115-
125+
116126
expect(getIconMap().get('my-fun-icon')).toEqual(logoIonitron);
117127
});
118-
128+
119129
it('should map to an explicit camel case name', () => {
120130
const logoIonitron = 'stubbed data';
121-
131+
122132
expect(getIconMap().get('myCoolIcon')).toEqual(undefined);
123-
133+
124134
addIcons({ 'myCoolIcon': logoIonitron });
125-
135+
126136
expect(getIconMap().get('myCoolIcon')).toEqual(logoIonitron);
127137
});
128-
138+
129139
it('should not warn when mapping the same icon twice', () => {
130140
const spy = jest.spyOn(console, 'warn');
131141

132142
const myIcon = 'my-icon';
133-
143+
134144
expect(spy).not.toHaveBeenCalled();
135-
145+
136146
addIcons({ myIcon });
137-
147+
138148
expect(spy).not.toHaveBeenCalled();
139-
149+
140150
addIcons({ myIcon });
141-
151+
142152
expect(spy).not.toHaveBeenCalled();
143153
});
144-
154+
145155
it('should not overwrite icons', () => {
146156
const spy = jest.spyOn(console, 'warn');
147-
157+
148158
const logoA = 'logo a';
149159
const logoB = 'logo b';
150-
160+
151161
expect(spy).not.toHaveBeenCalled();
152-
162+
153163
expect(getIconMap().get('logo-a')).toEqual(undefined);
154-
164+
155165
addIcons({ 'logo-a': logoB, logoA });
156-
166+
157167
expect(getIconMap().get('logo-a')).toEqual(logoB);
158168
expect(getIconMap().get('logoA')).toEqual(logoA);
159-
169+
160170
expect(spy).toHaveBeenCalled();
161171
});
162-
172+
163173
});

Diff for: src/components/icon/utils.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export const getIconMap = (): Map<string, string> => {
1818
};
1919

2020
export const addIcons = (icons: { [name: string]: string; }) => {
21-
Object.keys(icons).forEach(name => {
21+
Object.keys(icons).forEach(name => {
2222
addToIconMap(name, icons[name]);
23-
23+
2424
/**
2525
* Developers can also pass in the SVG object directly
2626
* and Ionicons can map the object to a kebab case name.
@@ -31,7 +31,7 @@ export const addIcons = (icons: { [name: string]: string; }) => {
3131
* Using name="addCircleOutline" is valid too, but the
3232
* kebab case naming is preferred.
3333
*/
34-
const toKebabCase = name.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
34+
const toKebabCase = name.replace(/([a-z0-9]|(?=[A-Z]))([A-Z0-9])/g, "$1-$2").toLowerCase();
3535
if (name !== toKebabCase) {
3636
addToIconMap(toKebabCase, icons[name]);
3737
}
@@ -40,12 +40,12 @@ export const addIcons = (icons: { [name: string]: string; }) => {
4040

4141
const addToIconMap = (name: string, data: any) => {
4242
const map = getIconMap();
43-
43+
4444
const existingIcon = map.get(name);
4545

4646
if (existingIcon === undefined) {
4747
map.set(name, data);
48-
48+
4949
/**
5050
* Importing and defining the same icon reference
5151
* multiple times should not yield a warning.

0 commit comments

Comments
 (0)