1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-12-16 01:10:30 +02:00

Show expected order hint on icons order lint (#12336)

This commit is contained in:
LitoMore 2024-12-14 00:14:20 +08:00 committed by GitHub
parent f66ea43199
commit 8c34c4967b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,11 +71,33 @@ const TESTS = {
return icon.title; return icon.title;
}; };
/**
* Find the expected position of an icon.
* @param {IconData[]} expectedOrder Expected order of the icons.
* @param {IconData} targetIcon Icon to find.
* @returns {string} Expected position of the icon.
*/
const findPositon = (expectedOrder, targetIcon) => {
const foundIndex = expectedOrder.findIndex(
(icon) =>
targetIcon.title === icon.title && targetIcon.slug === icon.slug,
);
const before = expectedOrder[foundIndex - 1];
const after = expectedOrder[foundIndex + 1];
if (before) return `should be after ${format(before)}`;
if (after) return `should be before ${format(after)}`;
return 'not found';
};
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/no-array-callback-reference // eslint-disable-next-line unicorn/no-array-reduce, unicorn/no-array-callback-reference
const invalids = icons.reduce(collector, []); const invalids = icons.reduce(collector, []);
if (invalids.length > 0) { if (invalids.length > 0) {
const expectedOrder = [...icons].sort((a, b) =>
collator.compare(a.title, b.title),
);
return `Some icons aren't in alphabetical order: return `Some icons aren't in alphabetical order:
${invalids.map((icon) => format(icon)).join(', ')}`; ${invalids.map((icon) => `${format(icon)} ${findPositon(expectedOrder, icon)}`).join('\n')}`;
} }
}, },