2021-03-28 18:32:48 +02:00
import { assert } from "chai" ;
2022-10-26 13:27:14 +02:00
import { HassEntity } from "home-assistant-js-websocket" ;
2021-03-28 18:32:48 +02:00
2022-10-26 13:27:14 +02:00
import {
formatNumber ,
getDefaultFormatOptions ,
getNumberFormatOptions ,
} from "../../../src/common/number/format_number" ;
2021-05-20 16:23:53 +02:00
import {
FrontendLocaleData ,
NumberFormat ,
TimeFormat ,
2022-10-10 16:58:27 +02:00
FirstWeekday ,
2021-05-20 16:23:53 +02:00
} from "../../../src/data/translation" ;
2021-03-28 18:32:48 +02:00
describe ( "formatNumber" , ( ) = > {
2021-05-20 16:23:53 +02:00
// Create default to not have to specify a not relevant TimeFormat over and over again.
const defaultLocale : FrontendLocaleData = {
language : "en" ,
number_format : NumberFormat.language ,
time_format : TimeFormat.language ,
2022-10-10 16:58:27 +02:00
first_weekday : FirstWeekday.language ,
2021-05-20 16:23:53 +02:00
} ;
2023-03-16 16:18:31 +01:00
// Node only ships with English support for `Intl`, so we cannot test for other number formats here.
2021-03-28 18:32:48 +02:00
it ( "Formats English numbers" , ( ) = > {
2021-05-20 16:23:53 +02:00
assert . strictEqual ( formatNumber ( 1234.5 , defaultLocale ) , "1,234.5" ) ;
2021-03-28 18:32:48 +02:00
} ) ;
2021-04-23 09:29:03 +02:00
it ( "Test format 'none' (keep dot despite language 'de')" , ( ) = > {
assert . strictEqual (
formatNumber ( 1.23 , {
2021-05-20 16:23:53 +02:00
. . . defaultLocale ,
2021-04-23 09:29:03 +02:00
language : "de" ,
number_format : NumberFormat.none ,
} ) ,
"1.23"
) ;
} ) ;
it ( "Ensure zero is kept for format 'language'" , ( ) = > {
2021-05-20 16:23:53 +02:00
assert . strictEqual ( formatNumber ( 0 , defaultLocale ) , "0" ) ;
2021-04-23 09:29:03 +02:00
} ) ;
it ( "Ensure zero is kept for format 'none'" , ( ) = > {
assert . strictEqual (
2021-05-20 16:23:53 +02:00
formatNumber ( 0 , { . . . defaultLocale , number_format : NumberFormat.none } ) ,
2021-04-23 09:29:03 +02:00
"0"
) ;
} ) ;
it ( "Test empty string input for format 'none'" , ( ) = > {
assert . strictEqual (
2021-05-20 16:23:53 +02:00
formatNumber ( "" , { . . . defaultLocale , number_format : NumberFormat.none } ) ,
2021-04-23 09:29:03 +02:00
""
) ;
} ) ;
it ( "Test empty string input for format 'language'" , ( ) = > {
2021-05-20 16:23:53 +02:00
assert . strictEqual ( formatNumber ( "" , defaultLocale ) , "0" ) ;
2021-04-23 09:29:03 +02:00
} ) ;
2021-03-28 18:32:48 +02:00
it ( "Formats number with options" , ( ) = > {
assert . strictEqual (
2021-05-20 16:23:53 +02:00
formatNumber ( 1234.5 , defaultLocale , {
minimumFractionDigits : 2 ,
} ) ,
2021-03-28 18:32:48 +02:00
"1,234.50"
) ;
} ) ;
2022-10-26 13:27:14 +02:00
2023-03-02 11:19:27 +01:00
it ( "Formats number with fraction digits options if number format is none" , ( ) = > {
assert . strictEqual (
formatNumber (
1234.5 ,
{ . . . defaultLocale , number_format : NumberFormat.none } ,
{
minimumFractionDigits : 2 ,
maximumFractionDigits : 2 ,
}
) ,
"1234.50"
) ;
} ) ;
it ( "Do not formats number with others options if number format is none" , ( ) = > {
assert . strictEqual (
formatNumber (
1234.5 ,
{ . . . defaultLocale , number_format : NumberFormat.none } ,
{
useGrouping : true ,
}
) ,
"1234.5"
) ;
} ) ;
2022-10-26 13:27:14 +02:00
it ( "Sets only the maximumFractionDigits format option when none are provided for a number value" , ( ) = > {
assert . deepEqual ( getDefaultFormatOptions ( 1234.5 ) , {
maximumFractionDigits : 2 ,
} ) ;
} ) ;
it ( "Sets minimumFractionDigits and maximumFractionDigits to '2' when none are provided for a string numeric value with two decimal places" , ( ) = > {
assert . deepEqual ( getDefaultFormatOptions ( "1234.50" ) , {
minimumFractionDigits : 2 ,
maximumFractionDigits : 2 ,
} ) ;
} ) ;
it ( "Merges default format options (minimumFractionDigits and maximumFractionDigits) and non-default format options for a string numeric value with two decimal places" , ( ) = > {
assert . deepEqual ( getDefaultFormatOptions ( "1234.50" , { currency : "USD" } ) , {
minimumFractionDigits : 2 ,
maximumFractionDigits : 2 ,
currency : "USD" ,
} ) ;
} ) ;
it ( "Sets maximumFractionDigits when that is the only format option provided" , ( ) = > {
assert . deepEqual (
getDefaultFormatOptions ( "1234.50" , { maximumFractionDigits : 0 } ) ,
{
maximumFractionDigits : 0 ,
}
) ;
} ) ;
it ( "Sets maximumFractionDigits to '2' and minimumFractionDigits to the provided value when only minimumFractionDigits is provided" , ( ) = > {
assert . deepEqual (
getDefaultFormatOptions ( "1234.50" , { minimumFractionDigits : 1 } ) ,
{
minimumFractionDigits : 1 ,
maximumFractionDigits : 2 ,
}
) ;
} ) ;
it ( "Sets maximumFractionDigits to '0' when the state value and step are integers" , ( ) = > {
assert . deepEqual (
getNumberFormatOptions ( {
state : "3.0" ,
attributes : { step : 1 } ,
} as unknown as HassEntity ) ,
{
maximumFractionDigits : 0 ,
}
) ;
} ) ;
it ( "Does not set any Intl.NumberFormatOptions when the step is not an integer" , ( ) = > {
assert . strictEqual (
getNumberFormatOptions ( {
state : "3.0" ,
attributes : { step : 0.5 } ,
2023-02-08 18:20:58 +01:00
} as unknown as HassEntity )
2022-10-26 13:27:14 +02:00
) ;
} ) ;
it ( "Does not set any Intl.NumberFormatOptions when the state value is not an integer" , ( ) = > {
assert . strictEqual (
getNumberFormatOptions ( { state : "3.5" } as unknown as HassEntity ) ,
undefined
) ;
} ) ;
it ( "Does not set any Intl.NumberFormatOptions when there is no step attribute" , ( ) = > {
assert . strictEqual (
getNumberFormatOptions ( { state : "3.0" } as unknown as HassEntity ) ,
undefined
) ;
} ) ;
2021-03-28 18:32:48 +02:00
} ) ;