@cloudflare/util-formatters 2.7.0 → 2.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/formatters.d.ts +2 -1
- package/es/formatters.js +25 -3
- package/lib/formatters.js +31 -3
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,30 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.7.3](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.7.2...@cloudflare/util-formatters@2.7.3) (2022-04-12)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @cloudflare/util-formatters
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [2.7.2](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.7.1...@cloudflare/util-formatters@2.7.2) (2022-02-14)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @cloudflare/util-formatters
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## [2.7.1](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.7.0...@cloudflare/util-formatters@2.7.1) (2021-12-06)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @cloudflare/util-formatters
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
6
30
|
# [2.7.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.6.15...@cloudflare/util-formatters@2.7.0) (2021-11-29)
|
|
7
31
|
|
|
8
32
|
|
package/dist/formatters.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export declare const formatCurrency: (value?: CurrencyValue, options?: CurrencyO
|
|
|
48
48
|
* @param {Boolean} [includeDecimals]
|
|
49
49
|
* @return {String}
|
|
50
50
|
*/
|
|
51
|
-
export declare const formatBytes: (number: number, locale: SupportedLocales | undefined, includeDecimals: boolean) => string;
|
|
51
|
+
export declare const formatBytes: (number: number, locale: SupportedLocales | undefined, includeDecimals: boolean, useBytes?: boolean) => string;
|
|
52
52
|
/**
|
|
53
53
|
* @param {Number} number
|
|
54
54
|
* @param {String} locale
|
|
@@ -68,3 +68,4 @@ export declare const formatBits: (number: number, locale: SupportedLocales | und
|
|
|
68
68
|
export declare const formatNumberForAnalytics: (number: number, locale?: SupportedLocales, useSI?: boolean, decimalPlaces?: number, trimInsignificantZeros?: boolean) => string;
|
|
69
69
|
export declare const capitalizeStr: (str: string) => string | undefined;
|
|
70
70
|
export declare const formatPercentage: (value: number, total: number) => string;
|
|
71
|
+
export declare const roundPercentage: (val: number, roundDecimalCases?: number, locale?: SupportedLocales) => string;
|
package/es/formatters.js
CHANGED
|
@@ -135,9 +135,9 @@ export const formatCurrency = (value = 0, options = {
|
|
|
135
135
|
* @return {String}
|
|
136
136
|
*/
|
|
137
137
|
|
|
138
|
-
export const formatBytes = (number, locale = DEFAULT_LOCALE, includeDecimals) => {
|
|
138
|
+
export const formatBytes = (number, locale = DEFAULT_LOCALE, includeDecimals, useBytes = false) => {
|
|
139
139
|
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit
|
|
140
|
-
.replace(/([a-zA-Z]?)$/, ' $1') + 'B'
|
|
140
|
+
.replace(/([a-zA-Z]?)$/, ' $1') + `${useBytes ? 'Bytes' : 'B'}`;
|
|
141
141
|
};
|
|
142
142
|
/**
|
|
143
143
|
* @param {Number} number
|
|
@@ -164,4 +164,26 @@ export const formatNumberForAnalytics = (number, locale = DEFAULT_LOCALE, useSI
|
|
|
164
164
|
return formatNumber(number, locale, useSI, decimalPlaces, trimInsignificantZeros);
|
|
165
165
|
};
|
|
166
166
|
export const capitalizeStr = str => str && str.charAt(0).toUpperCase() + str.slice(1);
|
|
167
|
-
export const formatPercentage = (value, total) => `${Math.round(value * 100 / total * 100) / 100}%`;
|
|
167
|
+
export const formatPercentage = (value, total) => `${Math.round(value * 100 / total * 100) / 100}%`;
|
|
168
|
+
export const roundPercentage = (val, roundDecimalCases = 0, locale = DEFAULT_LOCALE) => {
|
|
169
|
+
const roundDiv = Math.pow(10, roundDecimalCases); // Treat ~0% and ~100% values specially so the precision is surfaced
|
|
170
|
+
|
|
171
|
+
let localizedString = '';
|
|
172
|
+
|
|
173
|
+
if (val > 99) {
|
|
174
|
+
// Edge case: between 99 and 100, produce values with precision like this:
|
|
175
|
+
// 99.999735 --> 99.9997
|
|
176
|
+
localizedString = localizeNumberWithPrecision(100 - +(100 - val).toPrecision(1), locale);
|
|
177
|
+
} else if (val < 1) {
|
|
178
|
+
// Edge case: between 0 and 1, produce values with the precision of one
|
|
179
|
+
// significant digit:
|
|
180
|
+
// 0.000273 --> 0.0003
|
|
181
|
+
localizedString = localizeNumberWithPrecision(+val.toPrecision(1), locale);
|
|
182
|
+
} else {
|
|
183
|
+
// In all other cases, round to the nearest integer:
|
|
184
|
+
// 78.612 --> 79
|
|
185
|
+
localizedString = formatNumberForAnalytics(val * roundDiv / roundDiv, locale, false, roundDecimalCases);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return localizedString + '%';
|
|
189
|
+
};
|
package/lib/formatters.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.formatPercentage = exports.capitalizeStr = exports.formatNumberForAnalytics = exports.formatBits = exports.formatBytes = exports.formatCurrency = exports.formatNumber = exports.localizeNumberWithPrecision = exports.formatDate = exports.DateFormatters = void 0;
|
|
6
|
+
exports.roundPercentage = exports.formatPercentage = exports.capitalizeStr = exports.formatNumberForAnalytics = exports.formatBits = exports.formatBytes = exports.formatCurrency = exports.formatNumber = exports.localizeNumberWithPrecision = exports.formatDate = exports.DateFormatters = void 0;
|
|
7
7
|
|
|
8
8
|
var _d3Format = require("d3-format");
|
|
9
9
|
|
|
@@ -174,8 +174,9 @@ exports.formatCurrency = formatCurrency;
|
|
|
174
174
|
var formatBytes = function formatBytes(number) {
|
|
175
175
|
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LOCALE;
|
|
176
176
|
var includeDecimals = arguments.length > 2 ? arguments[2] : undefined;
|
|
177
|
+
var useBytes = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
177
178
|
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit
|
|
178
|
-
.replace(/([a-zA-Z]?)$/, ' $1') + 'B';
|
|
179
|
+
.replace(/([a-zA-Z]?)$/, ' $1') + "".concat(useBytes ? 'Bytes' : 'B');
|
|
179
180
|
};
|
|
180
181
|
/**
|
|
181
182
|
* @param {Number} number
|
|
@@ -227,4 +228,31 @@ var formatPercentage = function formatPercentage(value, total) {
|
|
|
227
228
|
return "".concat(Math.round(value * 100 / total * 100) / 100, "%");
|
|
228
229
|
};
|
|
229
230
|
|
|
230
|
-
exports.formatPercentage = formatPercentage;
|
|
231
|
+
exports.formatPercentage = formatPercentage;
|
|
232
|
+
|
|
233
|
+
var roundPercentage = function roundPercentage(val) {
|
|
234
|
+
var roundDecimalCases = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
235
|
+
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_LOCALE;
|
|
236
|
+
var roundDiv = Math.pow(10, roundDecimalCases); // Treat ~0% and ~100% values specially so the precision is surfaced
|
|
237
|
+
|
|
238
|
+
var localizedString = '';
|
|
239
|
+
|
|
240
|
+
if (val > 99) {
|
|
241
|
+
// Edge case: between 99 and 100, produce values with precision like this:
|
|
242
|
+
// 99.999735 --> 99.9997
|
|
243
|
+
localizedString = localizeNumberWithPrecision(100 - +(100 - val).toPrecision(1), locale);
|
|
244
|
+
} else if (val < 1) {
|
|
245
|
+
// Edge case: between 0 and 1, produce values with the precision of one
|
|
246
|
+
// significant digit:
|
|
247
|
+
// 0.000273 --> 0.0003
|
|
248
|
+
localizedString = localizeNumberWithPrecision(+val.toPrecision(1), locale);
|
|
249
|
+
} else {
|
|
250
|
+
// In all other cases, round to the nearest integer:
|
|
251
|
+
// 78.612 --> 79
|
|
252
|
+
localizedString = formatNumberForAnalytics(val * roundDiv / roundDiv, locale, false, roundDecimalCases);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return localizedString + '%';
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
exports.roundPercentage = roundPercentage;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/util-formatters",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "2.7.
|
|
4
|
+
"version": "2.7.3",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"module": "es/index.js",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"autoGeneratedReadme": true
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@cloudflare/intl-core": "^1.10.
|
|
20
|
+
"@cloudflare/intl-core": "^1.10.4",
|
|
21
21
|
"d3-format": "^1.3.2",
|
|
22
|
-
"moment": "^2.
|
|
22
|
+
"moment": "^2.29.1"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "16064397a5d5f2cf70fbbb5ced5c176604397439"
|
|
25
25
|
}
|