-
Notifications
You must be signed in to change notification settings - Fork 929
Move fieldFilter (free function) to Filter.create() #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8d40225
83d5fee
8a88cc8
1b83cd3
16c112a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -390,10 +390,39 @@ export class Query { | |
} | ||
} | ||
|
||
export interface Filter { | ||
matches(doc: Document): boolean; | ||
canonicalId(): string; | ||
isEqual(filter: Filter): boolean; | ||
export abstract class Filter { | ||
abstract matches(doc: Document): boolean; | ||
abstract canonicalId(): string; | ||
abstract isEqual(filter: Filter): boolean; | ||
|
||
/** | ||
* Creates a filter based on the provided arguments. | ||
*/ | ||
static create( | ||
field: FieldPath, | ||
op: RelationOp, | ||
value: FieldValue | ||
): Filter { | ||
if (value.isEqual(NullValue.INSTANCE)) { | ||
if (op !== RelationOp.EQUAL) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Invalid query. You can only perform equals ' + 'comparisons on null.' | ||
); | ||
} | ||
return new NullFilter(field); | ||
} else if (value.isEqual(DoubleValue.NAN)) { | ||
if (op !== RelationOp.EQUAL) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Invalid query. You can only perform equals ' + 'comparisons on NaN.' | ||
); | ||
} | ||
return new NanFilter(field); | ||
} else { | ||
return new RelationFilter(field, op, value); | ||
} | ||
} | ||
} | ||
|
||
export class RelationOp { | ||
|
@@ -434,12 +463,12 @@ export class RelationOp { | |
} | ||
} | ||
|
||
export class RelationFilter implements Filter { | ||
export class RelationFilter extends Filter { | ||
constructor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can omit constructors that match the super class constructor (here and everywhere).. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you need to keep them to ensure (in this case) field, op, and value are present? Or in other words, these ctors don't match the parent ctor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, yes, that's true. The constructors are much more powerful at second glance. |
||
public field: FieldPath, | ||
public op: RelationOp, | ||
public value: FieldValue | ||
) {} | ||
) { super(); } | ||
|
||
matches(doc: Document): boolean { | ||
if (this.field.isKeyField()) { | ||
|
@@ -528,8 +557,8 @@ export class RelationFilter implements Filter { | |
/** | ||
* Filter that matches 'null' values. | ||
*/ | ||
export class NullFilter implements Filter { | ||
constructor(public field: FieldPath) {} | ||
export class NullFilter extends Filter { | ||
constructor(public field: FieldPath) { super(); } | ||
|
||
matches(doc: Document): boolean { | ||
const val = doc.field(this.field); | ||
|
@@ -556,8 +585,8 @@ export class NullFilter implements Filter { | |
/** | ||
* Filter that matches 'NaN' values. | ||
*/ | ||
export class NanFilter implements Filter { | ||
constructor(public field: FieldPath) {} | ||
export class NanFilter extends Filter { | ||
constructor(public field: FieldPath) { super(); } | ||
|
||
matches(doc: Document): boolean { | ||
const val = doc.field(this.field).value(); | ||
|
@@ -581,35 +610,6 @@ export class NanFilter implements Filter { | |
} | ||
} | ||
|
||
/** | ||
* Creates a filter based on the provided arguments. | ||
*/ | ||
export function fieldFilter( | ||
field: FieldPath, | ||
op: RelationOp, | ||
value: FieldValue | ||
): Filter { | ||
if (value.isEqual(NullValue.INSTANCE)) { | ||
if (op !== RelationOp.EQUAL) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Invalid query. You can only perform equals ' + 'comparisons on null.' | ||
); | ||
} | ||
return new NullFilter(field); | ||
} else if (value.isEqual(DoubleValue.NAN)) { | ||
if (op !== RelationOp.EQUAL) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Invalid query. You can only perform equals ' + 'comparisons on NaN.' | ||
); | ||
} | ||
return new NanFilter(field); | ||
} else { | ||
return new RelationFilter(field, op, value); | ||
} | ||
} | ||
|
||
/** | ||
* The direction of sorting in an order by. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind removing the extraneous string concatenation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.