mirror of
https://github.com/simple-icons/simple-icons.git
synced 2024-12-16 01:10:30 +02:00
Standarize locale comparisons and sort icons by title in package (#7854)
* Use Intl.Collator for locale comparisons * Sort icons by title in package
This commit is contained in:
parent
fdc707bf81
commit
ad7de0434e
@ -3,6 +3,7 @@ import path from 'node:path';
|
||||
import {
|
||||
getDirnameFromImportMeta,
|
||||
htmlFriendlyToTitle,
|
||||
collator,
|
||||
} from './scripts/utils.js';
|
||||
import svgpath from 'svgpath';
|
||||
import svgPathBbox from 'svg-path-bbox';
|
||||
@ -46,7 +47,7 @@ const sortObjectByKey = (obj) => {
|
||||
|
||||
const sortObjectByValue = (obj) => {
|
||||
return Object.keys(obj)
|
||||
.sort((a, b) => ('' + obj[a]).localeCompare(obj[b]))
|
||||
.sort((a, b) => collator.compare(obj[a], obj[b]))
|
||||
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
|
||||
};
|
||||
|
||||
|
@ -18,6 +18,7 @@ import {
|
||||
slugToVariableName,
|
||||
getIconsData,
|
||||
getDirnameFromImportMeta,
|
||||
collator,
|
||||
} from '../utils.js';
|
||||
|
||||
const __dirname = getDirnameFromImportMeta(import.meta.url);
|
||||
@ -98,7 +99,8 @@ const build = async () => {
|
||||
const iconsBarrelJs = [];
|
||||
const iconsBarrelMjs = [];
|
||||
|
||||
buildIcons.forEach(({ icon, iconObject, iconExportName }) => {
|
||||
buildIcons.sort((a, b) => collator.compare(a.icon.title, b.icon.title));
|
||||
buildIcons.forEach(({ iconObject, iconExportName }) => {
|
||||
iconsBarrelDts.push(`export const ${iconExportName}:I;`);
|
||||
iconsBarrelJs.push(`${iconExportName}:${iconObject},`);
|
||||
iconsBarrelMjs.push(`export const ${iconExportName}=${iconObject}`);
|
||||
|
@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
import fakeDiff from 'fake-diff';
|
||||
import { getIconsDataString, normalizeNewlines } from '../utils.js';
|
||||
import { getIconsDataString, normalizeNewlines, collator } from '../utils.js';
|
||||
|
||||
/**
|
||||
* Contains our tests so they can be isolated from each other.
|
||||
@ -18,12 +18,12 @@ const TESTS = {
|
||||
const collector = (invalidEntries, icon, index, array) => {
|
||||
if (index > 0) {
|
||||
const prev = array[index - 1];
|
||||
const compare = icon.title.localeCompare(prev.title);
|
||||
if (compare < 0) {
|
||||
const comparison = collator.compare(icon.title, prev.title);
|
||||
if (comparison < 0) {
|
||||
invalidEntries.push(icon);
|
||||
} else if (compare === 0) {
|
||||
} else if (comparison === 0) {
|
||||
if (prev.slug) {
|
||||
if (!icon.slug || icon.slug.localeCompare(prev.slug) < 0) {
|
||||
if (!icon.slug || collator.compare(icon.slug, prev.slug) < 0) {
|
||||
invalidEntries.push(icon);
|
||||
}
|
||||
}
|
||||
|
@ -153,3 +153,13 @@ export const getThirdPartyExtensions = async (readmePath) =>
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* `Intl.Collator` object ready to be used for icon titles sorting.
|
||||
* @type {Intl.Collator}
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator
|
||||
**/
|
||||
export const collator = new Intl.Collator('en', {
|
||||
usage: 'search',
|
||||
caseFirst: 'upper',
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user