Units
Package for converting and formatting units and numerical values.
npm install @dynatrace-sdk/units
Conversion example
Code example
import { convert, units } from "@dynatrace-sdk/units";
convert(1000,
// units.<category>.<unit>
units.length.meter, // FromUnit
units.length.kilometer, // ToUnit
); // 1
Interfaces
AdjustFractionDigitsOptions
Properties
Name | Type | Description |
---|---|---|
locale | string | Array<string> | The locale according to which the number will be adjusted. |
maximumFractionDigits | number | Maximum number of decimal digits. |
maximumSignificantDigits | number | Maximum number of significant digits. Default is 21 |
minimumFractionDigits | number | Minimum number of decimal digits. |
minimumSignificantDigits | number | Minimum number of significant digits. Default is 1 |
useGrouping | boolean | Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. |
FormatDateOptions
Interface that extends Intl.DateTimeFormatOptions to pass an optional property to overwrite the default locale. See MDN.
Properties
Name | Type | Description |
---|---|---|
calendar | string | |
dateStyle | "long" | "short" | "full" | "medium" | |
day | "numeric" | "2-digit" | |
dayPeriod | "long" | "short" | "narrow" | |
era | "long" | "short" | "narrow" | |
formatMatcher | "best fit" | "basic" | |
fractionalSecondDigits | 1 | 2 | 3 | |
hour | "numeric" | "2-digit" | |
hour12 | boolean | |
hourCycle | "h11" | "h12" | "h23" | "h24" | |
locale | string | Array<string> | The locale according to which the number will be formatted. |
localeMatcher | "best fit" | "lookup" | |
minute | "numeric" | "2-digit" | |
month | "long" | "short" | "narrow" | "numeric" | "2-digit" | |
numberingSystem | string | |
second | "numeric" | "2-digit" | |
timeStyle | "long" | "short" | "full" | "medium" | |
timeZone | string | |
timeZoneName | "long" | "short" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric" | |
weekday | "long" | "short" | "narrow" | |
year | "numeric" | "2-digit" |
FormatOptions
Properties
Name | Type | Description |
---|---|---|
abbreviate | boolean | It will shorten the number to a shorter format. (e.g. |
cascade | number | Will search for the biggest unit within its group and cascade it down the the specified dept. The last number will get rounded to |
input | FromUnit | If input unit is defined, it will convert it to the best fitting unit. (e.g. |
locale | string | Array<string> | The locale that will be used to format the number. By default it will use the platform locale specified by the user. |
maximumFractionDigits | number | The amount of maximumFractionDigits points. See MDN. If minimumFractionDigits is passed, set to minimumFractionDigits by default. Otherwise set to 0 by default. |
maximumSignificantDigits | number | Maximum number of significant digits. Default is 21 |
minimumFractionDigits | number | The amount of minimumFractionDigits points. See MDN. |
minimumSignificantDigits | number | Minimum number of significant digits. Default is 1 |
output | OutputUnit<UndefinedCoalescing<FromUnit | > | ToUnit> | If not specified, the conversion is disabled (e.g. |
suffix | string | A custom suffix that overwrites the unit symbol. |
useGrouping | boolean | Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators See MDN |
Variables
ExponentialDecimalLevels: Object
Abbreviation levels for decimal metric prefixes from kilo to quecto, 10^3, 10^6, ...
ExponentialOctalBitLevels: Object
Abbreviation levels for bit binary prefixes from kilobit to quebibit, 2^10, 2^20, ...
ExponentialOctalByteLevels: Object
Abbreviation levels for byte binary prefixes from kibibyte to quebibyte, 2^10, 2^20, ...
units: Object
Grouped collection of all the supported units
Functions
_cascade
Cascades the input to the best fitting unit, depth/precision can be adjusted Supported base units units.electricity.ampere, units.electricity.volt, units.electricity.ohm, units.electricity.watt, units.electricity.coulomb, units.electricity.farad, units.electricity.weber, units.electricity.tesla, units.electricity.henry, units.electricity.siemens, units.force.newton, units.frequency.hertz, units.length.meter, units.mass.gram, units.physics.gray, units.physics.sievert, units.physics.candela, units.physics.lumen, units.physics.lux, units.physics.steradian, units.pressure.pascal, units.time.second, units.length.meter, units.data.byte, units.data.bit, units.unspecified.pixel units.angle.degree
Parameters
Name | Type |
---|---|
number*required | number |
unit*required | ConvertibleUnit |
depth*required | number |
list | CascadeUnit |
fractionDigits | number |
_convertTemperature
Parameters
Name | Type |
---|---|
input*required | number |
from*required | FromUnit |
to*required | ConvertibleTarget<FromUnit | ToUnit> |
_getDateFromExpression
Converts expression details into a date object
Parameters
Name | Type |
---|---|
__namedParameters*required | ExpressionDetails |
relativeDate*required | number |
_getTimeframeExpression
Returns the TimeframeExpression regex.
_parseDatetimeInput
Generates a date object out of the input string if provided in one of the accepted formats
Parameters
Name | Type |
---|---|
candidate*required | string |
_parseISOWithExtendedPrecision
Checks if the provided value is a valid ISO string with support for extended (up tp nano) precision. We cannot use dateFns parseISO because it returns native Date object, which do not support extended precision.
Parameters
Name | Type |
---|---|
candidate*required | string |
_parseTimeExpression
Parses the input for expression details.
Parameters
Name | Type |
---|---|
candidate*required | string |
abbreviateNumber
Abbreviate large numbers to a shorter format. Example:
import { abbreviateNumber } from '@dynatrace-sdk/units';
abbreviateNumber(1500) // {"formattedValue": "2", "postfix": "k"}
Parameters
Name | Type | Description |
---|---|---|
input*required | number | Number to be abbreviated. |
scale*required | Scale | Scale to use for the abbreviation. Default is the decimal scale:
|
options*required | AdjustFractionDigitsOptions |
adjustFractionDigits
Adjust number of fractional digits.
Parameters
Name | Type | Description |
---|---|---|
input*required | number | The number to be formatted. |
options | AdjustFractionDigitsOptions |
Returns
Description |
---|
The formatted number as a string. |
convert
Converts a given number to the specified unit. Example:
import { convert, units } from "@dynatrace-sdk/units";
convert(1500, units.length.meter, units.length.kilometer) // 1.5
Parameters
Name | Type | Description |
---|---|---|
input*required | number | The number that will get converted. |
from*required | FromUnit | The unit of the input (e.g. units.length.meter ). |
to*required | ConvertibleTarget<FromUnit | ToUnit> | The unit of the output (e.g. units.length.kilometer ). |
Returns
Description |
---|
The result of the conversion (e.g. 1.5 ). |
format
Converts and formats the provided number.
Parameters
Name | Type | Description |
---|---|---|
number*required | number | The number that will get formatted. |
options | FormatOptions<FromUnit | ToUnit> | Formatting options. |
Returns
Description |
---|
A string containing the formatted number. |
formatCurrency
Formats a number with the currency indicated
Parameters
Name | Type | Description |
---|---|---|
number*required | number | The value to be formatted |
currency*required | undefined | string | The ISO 4217 currency codes, such as 'USD' or 'EUR', to which the number will be formatted |
options | FormatCurrencyOptions | Options to adapt the formatting (locale and properties accepted by Intl.NumberFormat) |
Returns
Description |
---|
the formatted number to the specified currency |
formatDate
Formats a date according to locale.
Parameters
Name | Type | Description |
---|---|---|
input*required | number | Date | Number of milliseconds since UNIX epoch or a Javascript Date. |
options | FormatDateOptions | An object that contains one or more properties that specify comparison options. |
Returns
Description |
---|
Date string |
formatLong
Parameters
Name | Type |
---|---|
value*required | string | number | bigint |
options | FormatLong |
formatUnit
getFormatting
Converts and formats the provided number, but returns the formatting as parts.
Parameters
Name | Type | Description |
---|---|---|
number*required | number | The number that will get formatted. |
options | FormatOptions<FromUnit | ToUnit> | Formatting options. |
Returns
Description |
---|
An Array of parts containing the formatting result object. |
parseTime
You can use the parseTime
utility function to convert a string into
TimeDetails
, giving you a normalized version of the input string (trimmed
string), a Date
object created from the given input, and the type of input.
The type can be either an expression
or iso8601
string. If it is not
possible to convert the input, the function will return null
.
Parameters
Name | Type |
---|---|
null | string | |
number |
parseTimeAsTimeValue
You can use the parseTime
utility function to convert a string into
TimeValue
, giving you a normalized version of the input string (trimmed
string), a Date
object created from the given input, and the type of input.
The type can be either an expression
or iso8601
string. If it is not
possible to convert the input, the function will return null
.
Parameters
Name | Type |
---|---|
candidate | null | string |
relativeDate*required | number |
variantNames
Returns the names of all units to which the provided unit can be converted to.
Parameters
Name | Type |
---|---|
unit*required | U |
variantUnits
Returns all units to which the provided unit can be converted to.
Parameters
Name | Type |
---|---|
unit*required | U |
ConvertibleUnit
All supported convertible units. For more details see a conversion example.
import { units } from '@dynatrace-sdk/units';
units.length.meter // extends ConvertibleUnit
FromUnit
A type for every convertible unit. For more details see a conversion example.
ToUnit
All units to which the FromUnit
can be converted. For more details see a conversion example.
FormattableUnit
A unit that can be formatted. Example:
import { units } from '@dynatrace-sdk/units';
units.currency.eur // extends FormattableUnit
Unit
A ConvertibleUnit or a FormattableUnit or both.
ConvertibleTarget
TypeScript helper that returns all the possible convertible targets, where the first generic is FromUnit, and second is ToUnit.