@@ -8,7 +8,7 @@ export class Parser {
88 * @param separator The separator to use
99 * @return The array of string
1010 */
11- stringToArray ( string : string , separator : string ) : string [ ] {
11+ static stringToArray ( string : string , separator : string ) : string [ ] {
1212 return string . split ( separator ) . map ( index => index . trim ( ) )
1313 }
1414
@@ -19,7 +19,7 @@ export class Parser {
1919 * @param isCoordinate If string is a coordinate
2020 * @return The string parsed to int or float
2121 */
22- stringToNumber ( string : string , isCoordinate = false ) : number {
22+ static stringToNumber ( string : string , isCoordinate = false ) : number {
2323 if ( ! string . replace ( / \D / g, '' ) ) {
2424 throw new InternalServerException (
2525 'Your string is invalid, it should have at least one number.' ,
@@ -41,7 +41,7 @@ export class Parser {
4141 * @param object The object to parse
4242 * @return The object parsed to form data
4343 */
44- jsonToFormData ( object : any ) : string {
44+ static jsonToFormData ( object : any ) : string {
4545 return Object . keys ( object )
4646 . reduce ( ( previous , current ) => {
4747 return previous + `&${ current } =${ encodeURIComponent ( object [ current ] ) } `
@@ -55,7 +55,7 @@ export class Parser {
5555 * @param formData The form data to parse
5656 * @return The form data parsed to object
5757 */
58- formDataToJson ( formData : string ) : any {
58+ static formDataToJson ( formData : string ) : any {
5959 const object = { }
6060
6161 if ( formData . startsWith ( '?' ) ) formData = formData . replace ( '?' , '' )
@@ -68,4 +68,35 @@ export class Parser {
6868
6969 return object
7070 }
71+
72+ /**
73+ * bytesToSize creates a string based on the bytes size
74+ *
75+ * @param bytes - The number of bytes
76+ * @param decimals - The number of decimals to be showed
77+ * @return formattedSize - Return the formatted value based on the size (100 MB, 1 GB, etc)
78+ */
79+ static bytesToSize ( bytes : number , decimals = 2 ) {
80+ if ( bytes === 0 ) return '0 Bytes'
81+
82+ const k = 1024
83+ const dm = decimals < 0 ? 0 : decimals
84+ const sizes = [ 'Bytes' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ]
85+
86+ const i = Math . floor ( Math . log ( bytes ) / Math . log ( k ) )
87+
88+ return parseFloat ( ( bytes / Math . pow ( k , i ) ) . toFixed ( dm ) ) + ' ' + sizes [ i ]
89+ }
90+
91+ /**
92+ * linkToHref parses all links inside the string to HTML link with <a href= .../>
93+ *
94+ * @param string - The string with links inside
95+ * @return formattedString - Return the formatted string
96+ */
97+ static linkToHref ( string : any ) : string {
98+ const regex = / ( h t t p s ? : \/ \/ [ ^ \s ] + ) / g
99+
100+ return string . replace ( regex , '<a href="$1">$1</a>' )
101+ }
71102}
0 commit comments