11import {
22 formatDate as ngFormatDate ,
33 getLocaleCurrencyCode ,
4- getLocaleDateFormat as ngGetLocaleDateFormat ,
5- getLocaleDateTimeFormat as ngGetLocaleDateTimeFormat ,
64 getLocaleFirstDayOfWeek as ngGetLocaleFirstDayOfWeek ,
75 formatNumber as ngFormatNumber ,
86 formatPercent as ngFormatPercent ,
97 getCurrencySymbol as ngGetCurrencySymbol ,
10- FormatWidth ,
118 CurrencyPipe
129} from '@angular/common' ;
1310import { InjectionToken } from '@angular/core' ;
14- import { getCurrentI18n } from 'igniteui-i18n-core' ;
11+ import { getCurrentI18n , getDateFormatter } from 'igniteui-i18n-core' ;
1512
1613/**
1714 * Injection token that allows for retrieving the i18n formatter for the IgniteUI components.
@@ -30,60 +27,40 @@ export class BaseFormatter {
3027 } ;
3128 private _currencyPipe = new CurrencyPipe ( 'en-US' , 'USD' ) ;
3229
33- /**
34- * Returns the date format based on a provided locale.
35- * Supports Angular's DatePipe format options: `short`, `medium`, `long`, `full`, `shortDate`, `mediumDate`, `longDate` and `fullDate`.
36- */
37- public getLocaleDateFormat ( locale : string , displayFormat ?: string ) : string {
30+ public getSizeFromDisplayFormat ( displayFormat : string | null | undefined ) {
3831 const formatKeys = Object . keys ( this . IntlDateTimeStyleValues ) as ( keyof typeof this . IntlDateTimeStyleValues ) [ ] ;
3932 const targetFormat = displayFormat ?. toLowerCase ( ) . replace ( 'date' , '' ) ;
40- const targetKey = targetFormat ? formatKeys . find ( k => k === targetFormat ) : '' ;
41- if ( ! targetKey ) {
42- // if displayFormat is not shortDate, longDate, etc.
43- // or if it is not set by the user
44- return displayFormat ;
45- }
46-
47- return ngGetLocaleDateFormat ( locale , FormatWidth [ this . IntlDateTimeStyleValues [ targetKey ] ] ) ;
33+ return targetFormat ? formatKeys . find ( k => k === targetFormat ) : null ;
4834 }
4935
5036 /**
51- * Returns the date and time format based on a provided locale.
52- * Supports Angular's DatePipe format options: `short`, `medium`, `long`, `full`, `shortDate`, `mediumDate`, `longDate` and `fullDate`.
37+ * Returns the date and time format based on a provided locale and options.
5338 */
54- public getLocaleDateTimeFormat ( locale : string , displayFormat ?: string ) : string {
55- const formatKeys = Object . keys ( this . IntlDateTimeStyleValues ) as ( keyof typeof this . IntlDateTimeStyleValues ) [ ] ;
56- const targetFormat = displayFormat ?. toLowerCase ( ) . replace ( 'date' , '' ) ;
57- const targetKey = formatKeys . find ( k => k === targetFormat ) ;
58- if ( ! targetKey ) {
59- // if displayFormat is not shortDate, longDate, etc.
60- // or if it is not set by the user
61- return displayFormat ;
62- }
63- return ngGetLocaleDateTimeFormat ( locale , FormatWidth [ this . IntlDateTimeStyleValues [ targetKey ] ] ) ;
39+ public getLocaleDateTimeFormat ( locale : string , forceLeadingZero = false , options ?: Intl . DateTimeFormatOptions ) : string {
40+ return getDateFormatter ( ) . getLocaleDateTimeFormat ( locale , forceLeadingZero , options ) ;
6441 }
6542
6643 /**
6744 * Format provided date to reflect locales format. Similar to Angular's formatDate.
6845 */
6946 public formatDate ( value : Date | string | number | null | undefined , format : string , locale : string , timezone ?: string ) : string {
70- return value ? ngFormatDate ( value , format , locale , timezone ) : '' ;
47+ return value != null ? ngFormatDate ( value , format , locale , timezone ) : '' ;
7148 }
7249
7350 /** Format number value based on locale */
7451 public formatNumber ( value : number | string | null | undefined , locale : string , digitsInfo ?: string ) : string {
7552 if ( typeof value === "string" ) {
7653 value = parseFloat ( value ) ;
7754 }
78- return value ? ngFormatNumber ( value , locale , digitsInfo ) : '' ;
55+ return value != null ? ngFormatNumber ( value , locale , digitsInfo ) : '' ;
7956 }
8057
8158 /** Format number value as percent based on locale */
8259 public formatPercent ( value : number | string | null | undefined , locale : string , digitsInfo ?: string ) : string {
8360 if ( typeof value === "string" ) {
8461 value = parseFloat ( value ) ;
8562 }
86- return value ? ngFormatPercent ( value , locale , digitsInfo ) : '' ;
63+ return value != null ? ngFormatPercent ( value , locale , digitsInfo ) : '' ;
8764 }
8865
8966 /** Format number as a currency based on locale */
@@ -92,7 +69,7 @@ export class BaseFormatter {
9269 value = parseFloat ( value ) ;
9370 }
9471
95- return value ? this . _currencyPipe . transform ( value , currencyCode , display , digitsInfo , locale ?? getCurrentI18n ( ) ) : '' ;
72+ return value != null ? this . _currencyPipe . transform ( value , currencyCode , display , digitsInfo , locale ?? getCurrentI18n ( ) ) : '' ;
9673 }
9774
9875 /**
0 commit comments