|
| 1 | +// Copyright (C) 2024 Nethesis S.r.l. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +import { round } from 'lodash-es' |
| 5 | +import { nextTick } from 'vue' |
| 6 | + |
| 7 | +/** |
| 8 | + * Sort function to order array elements by a specific property (for array of objects) or by a specific index (for arrays of arrays) |
| 9 | + * |
| 10 | + */ |
| 11 | +export const sortByProperty = (property: string | number) => { |
| 12 | + return function (a: any, b: any) { |
| 13 | + if (a[property] < b[property]) { |
| 14 | + return -1 |
| 15 | + } |
| 16 | + if (a[property] > b[property]) { |
| 17 | + return 1 |
| 18 | + } |
| 19 | + return 0 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Set the focus on an element. To set the focus on a custom component, it needs to expose focus() function (see NeTextInput for an example) |
| 25 | + * |
| 26 | + */ |
| 27 | +export const focusElement = (elementRef: any) => { |
| 28 | + nextTick(() => { |
| 29 | + if (elementRef && elementRef.value) { |
| 30 | + elementRef.value.focus() |
| 31 | + } |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Returns a i18n key for an error returned by Axios |
| 37 | + * |
| 38 | + */ |
| 39 | +export const getAxiosErrorMessage = (error: any) => { |
| 40 | + if (error.message === 'Network Error') { |
| 41 | + return 'error.network_error' |
| 42 | + } |
| 43 | + |
| 44 | + if (/^timeout of .+ exceeded$/.test(error.message)) { |
| 45 | + // axios timeout reached |
| 46 | + return 'error.network_timeout' |
| 47 | + } |
| 48 | + |
| 49 | + if (error.response) { |
| 50 | + switch (error.response.status) { |
| 51 | + case 401: |
| 52 | + return 'error.http_401' |
| 53 | + case 403: |
| 54 | + return 'error.http_403' |
| 55 | + case 404: |
| 56 | + return 'error.http_404' |
| 57 | + case 500: |
| 58 | + return 'error.http_500' |
| 59 | + } |
| 60 | + } |
| 61 | + return 'error.generic_error' |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Format a byte size according to the International Electrotechnical Commission (IEC), using 1024 as multiple factor |
| 66 | + * |
| 67 | + * @param byteSize the number of bytes to format |
| 68 | + * @returns a string representing the byte size with the appropriate unit |
| 69 | + */ |
| 70 | +export const byteFormat1024 = (byteSize: number) => { |
| 71 | + switch (true) { |
| 72 | + case isNaN(byteSize) || (!byteSize && byteSize !== 0): |
| 73 | + return '-' |
| 74 | + case byteSize >= 0 && byteSize < 1024: |
| 75 | + return byteSize + ' B' |
| 76 | + case byteSize >= 1024 && byteSize < Math.pow(1024, 2): |
| 77 | + return round(byteSize / 1024, 2) + ' KiB' |
| 78 | + case byteSize >= Math.pow(1024, 2) && byteSize < Math.pow(1024, 3): |
| 79 | + return round(byteSize / Math.pow(1024, 2), 2) + ' MiB' |
| 80 | + case byteSize >= Math.pow(1024, 3) && byteSize < Math.pow(1024, 4): |
| 81 | + return round(byteSize / Math.pow(1024, 3), 2) + ' GiB' |
| 82 | + default: |
| 83 | + return round(byteSize / Math.pow(1024, 4), 2) + ' TiB' |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Format a byte size according to the International System of Units (SI), using 1000 as multiple factor |
| 89 | + * |
| 90 | + * @param byteSize the number of bytes to format |
| 91 | + * @returns a string representing the byte size with the appropriate unit |
| 92 | + */ |
| 93 | +export const byteFormat1000 = (byteSize: number) => { |
| 94 | + switch (true) { |
| 95 | + case isNaN(byteSize) || (!byteSize && byteSize !== 0): |
| 96 | + return '-' |
| 97 | + case byteSize >= 0 && byteSize < 1000: |
| 98 | + return byteSize + ' B' |
| 99 | + case byteSize >= 1000 && byteSize < Math.pow(1000, 2): |
| 100 | + return round(byteSize / 1000, 2) + ' kB' |
| 101 | + case byteSize >= Math.pow(1000, 2) && byteSize < Math.pow(1000, 3): |
| 102 | + return round(byteSize / Math.pow(1000, 2), 2) + ' MB' |
| 103 | + case byteSize >= Math.pow(1000, 3) && byteSize < Math.pow(1000, 4): |
| 104 | + return round(byteSize / Math.pow(1000, 3), 2) + ' GB' |
| 105 | + default: |
| 106 | + return round(byteSize / Math.pow(1000, 4), 2) + ' TB' |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Format kilobits per second (kbps, typically a network speed) |
| 112 | + * |
| 113 | + * @param kbps the number of kilobits to format |
| 114 | + * @returns a string representing the input value with the appropriate unit |
| 115 | + */ |
| 116 | +export const kbpsFormat = (kbps: number) => { |
| 117 | + switch (true) { |
| 118 | + case isNaN(kbps) || (!kbps && kbps !== 0): |
| 119 | + return '-' |
| 120 | + case kbps >= 0 && kbps < 1000: |
| 121 | + return round(kbps, 2) + ' kbps' |
| 122 | + case kbps >= 1000 && kbps < Math.pow(1000, 2): |
| 123 | + return round(kbps / 1000, 2) + ' Mbps' |
| 124 | + case kbps >= Math.pow(1000, 2) && kbps < Math.pow(1000, 3): |
| 125 | + return round(kbps / Math.pow(1000, 2), 2) + ' Gbps' |
| 126 | + default: |
| 127 | + return round(kbps / Math.pow(1000, 3), 2) + ' Tbps' |
| 128 | + } |
| 129 | +} |
0 commit comments