mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 02:49:51 +00:00
Sort strings by locale language (#14533)
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
export const stringCompare = (a: string, b: string) => {
|
||||
import memoizeOne from "memoize-one";
|
||||
|
||||
const collator = memoizeOne(
|
||||
(language: string | undefined) => new Intl.Collator(language)
|
||||
);
|
||||
|
||||
const caseInsensitiveCollator = memoizeOne(
|
||||
(language: string | undefined) =>
|
||||
new Intl.Collator(language, { sensitivity: "accent" })
|
||||
);
|
||||
|
||||
const fallbackStringCompare = (a: string, b: string) => {
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
@@ -9,5 +20,28 @@ export const stringCompare = (a: string, b: string) => {
|
||||
return 0;
|
||||
};
|
||||
|
||||
export const caseInsensitiveStringCompare = (a: string, b: string) =>
|
||||
stringCompare(a.toLowerCase(), b.toLowerCase());
|
||||
export const stringCompare = (
|
||||
a: string,
|
||||
b: string,
|
||||
language: string | undefined = undefined
|
||||
) => {
|
||||
// @ts-ignore
|
||||
if (Intl?.Collator) {
|
||||
return collator(language).compare(a, b);
|
||||
}
|
||||
|
||||
return fallbackStringCompare(a, b);
|
||||
};
|
||||
|
||||
export const caseInsensitiveStringCompare = (
|
||||
a: string,
|
||||
b: string,
|
||||
language: string | undefined = undefined
|
||||
) => {
|
||||
// @ts-ignore
|
||||
if (Intl?.Collator) {
|
||||
return caseInsensitiveCollator(language).compare(a, b);
|
||||
}
|
||||
|
||||
return fallbackStringCompare(a.toLowerCase(), b.toLowerCase());
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user