@@ -452,10 +452,10 @@ export class Query {
452
452
* Makes sure a document is within the bounds, if provided.
453
453
*/
454
454
private matchesBounds ( doc : Document ) : boolean {
455
- if ( this . startAt && ! this . startAt . sortsBeforeDocument ( this . orderBy , doc ) ) {
455
+ if ( this . startAt && ! sortsBeforeDocument ( this . startAt , this . orderBy , doc ) ) {
456
456
return false ;
457
457
}
458
- if ( this . endAt && this . endAt . sortsBeforeDocument ( this . orderBy , doc ) ) {
458
+ if ( this . endAt && sortsBeforeDocument ( this . endAt , this . orderBy , doc ) ) {
459
459
return false ;
460
460
}
461
461
return true ;
@@ -732,73 +732,80 @@ export const enum Direction {
732
732
*/
733
733
export class Bound {
734
734
constructor ( readonly position : api . Value [ ] , readonly before : boolean ) { }
735
+ }
735
736
736
- canonicalId ( ) : string {
737
- // TODO(b/29183165): Make this collision robust.
738
- return `${ this . before ? 'b' : 'a' } :${ this . position
739
- . map ( p => canonicalId ( p ) )
740
- . join ( ',' ) } `;
741
- }
737
+ export function canonifyBound ( bound : Bound ) : string {
738
+ // TODO(b/29183165): Make this collision robust.
739
+ return `${ bound . before ? 'b' : 'a' } :${ bound . position
740
+ . map ( p => canonicalId ( p ) )
741
+ . join ( ',' ) } `;
742
+ }
742
743
743
- /**
744
- * Returns true if a document sorts before a bound using the provided sort
745
- * order.
746
- */
747
- sortsBeforeDocument ( orderBy : OrderBy [ ] , doc : Document ) : boolean {
748
- debugAssert (
749
- this . position . length <= orderBy . length ,
750
- "Bound has more components than query's orderBy"
751
- ) ;
752
- let comparison = 0 ;
753
- for ( let i = 0 ; i < this . position . length ; i ++ ) {
754
- const orderByComponent = orderBy [ i ] ;
755
- const component = this . position [ i ] ;
756
- if ( orderByComponent . field . isKeyField ( ) ) {
757
- debugAssert (
758
- isReferenceValue ( component ) ,
759
- 'Bound has a non-key value where the key path is being used.'
760
- ) ;
761
- comparison = DocumentKey . comparator (
762
- DocumentKey . fromName ( component . referenceValue ) ,
763
- doc . key
764
- ) ;
765
- } else {
766
- const docValue = doc . field ( orderByComponent . field ) ;
767
- debugAssert (
768
- docValue !== null ,
769
- 'Field should exist since document matched the orderBy already.'
770
- ) ;
771
- comparison = valueCompare ( component , docValue ) ;
772
- }
773
- if ( orderByComponent . dir === Direction . DESCENDING ) {
774
- comparison = comparison * - 1 ;
775
- }
776
- if ( comparison !== 0 ) {
777
- break ;
778
- }
744
+ /**
745
+ * Returns true if a document sorts before a bound using the provided sort
746
+ * order.
747
+ */
748
+ export function sortsBeforeDocument (
749
+ bound : Bound ,
750
+ orderBy : OrderBy [ ] ,
751
+ doc : Document
752
+ ) : boolean {
753
+ debugAssert (
754
+ bound . position . length <= orderBy . length ,
755
+ "Bound has more components than query's orderBy"
756
+ ) ;
757
+ let comparison = 0 ;
758
+ for ( let i = 0 ; i < bound . position . length ; i ++ ) {
759
+ const orderByComponent = orderBy [ i ] ;
760
+ const component = bound . position [ i ] ;
761
+ if ( orderByComponent . field . isKeyField ( ) ) {
762
+ debugAssert (
763
+ isReferenceValue ( component ) ,
764
+ 'Bound has a non-key value where the key path is being used.'
765
+ ) ;
766
+ comparison = DocumentKey . comparator (
767
+ DocumentKey . fromName ( component . referenceValue ) ,
768
+ doc . key
769
+ ) ;
770
+ } else {
771
+ const docValue = doc . field ( orderByComponent . field ) ;
772
+ debugAssert (
773
+ docValue !== null ,
774
+ 'Field should exist since document matched the orderBy already.'
775
+ ) ;
776
+ comparison = valueCompare ( component , docValue ) ;
777
+ }
778
+ if ( orderByComponent . dir === Direction . DESCENDING ) {
779
+ comparison = comparison * - 1 ;
780
+ }
781
+ if ( comparison !== 0 ) {
782
+ break ;
779
783
}
780
- return this . before ? comparison <= 0 : comparison < 0 ;
781
784
}
785
+ return bound . before ? comparison <= 0 : comparison < 0 ;
786
+ }
782
787
783
- isEqual ( other : Bound | null ) : boolean {
784
- if ( other === null ) {
785
- return false ;
786
- }
787
- if (
788
- this . before !== other . before ||
789
- this . position . length !== other . position . length
790
- ) {
788
+ export function boundEquals ( left : Bound | null , right : Bound | null ) : boolean {
789
+ if ( left === null ) {
790
+ return right === null ;
791
+ } else if ( right === null ) {
792
+ return false ;
793
+ }
794
+
795
+ if (
796
+ left . before !== right . before ||
797
+ left . position . length !== right . position . length
798
+ ) {
799
+ return false ;
800
+ }
801
+ for ( let i = 0 ; i < left . position . length ; i ++ ) {
802
+ const thisPosition = left . position [ i ] ;
803
+ const otherPosition = right . position [ i ] ;
804
+ if ( ! valueEquals ( thisPosition , otherPosition ) ) {
791
805
return false ;
792
806
}
793
- for ( let i = 0 ; i < this . position . length ; i ++ ) {
794
- const thisPosition = this . position [ i ] ;
795
- const otherPosition = other . position [ i ] ;
796
- if ( ! valueEquals ( thisPosition , otherPosition ) ) {
797
- return false ;
798
- }
799
- }
800
- return true ;
801
807
}
808
+ return true ;
802
809
}
803
810
804
811
/**
0 commit comments