-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathstrings.ts
118 lines (99 loc) · 3.37 KB
/
strings.ts
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
import { round } from 'lodash';
import { mmToKm, mmToM } from './physics';
import type { Unit } from './types';
export const NARROW_NO_BREAK_SPACE = '\u202f';
export const NO_BREAK_SPACE = '\xa0';
export function conditionalStringConcat<Condition>(
elements: [Condition, string][],
separator = ', '
) {
function elementString([element, name]: [Condition, string]) {
return element ? [name] : [];
}
return elements.reduce<string[]>((acc, el) => [...acc, ...elementString(el)], []).join(separator);
}
export function formatKmValue(value: number, unit: Unit = 'meters', digits = 3) {
let divider = 1000;
if (unit === 'millimeters') {
divider = 1000000;
}
return `${(value / divider).toFixed(digits)}${NO_BREAK_SPACE}km`;
}
export function language2flag(lng: string) {
switch (lng) {
case 'en':
return 'gb';
case 'uk':
return 'ua';
default:
return lng;
}
}
export function snakeToCamel(str: string) {
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr: string) => chr.toUpperCase());
}
export function getTranslationKey(translationList: string | undefined, item: string): string {
return `${translationList ? `${translationList}.` : ''}${item}`;
}
export function geti18nKeyForNull(str: string | null): string {
return str || `N/C`;
}
/** Given a string, return a number or undefined */
export function parseNumber(str: string) {
return str && !Number.isNaN(Number(str)) ? Number(str) : undefined;
}
/**
* Given a string, return a number or 0
* Useful for number input
*/
export function convertInputStringToNumber(str: string) {
return parseNumber(str) || 0;
}
/**
* Given an UIC number, check if it begins with 87,
* if true return the UIC without the 87,
* if false return the full UIC
* @param uic full UIC and CI code (8 digits)
*/
export function formatUicToCi(uic: number) {
return uic.toString().replace(/^87/, '');
}
/**
* Normalizes a string by removing accents and converting to lowercase.
*
* Accent removal is done using `String.normalize` which converts accented characters to their
* decomposed form (e.g. é -> e + ´). We then remove all characters in the range of U+0300 to U+036f
* which contains all unicode combining diacritical marks (https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
*/
export function normalized(s: string): string {
return s
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
}
export const TEXT_AREA_MAX_LENGTH = 4096;
export const SMALL_TEXT_AREA_MAX_LENGTH = 1024;
export const TEXT_INPUT_MAX_LENGTH = 255;
export const SMALL_INPUT_MAX_LENGTH = 128;
export const isInvalidString = (maxLengthAuthorized: number, field?: string | null): boolean =>
!!field && field.length > maxLengthAuthorized;
/**
* Given a distance in millimeters, returns a human readable string.
* Examples:
* - 927520 -> 927m
* - 1927520 -> 1.92Km
*
* @param distance the distance in millimeters
* @returns A human readable string of the distance
*/
export function distanceToHumanReadable(distance: number): string {
if (!distance) return '0';
const kms = mmToKm(distance);
if (Math.floor(kms) > 0) return `${round(kms, 2)}km`;
const meters = mmToM(distance);
if (Math.floor(meters) > 0) return `${round(meters, 0)}m`;
return `${distance}mm`;
}
export function capitalizeFirstLetter(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
}