@cloudflare/util-formatters
Advanced tools
Comparing version 2.4.29 to 2.4.30
@@ -6,2 +6,13 @@ # Change Log | ||
## [2.4.30](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.4.29...@cloudflare/util-formatters@2.4.30) (2020-05-20) | ||
### Bug Fixes | ||
* **util-formatters:** LOUI-1014 Abbreviate numbers as short scale rather ([73bf84e](http://stash.cfops.it:7999/fe/stratus/commits/73bf84e)) | ||
## [2.4.29](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-formatters@2.4.27...@cloudflare/util-formatters@2.4.29) (2020-04-27) | ||
@@ -8,0 +19,0 @@ |
@@ -38,3 +38,3 @@ import { SupportedLocales } from '@cloudflare/intl-core'; | ||
export declare const localizeNumberWithPrecision: (number: number, locale?: SupportedLocales) => string; | ||
export declare const formatNumber: (number: number, locale?: SupportedLocales, useSI?: boolean, decimalPlaces?: number, trimInsignificantZeros?: boolean) => string; | ||
export declare const formatNumber: (number: number, locale?: SupportedLocales, abbreviate?: boolean | "si", decimalPlaces?: number, trimInsignificantZeros?: boolean) => string; | ||
export declare type CurrencyOptions = { | ||
@@ -41,0 +41,0 @@ locale: SupportedLocales; |
@@ -64,10 +64,10 @@ import { format as d3Format } from 'd3-format'; | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LOCALE; | ||
var useSI = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var abbreviate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var decimalPlaces = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2; | ||
var trimInsignificantZeros = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true; | ||
var val = number; | ||
var suffix = ''; // SI units = International system of units. | ||
var siSuffix = ''; // SI units = International system of units. | ||
// Don't use SI units for sub-1 numbers (milli, micro, etc) | ||
if (useSI && number >= 1) { | ||
if (abbreviate && number >= 1) { | ||
// d3Format notes: | ||
@@ -84,3 +84,3 @@ // 1.) s refers to decimal notation with an SI prefix, rounded to significant digits. | ||
val = +siMatch[1]; | ||
suffix = siMatch[2] || ''; | ||
siSuffix = siMatch[2] || ''; | ||
} | ||
@@ -93,3 +93,19 @@ } // d3Format notes: | ||
var formatted = d3Format(".".concat(decimalPlaces).concat(trimInsignificantZeros ? '~' : '', "f"))(val); | ||
return Number(formatted).toLocaleString(locale) + suffix; | ||
var formattedAsLocale = Number(formatted).toLocaleString(locale); | ||
var siToShortScaleSuffix = { | ||
k: 'k', | ||
M: 'M', | ||
G: 'B', | ||
T: 't', | ||
P: 'q' | ||
}; // Note - abbreviate can be set to either `true`, which will abbreviate the number | ||
// as 1.5B for 1.5 billion. Or, it can be set to the string 'si', which will format | ||
// the number using the SI suffix instead. For billions this is 'giga' which makes | ||
// sense for bits and bytes, but does not for formatting counts of say, requests. | ||
if (abbreviate === true && siToShortScaleSuffix.hasOwnProperty(siSuffix)) { | ||
return formattedAsLocale + siToShortScaleSuffix[siSuffix]; | ||
} | ||
return formattedAsLocale + siSuffix; | ||
}; | ||
@@ -140,3 +156,3 @@ export var formatCurrency = function formatCurrency() { | ||
var includeDecimals = arguments.length > 2 ? arguments[2] : undefined; | ||
return formatNumber(number, locale, true, includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
.replace(/([a-zA-Z]?)$/, ' $1') + 'B'; | ||
@@ -156,3 +172,3 @@ }; | ||
var useBit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; | ||
return formatNumber(number, locale, true, includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
.replace(/([a-zA-Z]?)$/, ' $1') + "".concat(useBit ? 'bit' : 'b'); | ||
@@ -159,0 +175,0 @@ }; |
@@ -83,10 +83,10 @@ "use strict"; | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LOCALE; | ||
var useSI = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var abbreviate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var decimalPlaces = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2; | ||
var trimInsignificantZeros = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true; | ||
var val = number; | ||
var suffix = ''; // SI units = International system of units. | ||
var siSuffix = ''; // SI units = International system of units. | ||
// Don't use SI units for sub-1 numbers (milli, micro, etc) | ||
if (useSI && number >= 1) { | ||
if (abbreviate && number >= 1) { | ||
// d3Format notes: | ||
@@ -103,3 +103,3 @@ // 1.) s refers to decimal notation with an SI prefix, rounded to significant digits. | ||
val = +siMatch[1]; | ||
suffix = siMatch[2] || ''; | ||
siSuffix = siMatch[2] || ''; | ||
} | ||
@@ -112,3 +112,19 @@ } // d3Format notes: | ||
var formatted = (0, _d3Format.format)(".".concat(decimalPlaces).concat(trimInsignificantZeros ? '~' : '', "f"))(val); | ||
return Number(formatted).toLocaleString(locale) + suffix; | ||
var formattedAsLocale = Number(formatted).toLocaleString(locale); | ||
var siToShortScaleSuffix = { | ||
k: 'k', | ||
M: 'M', | ||
G: 'B', | ||
T: 't', | ||
P: 'q' | ||
}; // Note - abbreviate can be set to either `true`, which will abbreviate the number | ||
// as 1.5B for 1.5 billion. Or, it can be set to the string 'si', which will format | ||
// the number using the SI suffix instead. For billions this is 'giga' which makes | ||
// sense for bits and bytes, but does not for formatting counts of say, requests. | ||
if (abbreviate === true && siToShortScaleSuffix.hasOwnProperty(siSuffix)) { | ||
return formattedAsLocale + siToShortScaleSuffix[siSuffix]; | ||
} | ||
return formattedAsLocale + siSuffix; | ||
}; | ||
@@ -165,3 +181,3 @@ | ||
var includeDecimals = arguments.length > 2 ? arguments[2] : undefined; | ||
return formatNumber(number, locale, true, includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
.replace(/([a-zA-Z]?)$/, ' $1') + 'B'; | ||
@@ -184,3 +200,3 @@ }; | ||
var useBit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; | ||
return formatNumber(number, locale, true, includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
return formatNumber(number, locale, 'si', includeDecimals ? 2 : 0) // Add space between value and SI unit | ||
.replace(/([a-zA-Z]?)$/, ' $1') + "".concat(useBit ? 'bit' : 'b'); | ||
@@ -187,0 +203,0 @@ }; |
{ | ||
"name": "@cloudflare/util-formatters", | ||
"description": "", | ||
"version": "2.4.29", | ||
"version": "2.4.30", | ||
"types": "./dist/index.d.ts", | ||
@@ -25,3 +25,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "3a5a556f3392b3c8b6e35a7857a39f05ac1bb7eb" | ||
"gitHead": "94376cc03d5549dbcd3c061407fc10381f282d30" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29079
475