summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/unit_format/index.js
blob: d3aea37e677f133663f7df6e8ad1486cc2b5d8e4 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { s__ } from '~/locale';

import {
  suffixFormatter,
  scaledSIFormatter,
  scaledBinaryFormatter,
  numberFormatter,
} from './formatter_factory';

/**
 * Supported formats
 *
 * Based on:
 *
 * https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier
 */
export const SUPPORTED_FORMATS = {
  // Number
  number: 'number',
  percent: 'percent',
  percentHundred: 'percentHundred',

  // Duration
  seconds: 'seconds',
  milliseconds: 'milliseconds',

  // Digital (Metric)
  decimalBytes: 'decimalBytes',
  kilobytes: 'kilobytes',
  megabytes: 'megabytes',
  gigabytes: 'gigabytes',
  terabytes: 'terabytes',
  petabytes: 'petabytes',

  // Digital (IEC)
  bytes: 'bytes',
  kibibytes: 'kibibytes',
  mebibytes: 'mebibytes',
  gibibytes: 'gibibytes',
  tebibytes: 'tebibytes',
  pebibytes: 'pebibytes',
};

/**
 * Returns a function that formats number to different units
 * @param {String} format - Format to use, must be one of the SUPPORTED_FORMATS. Defaults to number.
 *
 *
 */
export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
  // Number

  if (format === SUPPORTED_FORMATS.number) {
    /**
     * Formats a number
     *
     * @function
     * @param {Number} value - Number to format
     * @param {Number} fractionDigits - precision decimals
     * @param {Number} maxLength - Max lenght of formatted number
     * if lenght is exceeded, exponential format is used.
     */
    return numberFormatter();
  }
  if (format === SUPPORTED_FORMATS.percent) {
    /**
     * Formats a percentge (0 - 1)
     *
     * @function
     * @param {Number} value - Number to format, `1` is rendered as `100%`
     * @param {Number} fractionDigits - number of precision decimals
     * @param {Number} maxLength - Max lenght of formatted number
     * if lenght is exceeded, exponential format is used.
     */
    return numberFormatter('percent');
  }
  if (format === SUPPORTED_FORMATS.percentHundred) {
    /**
     * Formats a percentge (0 to 100)
     *
     * @function
     * @param {Number} value - Number to format, `100` is rendered as `100%`
     * @param {Number} fractionDigits - number of precision decimals
     * @param {Number} maxLength - Max lenght of formatted number
     * if lenght is exceeded, exponential format is used.
     */
    return numberFormatter('percent', 1 / 100);
  }

  // Durations

  if (format === SUPPORTED_FORMATS.seconds) {
    /**
     * Formats a number of seconds
     *
     * @function
     * @param {Number} value - Number to format, `1` is rendered as `1s`
     * @param {Number} fractionDigits - number of precision decimals
     * @param {Number} maxLength - Max lenght of formatted number
     * if lenght is exceeded, exponential format is used.
     */
    return suffixFormatter(s__('Units|s'));
  }
  if (format === SUPPORTED_FORMATS.milliseconds) {
    /**
     * Formats a number of milliseconds with ms as units
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1ms`
     * @param {Number} fractionDigits - number of precision decimals
     * @param {Number} maxLength - Max lenght of formatted number
     * if lenght is exceeded, exponential format is used.
     */
    return suffixFormatter(s__('Units|ms'));
  }

  // Digital (Metric)

  if (format === SUPPORTED_FORMATS.decimalBytes) {
    /**
     * Formats a number of bytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1B`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B');
  }
  if (format === SUPPORTED_FORMATS.kilobytes) {
    /**
     * Formats a number of kilobytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1kB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B', 1);
  }
  if (format === SUPPORTED_FORMATS.megabytes) {
    /**
     * Formats a number of megabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1MB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B', 2);
  }
  if (format === SUPPORTED_FORMATS.gigabytes) {
    /**
     * Formats a number of gigabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1GB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B', 3);
  }
  if (format === SUPPORTED_FORMATS.terabytes) {
    /**
     * Formats a number of terabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1GB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B', 4);
  }
  if (format === SUPPORTED_FORMATS.petabytes) {
    /**
     * Formats a number of petabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1PB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledSIFormatter('B', 5);
  }

  // Digital (IEC)

  if (format === SUPPORTED_FORMATS.bytes) {
    /**
     * Formats a number of bytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1B`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B');
  }
  if (format === SUPPORTED_FORMATS.kibibytes) {
    /**
     * Formats a number of kilobytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1kB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B', 1);
  }
  if (format === SUPPORTED_FORMATS.mebibytes) {
    /**
     * Formats a number of megabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1MB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B', 2);
  }
  if (format === SUPPORTED_FORMATS.gibibytes) {
    /**
     * Formats a number of gigabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1GB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B', 3);
  }
  if (format === SUPPORTED_FORMATS.tebibytes) {
    /**
     * Formats a number of terabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1GB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B', 4);
  }
  if (format === SUPPORTED_FORMATS.pebibytes) {
    /**
     * Formats a number of petabytes scaled up to larger digital
     * units for larger numbers.
     *
     * @function
     * @param {Number} value - Number to format, `1` is formatted as `1PB`
     * @param {Number} fractionDigits - number of precision decimals
     */
    return scaledBinaryFormatter('B', 5);
  }

  // Fail so client library addresses issue
  throw TypeError(`${format} is not a valid number format`);
};