1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import { Pipe , PipeTransform } from '@angular/core' ;
15+ import { OnDestroy , Pipe , PipeTransform } from '@angular/core' ;
1616import { CoreTime } from '@static/time' ;
1717import { CoreLogger } from '@static/logger' ;
18+ import { Translate } from '@singletons' ;
19+ import { Subscription } from 'rxjs' ;
1820
1921/**
2022 * Filter to format a date.
2123 */
2224@Pipe ( {
2325 name : 'coreFormatDate' ,
26+ pure : false ,
2427} )
25- export class CoreFormatDatePipe implements PipeTransform {
28+ export class CoreFormatDatePipe implements PipeTransform , OnDestroy {
2629
2730 protected logger : CoreLogger ;
31+ protected cachedResult ?: string ;
32+ protected subscription : Subscription ;
33+
34+ protected lastFormat ?: string ;
35+ protected lastTimestamp ?: string | number ;
36+ protected lastConvert ?: boolean ;
2837
2938 constructor ( ) {
3039 this . logger = CoreLogger . getInstance ( 'CoreFormatDatePipe' ) ;
40+
41+ this . subscription = Translate . onLangChange . subscribe ( ( ) => {
42+ this . cachedResult = undefined ;
43+ } ) ;
3144 }
3245
3346 /**
34- * Format a date.
47+ * Pipes a timestamp into a formatted date.
3548 *
3649 * @param timestamp Timestamp to format (in milliseconds). If not defined, use current time.
3750 * @param format Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
3851 * Defaults to strftimedaydatetime.
3952 * @param convert If true, convert the format from PHP to DayJS. Set it to false for DayJS formats.
4053 * @returns Formatted date.
4154 */
42- transform ( timestamp : string | number , format ?: string , convert ?: boolean ) : string {
55+ transform ( timestamp : string | number , format = 'strftimedaydatetime' , convert ?: boolean ) : string {
56+ if ( this . lastTimestamp !== timestamp || this . lastFormat !== format || this . lastConvert !== convert ) {
57+ this . lastTimestamp = timestamp ;
58+ this . lastFormat = format ;
59+ this . lastConvert = convert ;
60+ this . cachedResult = undefined ;
61+ }
62+
63+ if ( this . cachedResult === undefined ) {
64+ this . cachedResult = this . formatDate ( timestamp , format , convert ) ;
65+ }
66+
67+ return this . cachedResult ;
68+ }
69+
70+ /**
71+ * Format a date.
72+ *
73+ * @param timestamp Timestamp to format (in milliseconds). If not defined, use current time.
74+ * @param format Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
75+ * @param convert If true, convert the format from PHP to DayJS. Set it to false for DayJS formats.
76+ * @returns Formatted date.
77+ */
78+ protected formatDate ( timestamp : string | number , format : string , convert ?: boolean ) : string {
4379 timestamp = timestamp || Date . now ( ) ;
44- format = format || 'strftimedaydatetime' ;
4580
4681 if ( typeof timestamp === 'string' ) {
4782 // Convert the value to a number.
@@ -55,16 +90,23 @@ export class CoreFormatDatePipe implements PipeTransform {
5590 }
5691
5792 // Add "core." if needed.
58- if ( format . indexOf ( 'strf' ) === 0 || format . indexOf ( 'df' ) === 0 ) {
93+ if ( format . startsWith ( 'strf' ) || format . startsWith ( 'df' ) ) {
5994 format = `core.${ format } ` ;
6095 }
6196
6297 if ( convert === undefined ) {
6398 // Initialize convert param. Set it to false if it's a core.df format, set it to true otherwise.
64- convert = format . indexOf ( 'core.df' ) != 0 ;
99+ convert = ! format . startsWith ( 'core.df' ) ;
65100 }
66101
67102 return CoreTime . userDate ( timestamp , format , convert ) ;
68103 }
69104
105+ /**
106+ * @inheritdoc
107+ */
108+ ngOnDestroy ( ) : void {
109+ this . subscription . unsubscribe ( ) ;
110+ }
111+
70112}
0 commit comments