Skip to content

Commit 799bc96

Browse files
Adding Proto-based equality and comparison (#2678)
1 parent 6dfb8b8 commit 799bc96

File tree

5 files changed

+435
-113
lines changed

5 files changed

+435
-113
lines changed

packages/firestore/src/model/field_value.ts

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import { GeoPoint } from '../api/geo_point';
2020
import { Timestamp } from '../api/timestamp';
2121
import { DatabaseId } from '../core/database_info';
2222
import { assert, fail } from '../util/assert';
23-
import { primitiveComparator } from '../util/misc';
23+
import {
24+
numericComparator,
25+
numericEquals,
26+
primitiveComparator
27+
} from '../util/misc';
2428
import { DocumentKey } from './document_key';
2529
import { FieldMask } from './mutation';
2630
import { FieldPath } from './path';
@@ -244,40 +248,6 @@ export abstract class NumberValue extends FieldValue {
244248
}
245249
}
246250

247-
/** Utility function to compare doubles (using Firestore semantics for NaN). */
248-
function numericComparator(left: number, right: number): number {
249-
if (left < right) {
250-
return -1;
251-
} else if (left > right) {
252-
return 1;
253-
} else if (left === right) {
254-
return 0;
255-
} else {
256-
// one or both are NaN.
257-
if (isNaN(left)) {
258-
return isNaN(right) ? 0 : -1;
259-
} else {
260-
return 1;
261-
}
262-
}
263-
}
264-
265-
/**
266-
* Utility function to check numbers for equality using Firestore semantics
267-
* (NaN === NaN, -0.0 !== 0.0).
268-
*/
269-
function numericEquals(left: number, right: number): boolean {
270-
// Implemented based on Object.is() polyfill from
271-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
272-
if (left === right) {
273-
// +0 != -0
274-
return left !== 0 || 1 / left === 1 / right;
275-
} else {
276-
// NaN == NaN
277-
return left !== left && right !== right;
278-
}
279-
}
280-
281251
export class IntegerValue extends NumberValue {
282252
isEqual(other: FieldValue): boolean {
283253
// NOTE: DoubleValue and IntegerValue instances may compareTo() the same,

0 commit comments

Comments
 (0)