Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-28 15:09:05 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-28 15:09:05 +0300
commit5426ca9908085087d465fa52800335f408eb965a (patch)
tree6b442cff02fda9402fc7bb9cf9986e363dd5aaa6 /app/assets/javascripts/lib
parent67cdfd2683b89bce260600fa8925eefdcdf9e3e5 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r--app/assets/javascripts/lib/utils/unit_format/formatter_factory.js20
-rw-r--r--app/assets/javascripts/lib/utils/unit_format/index.js106
2 files changed, 118 insertions, 8 deletions
diff --git a/app/assets/javascripts/lib/utils/unit_format/formatter_factory.js b/app/assets/javascripts/lib/utils/unit_format/formatter_factory.js
index 432a9254558..98bcb8348e2 100644
--- a/app/assets/javascripts/lib/utils/unit_format/formatter_factory.js
+++ b/app/assets/javascripts/lib/utils/unit_format/formatter_factory.js
@@ -117,3 +117,23 @@ export const scaledSIFormatter = (unit = '', prefixOffset = 0) => {
return scaledFormatter(units);
};
+
+/**
+ * Returns a function that formats a number scaled using SI units notation.
+ */
+export const scaledBinaryFormatter = (unit = '', prefixOffset = 0) => {
+ // eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
+ const multiplicative = ['Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
+ const symbols = ['', ...multiplicative];
+
+ const units = symbols.slice(prefixOffset).map(prefix => {
+ return `${prefix}${unit}`;
+ });
+
+ if (!units.length) {
+ // eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
+ throw new RangeError('The unit cannot be converted, please try a different scale');
+ }
+
+ return scaledFormatter(units, 1024);
+};
diff --git a/app/assets/javascripts/lib/utils/unit_format/index.js b/app/assets/javascripts/lib/utils/unit_format/index.js
index daf70ebb5d7..d3aea37e677 100644
--- a/app/assets/javascripts/lib/utils/unit_format/index.js
+++ b/app/assets/javascripts/lib/utils/unit_format/index.js
@@ -1,9 +1,18 @@
import { s__ } from '~/locale';
-import { suffixFormatter, scaledSIFormatter, numberFormatter } from './formatter_factory';
+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
@@ -13,15 +22,23 @@ export const SUPPORTED_FORMATS = {
// Duration
seconds: 'seconds',
- miliseconds: 'miliseconds',
+ milliseconds: 'milliseconds',
- // Digital
- bytes: 'bytes',
+ // 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',
};
/**
@@ -32,6 +49,7 @@ export const SUPPORTED_FORMATS = {
*/
export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
// Number
+
if (format === SUPPORTED_FORMATS.number) {
/**
* Formats a number
@@ -70,6 +88,7 @@ export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
}
// Durations
+
if (format === SUPPORTED_FORMATS.seconds) {
/**
* Formats a number of seconds
@@ -82,9 +101,9 @@ export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
*/
return suffixFormatter(s__('Units|s'));
}
- if (format === SUPPORTED_FORMATS.miliseconds) {
+ if (format === SUPPORTED_FORMATS.milliseconds) {
/**
- * Formats a number of miliseconds with ms as units
+ * Formats a number of milliseconds with ms as units
*
* @function
* @param {Number} value - Number to format, `1` is formatted as `1ms`
@@ -95,8 +114,9 @@ export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
return suffixFormatter(s__('Units|ms'));
}
- // Digital
- if (format === SUPPORTED_FORMATS.bytes) {
+ // Digital (Metric)
+
+ if (format === SUPPORTED_FORMATS.decimalBytes) {
/**
* Formats a number of bytes scaled up to larger digital
* units for larger numbers.
@@ -162,6 +182,76 @@ export const getFormatter = (format = SUPPORTED_FORMATS.number) => {
*/
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`);
};