|
1 | 1 | import type _Vue from 'vue' |
2 | 2 | import { isNavigationFailure, NavigationFailureType, type RawLocation } from 'vue-router' |
3 | | -import { upperFirst } from 'lodash-es' |
4 | 3 | import type { ApiConfig } from '@/store/config/types' |
5 | 4 | import { TinyColor } from '@ctrl/tinycolor' |
6 | 5 | import { Globals, Waits } from '@/globals' |
7 | 6 | import i18n from '@/plugins/i18n' |
8 | 7 | import type { TranslateResult } from 'vue-i18n' |
9 | 8 | import store from '@/store' |
10 | 9 | import router from '@/router' |
11 | | -import dateTimeFormatter from '@/util/date-time-formatter' |
| 10 | +import * as dateTimeFormatters from '@/util/date-time-formatters' |
| 11 | +import * as stringFormatters from '@/util/string-formatters' |
12 | 12 | import consola from 'consola' |
13 | 13 |
|
14 | 14 | const Filters = { |
15 | | - getNavigatorLocales: () => { |
16 | | - return navigator.languages ?? [navigator.language] |
17 | | - }, |
18 | | - |
19 | | - getAllLocales: (): Intl.LocalesArgument => { |
20 | | - return [ |
21 | | - i18n.locale, |
22 | | - ...Filters.getNavigatorLocales() |
23 | | - ] |
24 | | - }, |
25 | | - |
26 | | - prettyCase: (value: string) => { |
27 | | - return value |
28 | | - .split(/[ _]/g) |
29 | | - .filter(x => x) |
30 | | - .map(upperFirst) |
31 | | - .join(' ') |
32 | | - }, |
33 | | - |
34 | | - /** |
35 | | - * Formats a number (in bytes) to a human readable file size. |
36 | | - */ |
37 | | - getReadableFileSizeString (fileSizeInBytes: number, fractionDigits = 1) { |
38 | | - let i = -1 |
39 | | - const byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'] |
40 | | - if (fileSizeInBytes === 0) return `0${byteUnits[0]}` |
41 | | - do { |
42 | | - fileSizeInBytes = fileSizeInBytes / 1024 |
43 | | - i++ |
44 | | - } while (fileSizeInBytes > 1024) |
45 | | - |
46 | | - return Math.max(fileSizeInBytes, 0.1).toFixed(fractionDigits) + byteUnits[i] |
47 | | - }, |
48 | | - |
49 | | - /** |
50 | | - * Formats a number (in bytes/sec) to a human readable data rate. |
51 | | - */ |
52 | | - getReadableDataRateString (dataRateInBytesPerSec: number, fractionDigits = 1) { |
53 | | - let i = -1 |
54 | | - const byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'] |
55 | | - if (dataRateInBytesPerSec === 0) return `0${byteUnits[0]}` |
56 | | - do { |
57 | | - dataRateInBytesPerSec = dataRateInBytesPerSec / 1024 |
58 | | - i++ |
59 | | - } while (dataRateInBytesPerSec > 1024) |
60 | | - |
61 | | - return Math.max(dataRateInBytesPerSec, 0.2).toFixed(fractionDigits) + byteUnits[i] + '/Sec' |
62 | | - }, |
63 | | - |
64 | | - /** |
65 | | - * Formats a number representing mm to human readable distance. |
66 | | - */ |
67 | | - getReadableLengthString (lengthInMm: number | undefined | null, showMicrons = false) { |
68 | | - if (lengthInMm === undefined || lengthInMm === null) { |
69 | | - return '-' |
70 | | - } |
71 | | - |
72 | | - if (lengthInMm >= 1000) return (lengthInMm / 1000).toFixed(2) + ' m' |
73 | | - if (lengthInMm > 100) return (lengthInMm / 10).toFixed(1) + ' cm' |
74 | | - if (lengthInMm < 0.1 && showMicrons) return (lengthInMm * 1000).toFixed(0) + ' μm' |
75 | | - return lengthInMm.toFixed(1) + ' mm' |
76 | | - }, |
77 | | - |
78 | | - /** |
79 | | - * Formats a number representing g to human readable weight. |
80 | | - */ |
81 | | - getReadableWeightString (weightInG: number | undefined | null, fractionDigits = 2) { |
82 | | - if (weightInG === undefined || weightInG === null) { |
83 | | - return '-' |
84 | | - } |
85 | | - |
86 | | - if (weightInG >= 1000) return (weightInG / 1000).toFixed(fractionDigits) + ' kg' |
87 | | - return weightInG.toFixed(fractionDigits) + ' g' |
88 | | - }, |
89 | | - |
90 | | - /** |
91 | | - * Formats a number (in Hz) to a human readable frequency. |
92 | | - */ |
93 | | - getReadableFrequencyString (frequencyInHz: number, fractionDigits = 0) { |
94 | | - let i = 0 |
95 | | - const frequencyUnits = [' Hz', ' kHz', ' MHz', ' GHz', ' THz'] |
96 | | - while (frequencyInHz >= 1000) { |
97 | | - frequencyInHz = frequencyInHz / 1000 |
98 | | - i++ |
99 | | - } |
100 | | - return frequencyInHz.toFixed(fractionDigits) + frequencyUnits[i] |
101 | | - }, |
102 | | - |
103 | | - /** |
104 | | - * Formats a number (in ohms) to a human readable resistance. |
105 | | - */ |
106 | | - getReadableResistanceString (resistanceInOhms: number, fractionDigits = 1) { |
107 | | - let i = 0 |
108 | | - const resistanceUnits = [' Ω', ' kΩ', ' MΩ', ' GΩ', ' TΩ'] |
109 | | - while (resistanceInOhms >= 1000) { |
110 | | - resistanceInOhms = resistanceInOhms / 1000 |
111 | | - i++ |
112 | | - } |
113 | | - return resistanceInOhms.toFixed(fractionDigits) + resistanceUnits[i] |
114 | | - }, |
115 | | - |
116 | | - /** |
117 | | - * Formats a number (in hPa) to human readable atmospheric pressure. |
118 | | - */ |
119 | | - getReadableAtmosphericPressureString (pressumeInHPa: number, fractionDigits = 1) { |
120 | | - return pressumeInHPa.toFixed(fractionDigits) + ' hPa' |
121 | | - }, |
122 | | - |
123 | 15 | /** |
124 | 16 | * Determines API urls from a base url |
125 | 17 | */ |
@@ -181,11 +73,14 @@ const Filters = { |
181 | 73 | return weight / density / (Math.PI * (diameter / 2) ** 2) * 1000 |
182 | 74 | }, |
183 | 75 |
|
184 | | - ...dateTimeFormatter( |
185 | | - (): Intl.LocalesArgument => Filters.getAllLocales(), |
| 76 | + ...stringFormatters, |
| 77 | + |
| 78 | + ...dateTimeFormatters, |
| 79 | + |
| 80 | + ...dateTimeFormatters.buildDateTimeFormatters( |
186 | 81 | () => store.state.config.uiSettings.general.dateFormat, |
187 | 82 | () => store.state.config.uiSettings.general.timeFormat |
188 | | - ) |
| 83 | + ), |
189 | 84 | } |
190 | 85 |
|
191 | 86 | export const Rules = { |
|
0 commit comments