@messagevisor/catalog 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ import type { DuplicateTranslationValue } from "../types";
2
+
3
+ export type DuplicateValuesSortColumn = "value" | "messages";
4
+ export type SortDirection = "asc" | "desc";
5
+
6
+ export interface DuplicateValuesSort {
7
+ column: DuplicateValuesSortColumn;
8
+ direction: SortDirection;
9
+ }
10
+
11
+ export function sortDuplicateValues(
12
+ duplicateValues: DuplicateTranslationValue[],
13
+ sort: DuplicateValuesSort,
14
+ ): DuplicateTranslationValue[] {
15
+ return duplicateValues.slice().sort((left, right) => {
16
+ const direction = sort.direction === "asc" ? 1 : -1;
17
+ const fallback = left.value.localeCompare(right.value);
18
+
19
+ if (sort.column === "messages") {
20
+ const result = left.messageKeys.length - right.messageKeys.length;
21
+ return (result || fallback) * direction;
22
+ }
23
+
24
+ return (
25
+ (left.value.localeCompare(right.value) ||
26
+ left.messageKeys.length - right.messageKeys.length) * direction
27
+ );
28
+ });
29
+ }
30
+
31
+ export function getNextDuplicateValuesSort(
32
+ current: DuplicateValuesSort,
33
+ column: DuplicateValuesSortColumn,
34
+ ): DuplicateValuesSort {
35
+ if (current.column !== column) {
36
+ return {
37
+ column,
38
+ direction: column === "messages" ? "desc" : "asc",
39
+ };
40
+ }
41
+
42
+ return {
43
+ column,
44
+ direction: current.direction === "asc" ? "desc" : "asc",
45
+ };
46
+ }