11import { PipeTransform , Pipe , Injectable , EventEmitter , OnDestroy , ChangeDetectorRef } from '@angular/core' ;
22import { TranslateService , LangChangeEvent , TranslationChangeEvent } from './translate.service' ;
3+ import { equals , isDefined } from "./util" ;
34
45@Injectable ( )
56@Pipe ( {
@@ -16,60 +17,6 @@ export class TranslatePipe implements PipeTransform, OnDestroy {
1617 constructor ( private translate : TranslateService , private _ref : ChangeDetectorRef ) {
1718 }
1819
19- /* tslint:disable */
20- /**
21- * @name equals
22- *
23- * @description
24- * Determines if two objects or two values are equivalent.
25- *
26- * Two objects or values are considered equivalent if at least one of the following is true:
27- *
28- * * Both objects or values pass `===` comparison.
29- * * Both objects or values are of the same type and all of their properties are equal by
30- * comparing them with `equals`.
31- *
32- * @param {* } o1 Object or value to compare.
33- * @param {* } o2 Object or value to compare.
34- * @returns {boolean } True if arguments are equal.
35- */
36- private equals ( o1 : any , o2 : any ) : boolean {
37- if ( o1 === o2 ) return true ;
38- if ( o1 === null || o2 === null ) return false ;
39- if ( o1 !== o1 && o2 !== o2 ) return true ; // NaN === NaN
40- let t1 = typeof o1 , t2 = typeof o2 , length : number , key : any , keySet : any ;
41- if ( t1 == t2 && t1 == 'object' ) {
42- if ( Array . isArray ( o1 ) ) {
43- if ( ! Array . isArray ( o2 ) ) return false ;
44- if ( ( length = o1 . length ) == o2 . length ) {
45- for ( key = 0 ; key < length ; key ++ ) {
46- if ( ! this . equals ( o1 [ key ] , o2 [ key ] ) ) return false ;
47- }
48- return true ;
49- }
50- } else {
51- if ( Array . isArray ( o2 ) ) {
52- return false ;
53- }
54- keySet = Object . create ( null ) ;
55- for ( key in o1 ) {
56- if ( ! this . equals ( o1 [ key ] , o2 [ key ] ) ) {
57- return false ;
58- }
59- keySet [ key ] = true ;
60- }
61- for ( key in o2 ) {
62- if ( ! ( key in keySet ) && typeof o2 [ key ] !== 'undefined' ) {
63- return false ;
64- }
65- }
66- return true ;
67- }
68- }
69- return false ;
70- }
71- /* tslint:enable */
72-
7320 updateValue ( key : string , interpolateParams ?: Object , translations ?: any ) : void {
7421 let onTranslation = ( res : string ) => {
7522 this . value = res !== undefined ? res : key ;
@@ -91,13 +38,14 @@ export class TranslatePipe implements PipeTransform, OnDestroy {
9138 if ( ! query || query . length === 0 ) {
9239 return query ;
9340 }
41+
9442 // if we ask another time for the same key, return the last value
95- if ( this . equals ( query , this . lastKey ) && this . equals ( args , this . lastParams ) ) {
43+ if ( equals ( query , this . lastKey ) && equals ( args , this . lastParams ) ) {
9644 return this . value ;
9745 }
9846
9947 let interpolateParams : Object ;
100- if ( args . length && args [ 0 ] !== null ) {
48+ if ( isDefined ( args [ 0 ] ) && args . length ) {
10149 if ( typeof args [ 0 ] === 'string' && args [ 0 ] . length ) {
10250 // we accept objects written in the template such as {n:1}, {'n':1}, {n:'v'}
10351 // which is why we might need to change it to real JSON objects such as {"n":1} or {"n":"v"}
0 commit comments