@@ -2,6 +2,12 @@ import { upperFirst } from 'lodash-es'
22import { getAllLocales } from '@/plugins/i18n'
33
44const stringFormatters = ( ) => {
5+ const byteUnits = [ ' kB' , ' MB' , ' GB' , ' TB' , 'PB' , 'EB' , 'ZB' , 'YB' ]
6+
7+ const frequencyUnits = [ ' Hz' , ' kHz' , ' MHz' , ' GHz' , ' THz' ]
8+
9+ const resistanceUnits = [ ' Ω' , ' kΩ' , ' MΩ' , ' GΩ' , ' TΩ' ]
10+
511 const instance = {
612 prettyCase : ( value : string ) => {
713 return value
@@ -11,101 +17,110 @@ const stringFormatters = () => {
1117 . join ( ' ' )
1218 } ,
1319
20+ getStringValueWithUnit : ( value : number , fractionDigits : number , unit : string ) => {
21+ if ( value === 0 ) {
22+ return `0${ unit } `
23+ }
24+
25+ return Math . max ( value , Math . pow ( 10 , - fractionDigits ) )
26+ . toFixed ( fractionDigits ) + unit
27+ } ,
28+
1429 /**
15- * Splits a string into an array of strings, removing quotes.
16- */
30+ * Splits a string into an array of strings, removing quotes.
31+ */
1732 getStringArray : ( value : string , separator = ';' ) => {
1833 return value . split ( separator )
1934 . map ( x => x . replace ( / ^ " | " $ / g, '' ) )
2035 } ,
2136
2237 /**
23- * Formats a number (in bytes) to a human readable file size.
24- */
38+ * Formats a number (in bytes) to a human readable file size.
39+ */
2540 getReadableFileSizeString : ( fileSizeInBytes : number , fractionDigits = 1 ) => {
2641 let i = - 1
27- const byteUnits = [ ' kB' , ' MB' , ' GB' , ' TB' , 'PB' , 'EB' , 'ZB' , 'YB' ]
28- if ( fileSizeInBytes === 0 ) return `0${ byteUnits [ 0 ] } `
2942 do {
3043 fileSizeInBytes = fileSizeInBytes / 1024
3144 i ++
3245 } while ( fileSizeInBytes > 1024 )
3346
34- return Math . max ( fileSizeInBytes , 0.1 ) . toFixed ( fractionDigits ) + byteUnits [ i ]
47+ return instance . getStringValueWithUnit ( fileSizeInBytes , fractionDigits , byteUnits [ i ] )
3548 } ,
3649
3750 /**
38- * Formats a number (in bytes/sec) to a human readable data rate.
39- */
51+ * Formats a number (in bytes/sec) to a human readable data rate.
52+ */
4053 getReadableDataRateString : ( dataRateInBytesPerSec : number , fractionDigits = 1 ) => {
4154 let i = - 1
42- const byteUnits = [ ' kB' , ' MB' , ' GB' , ' TB' , 'PB' , 'EB' , 'ZB' , 'YB' ]
43- if ( dataRateInBytesPerSec === 0 ) return `0${ byteUnits [ 0 ] } `
4455 do {
4556 dataRateInBytesPerSec = dataRateInBytesPerSec / 1024
4657 i ++
4758 } while ( dataRateInBytesPerSec > 1024 )
4859
49- return Math . max ( dataRateInBytesPerSec , 0.2 ) . toFixed ( fractionDigits ) + byteUnits [ i ] + '/Sec'
60+ return instance . getStringValueWithUnit ( dataRateInBytesPerSec , fractionDigits , byteUnits [ i ] + '/Sec' )
5061 } ,
5162
5263 /**
53- * Formats a number representing mm to human readable distance.
54- */
55- getReadableLengthString : ( lengthInMm : number | undefined | null , showMicrons = false ) => {
56- if ( lengthInMm === undefined || lengthInMm === null ) {
57- return '- '
64+ * Formats a number representing mm to human readable distance.
65+ */
66+ getReadableLengthString : ( lengthInMm : number , showMicrons = false , fractionDigits : number | undefined = undefined ) => {
67+ if ( lengthInMm >= 1000 ) {
68+ return ( lengthInMm / 1000 ) . toFixed ( fractionDigits ?? 2 ) + ' m '
5869 }
5970
60- if ( lengthInMm >= 1000 ) return ( lengthInMm / 1000 ) . toFixed ( 2 ) + ' m'
61- if ( lengthInMm > 100 ) return ( lengthInMm / 10 ) . toFixed ( 1 ) + ' cm'
62- if ( lengthInMm < 0.1 && showMicrons ) return ( lengthInMm * 1000 ) . toFixed ( 0 ) + ' μm'
63- return lengthInMm . toFixed ( 1 ) + ' mm'
71+ if ( lengthInMm > 100 ) {
72+ return ( lengthInMm / 10 ) . toFixed ( fractionDigits ?? 1 ) + ' cm'
73+ }
74+
75+ if ( lengthInMm < 0.1 && showMicrons ) {
76+ return instance . getStringValueWithUnit ( lengthInMm * 1000 , fractionDigits ?? 0 , ' μm' )
77+ }
78+
79+ return instance . getStringValueWithUnit ( lengthInMm , fractionDigits ?? 1 , ' mm' )
6480 } ,
6581
6682 /**
67- * Formats a number representing g to human readable weight.
68- */
69- getReadableWeightString : ( weightInG : number | undefined | null , fractionDigits = 2 ) => {
70- if ( weightInG === undefined || weightInG === null ) {
71- return '- '
83+ * Formats a number representing g to human readable weight.
84+ */
85+ getReadableWeightString : ( weightInG : number , fractionDigits = 2 ) => {
86+ if ( weightInG >= 1000 ) {
87+ return ( weightInG / 1000 ) . toFixed ( fractionDigits ) + ' kg '
7288 }
7389
74- if ( weightInG >= 1000 ) return ( weightInG / 1000 ) . toFixed ( fractionDigits ) + ' kg'
75- return weightInG . toFixed ( fractionDigits ) + ' g'
90+ return instance . getStringValueWithUnit ( weightInG , fractionDigits , ' g' )
7691 } ,
7792
7893 /**
79- * Formats a number (in Hz) to a human readable frequency.
80- */
94+ * Formats a number (in Hz) to a human readable frequency.
95+ */
8196 getReadableFrequencyString : ( frequencyInHz : number , fractionDigits = 0 ) => {
8297 let i = 0
83- const frequencyUnits = [ ' Hz' , ' kHz' , ' MHz' , ' GHz' , ' THz' ]
8498 while ( frequencyInHz >= 1000 ) {
8599 frequencyInHz = frequencyInHz / 1000
86100 i ++
87101 }
88- return frequencyInHz . toFixed ( fractionDigits ) + frequencyUnits [ i ]
102+
103+ return instance . getStringValueWithUnit ( frequencyInHz , fractionDigits , frequencyUnits [ i ] )
89104 } ,
90105
91106 /**
92- * Formats a number (in ohms) to a human readable resistance.
93- */
107+ * Formats a number (in ohms) to a human readable resistance.
108+ */
94109 getReadableResistanceString : ( resistanceInOhms : number , fractionDigits = 1 ) => {
95110 let i = 0
96- const resistanceUnits = [ ' Ω' , ' kΩ' , ' MΩ' , ' GΩ' , ' TΩ' ]
97111 while ( resistanceInOhms >= 1000 ) {
98112 resistanceInOhms = resistanceInOhms / 1000
99113 i ++
100114 }
101- return resistanceInOhms . toFixed ( fractionDigits ) + resistanceUnits [ i ]
115+
116+ return instance . getStringValueWithUnit ( resistanceInOhms , fractionDigits , resistanceUnits [ i ] )
102117 } ,
103118
104119 /**
105- * Formats a number (in hPa) to human readable atmospheric pressure.
106- */
120+ * Formats a number (in hPa) to human readable atmospheric pressure.
121+ */
107122 getReadableAtmosphericPressureString : ( pressumeInHPa : number , fractionDigits = 1 ) => {
108- return pressumeInHPa . toFixed ( fractionDigits ) + ' hPa'
123+ return instance . getStringValueWithUnit ( pressumeInHPa , fractionDigits , ' hPa' )
109124 } ,
110125
111126 getReadableCurrencyString : ( value : number , currency : string , options ?: Intl . NumberFormatOptions ) => {
0 commit comments