You've already forked simple-icons
mirror of
https://github.com/simple-icons/simple-icons.git
synced 2025-07-12 22:21:23 +02:00
Sort icon data object keys (#12795)
This commit is contained in:
@ -81,3 +81,88 @@ export const sortIconsCompare = (a, b) => {
|
||||
? collator.compare(getIconSlug(a), getIconSlug(b))
|
||||
: collator.compare(a.title, b.title);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sort icon data obeject.
|
||||
* @param {IconData} icon The icon data as it appears in *_data/simple-icons.json*.
|
||||
* @returns {IconData} The sorted icon data.
|
||||
*/
|
||||
const sortIcon = (icon) => {
|
||||
const keyOrder = [
|
||||
'title',
|
||||
'slug',
|
||||
'hex',
|
||||
'source',
|
||||
'guidelines',
|
||||
'license',
|
||||
'aliases',
|
||||
// This is not appears in icon data but it's in the alias object.
|
||||
'loc',
|
||||
];
|
||||
const sortedIcon = Object.fromEntries(
|
||||
Object.entries(icon).sort(
|
||||
([key1], [key2]) => keyOrder.indexOf(key1) - keyOrder.indexOf(key2),
|
||||
),
|
||||
);
|
||||
return sortedIcon;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sort license object.
|
||||
* @param {IconData['license']} license The license object as it appears in *_data/simple-icons.json*.
|
||||
* @returns {IconData['license'] | undefined} The sorted license object.
|
||||
*/
|
||||
const sortLicense = (license) => {
|
||||
if (!license) return undefined;
|
||||
const keyOrder = ['type', 'url'];
|
||||
const sortedLicense = Object.fromEntries(
|
||||
Object.entries(license).sort(
|
||||
([key1], [key2]) => keyOrder.indexOf(key1) - keyOrder.indexOf(key2),
|
||||
),
|
||||
);
|
||||
return sortedLicense;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sort object key alphabetically.
|
||||
* @param {IconData['aliases']} object The aliases object as it appears in *_data/simple-icons.json*.
|
||||
* @returns {IconData['aliases'] | undefined} The sorted aliases object.
|
||||
*/
|
||||
const sortAlphabetically = (object) => {
|
||||
if (!object) return undefined;
|
||||
const sorted = Object.fromEntries(
|
||||
Object.entries(object).sort(([key1], [key2]) => (key1 > key2 ? 1 : -1)),
|
||||
);
|
||||
return sorted;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sort icons data.
|
||||
* @param {IconData[]} iconsData The icons data as it appears in *_data/simple-icons.json*.
|
||||
* @returns {IconData[]} The sorted icons data.
|
||||
*/
|
||||
export const formatIconData = (iconsData) => {
|
||||
const mappedIcons = iconsData.map((icon) => {
|
||||
return sortIcon({
|
||||
...icon,
|
||||
license: sortLicense(icon.license),
|
||||
aliases: icon.aliases
|
||||
? sortAlphabetically({
|
||||
aka: icon.aliases.aka?.sort(collator.compare),
|
||||
dup: icon.aliases.dup
|
||||
? icon.aliases.dup.sort(sortIconsCompare).map((d) =>
|
||||
sortIcon({
|
||||
...d,
|
||||
loc: sortAlphabetically(d.loc),
|
||||
}),
|
||||
)
|
||||
: undefined,
|
||||
loc: sortAlphabetically(icon.aliases.loc),
|
||||
old: icon.aliases.old?.sort(collator.compare),
|
||||
})
|
||||
: undefined,
|
||||
});
|
||||
});
|
||||
const sortedIcons = [...mappedIcons].sort(sortIconsCompare);
|
||||
return sortedIcons;
|
||||
};
|
||||
|
Reference in New Issue
Block a user