-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhelpers.ts
More file actions
119 lines (106 loc) Β· 3.56 KB
/
helpers.ts
File metadata and controls
119 lines (106 loc) Β· 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { default_value, translation_value_limit } from '../utils/micro';
import { LanguageMapping } from './languages';
import { TranslationModule, TranslationModules } from './modules';
// Define the enum for non-safe types
enum nonSafeTypes {
long,
undefined,
null,
empty,
}
// Define the interface for ValueSafety
interface ValueSafety {
is_safe: boolean;
type: nonSafeTypes | undefined;
}
/**
* Checks if a value is safe according to the defined criteria.
* @param value The string value to check.
* @returns An object indicating whether the value is safe and its type if not safe.
*/
function valueIsSafe(value: string): ValueSafety {
if (value === undefined) return { is_safe: false, type: nonSafeTypes.undefined };
if (value === null) return { is_safe: false, type: nonSafeTypes.null };
if (value.length >= translation_value_limit) return { is_safe: false, type: nonSafeTypes.long };
if (value === '') return { is_safe: false, type: nonSafeTypes.empty };
return { is_safe: true, type: undefined };
}
/**
* Transitions a value to a safe value if it does not meet the safety criteria.
* @param value The string value to transition.
* @returns The safe value.
*/
export function safeValueTransition(value: string): string {
const valueSafety: ValueSafety = valueIsSafe(value);
if (valueSafety.is_safe) {
return value;
}
switch (valueSafety.type) {
case nonSafeTypes.null:
case nonSafeTypes.undefined:
case nonSafeTypes.empty:
return default_value;
case nonSafeTypes.long:
return value.substring(0, translation_value_limit);
default:
throw new Error(`Unknown non-safe type: ${valueSafety.type}`);
}
}
/**
* Returns an array of keys from the TranslationModules object.
* @returns An array of keys.
*/
export function translationModuleKeys(): string[] {
return Object.keys(TranslationModules);
}
/**
* Retrieves a TranslationModule by its key.
* @param key The key of the TranslationModule.
* @returns The TranslationModule if found, otherwise undefined.
*/
export function getTranslationModuleByKey(key: string): TranslationModule | undefined {
return TranslationModules[key];
}
/**
* Finds the language key from a given value in the languages object.
* @param value The value to find.
* @param languages The object containing language mappings.
* @returns The language key if found, otherwise undefined.
*/
export function getLanguageKeyFromValue(
value: string,
languages: Record<string, string>
): string | undefined {
return Object.keys(languages).find(key => languages[key] === value);
}
/**
* Returns an array of language values from the languages object.
* @param languages The object containing language mappings.
* @returns An array of language values.
*/
export function getLanguageValues(languages: Record<string, string>): string[] {
return Object.values(languages);
}
/**
* Finds the language variant for a given source and destination.
* @param source The source language.
* @param sourceValue The value in the source language.
* @param destination The destination language.
* @returns The translated value if found, otherwise undefined.
*/
export function getLanguageVariant(
source: string,
sourceValue: string,
destination: string
): string | undefined {
for (const key of Object.keys(LanguageMapping)) {
if (
LanguageMapping[key].includes(source) &&
getTranslationModuleByKey(source)?.languages[key] === sourceValue &&
LanguageMapping[key].includes(destination)
) {
return getTranslationModuleByKey(destination)?.languages[key];
}
}
return undefined;
}