Skip to content

Commit f23c167

Browse files
Adding isEqual to the public API (#140)
1 parent 25067bb commit f23c167

16 files changed

+734
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@google-cloud/common": "^0.16.0",
5454
"@google-cloud/common-grpc": "^0.5.3",
5555
"bun": "^0.0.12",
56+
"deep-equal": "^1.0.1",
5657
"extend": "^3.0.1",
5758
"functional-red-black-tree": "^1.0.1",
5859
"google-gax": "^0.14.3",

src/document.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'use strict';
1818

1919
const assert = require('assert');
20+
const deepEqual = require('deep-equal');
2021
const is = require('is');
2122

2223
const path = require('./path');
@@ -132,6 +133,21 @@ class GeoPoint {
132133
} }`;
133134
}
134135

136+
/**
137+
* Returns true if this `GeoPoint` is equal to the provided value.
138+
*
139+
* @param {*} other The value to compare against.
140+
* @return {boolean} true if this `GeoPoint` is equal to the provided value.
141+
*/
142+
isEqual(other) {
143+
return (
144+
this === other ||
145+
(is.instanceof(other, GeoPoint) &&
146+
this.latitude === other.latitude &&
147+
this.longitude === other.longitude)
148+
);
149+
}
150+
135151
/**
136152
* Converts the GeoPoint to a google.type.LatLng proto.
137153
* @private
@@ -600,6 +616,23 @@ class DocumentSnapshot {
600616
};
601617
}
602618

619+
/**
620+
* Returns true if the document's data and path in this `DocumentSnapshot` is
621+
* equal to the provided value.
622+
*
623+
* @param {*} other The value to compare against.
624+
* @return {boolean} true if this `DocumentSnapshot` is equal to the provided
625+
* value.
626+
*/
627+
isEqual(other) {
628+
return (
629+
this === other ||
630+
(is.instance(other, DocumentSnapshot) &&
631+
this._ref.isEqual(other._ref) &&
632+
deepEqual(this._fieldsProto, other._fieldsProto, {strict: true}))
633+
);
634+
}
635+
603636
/**
604637
* Converts a Google Protobuf timestamp to an ISO 8601 string.
605638
*

src/field-value.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ class FieldValue {
8080
static serverTimestamp() {
8181
return SERVER_TIMESTAMP_SENTINEL;
8282
}
83+
84+
/**
85+
* Returns true if this `FieldValue` is equal to the provided value.
86+
*
87+
* @param {*} other The value to compare against.
88+
* @return {boolean} true if this `FieldValue` is equal to the provided value.
89+
*/
90+
isEqual(other) {
91+
return this === other;
92+
}
8393
}
8494

8595
DELETE_SENTINEL = new FieldValue();

src/path.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ class Path {
194194
toArray() {
195195
return this.segments.slice();
196196
}
197+
198+
/**
199+
* Returns true if this `Path` is equal to the provided value.
200+
*
201+
* @private
202+
* @param {*} other The value to compare against.
203+
* @return {boolean} true if this `Path` is equal to the provided value.
204+
*/
205+
isEqual(other) {
206+
return (
207+
this === other ||
208+
(is.instanceof(other, this.constructor) && this.compareTo(other) === 0)
209+
);
210+
}
197211
}
198212

199213
/**
@@ -562,6 +576,16 @@ class FieldPath extends Path {
562576
construct(segments) {
563577
return new FieldPath(segments);
564578
}
579+
580+
/**
581+
* Returns true if this `FieldPath` is equal to the provided value.
582+
*
583+
* @param {*} other The value to compare against.
584+
* @return {boolean} true if this `FieldPath` is equal to the provided value.
585+
*/
586+
isEqual(other) {
587+
return super.isEqual(other);
588+
}
565589
}
566590

567591
/**

0 commit comments

Comments
 (0)