Skip to content

Commit a90acfa

Browse files
Remove custom implementations for Number methods (#2752)
1 parent 148db34 commit a90acfa

File tree

4 files changed

+19
-62
lines changed

4 files changed

+19
-62
lines changed

packages/firestore/src/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type MutationBatchState = 'pending' | 'acknowledged' | 'rejected';
3838
* primarily used by the View / EventManager code to change their behavior while
3939
* offline (e.g. get() calls shouldn't wait for data from the server and
4040
* snapshot events should set metadata.isFromCache=true).
41-
*
41+
*
4242
* The string values should not be changed since they are persisted in
4343
* WebStorage.
4444
*/

packages/firestore/src/util/types.ts

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Untyped Number alias we can use to check for ES6 methods / properties.
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
const NumberAsAny = Number as any;
21-
2218
// An Object whose keys and values are strings.
2319
export interface StringMap {
2420
[key: string]: string;
2521
}
2622

27-
/**
28-
* Minimum safe integer in Javascript because of floating point precision.
29-
* Added to not rely on ES6 features.
30-
*/
31-
export const MIN_SAFE_INTEGER: number =
32-
NumberAsAny.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
33-
34-
/**
35-
* Maximum safe integer in Javascript because of floating point precision.
36-
* Added to not rely on ES6 features.
37-
*/
38-
export const MAX_SAFE_INTEGER: number =
39-
NumberAsAny.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
40-
41-
/**
42-
* Returns whether an number is an integer, uses native implementation if
43-
* available.
44-
* Added to not rely on ES6 features.
45-
* @param value The value to test for being an integer
46-
*/
47-
export const isInteger: (value: unknown) => boolean =
48-
NumberAsAny.isInteger ||
49-
(value =>
50-
typeof value === 'number' &&
51-
isFinite(value) &&
52-
Math.floor(value) === value);
53-
5423
/**
5524
* Returns whether a variable is either undefined or null.
5625
*/
@@ -64,19 +33,9 @@ export function isNullOrUndefined(value: unknown): boolean {
6433
*/
6534
export function isSafeInteger(value: unknown): boolean {
6635
return (
67-
isInteger(value) &&
68-
(value as number) <= MAX_SAFE_INTEGER &&
69-
(value as number) >= MIN_SAFE_INTEGER
36+
typeof value === 'number' &&
37+
Number.isInteger(value) &&
38+
(value as number) <= Number.MAX_SAFE_INTEGER &&
39+
(value as number) >= Number.MIN_SAFE_INTEGER
7040
);
7141
}
72-
73-
/**
74-
* Safely checks if the number is NaN.
75-
*/
76-
export function safeIsNaN(value: unknown): boolean {
77-
if (NumberAsAny.IsNaN) {
78-
return NumberAsAny.IsNaN(value);
79-
} else {
80-
return typeof value === 'number' && isNaN(value);
81-
}
82-
}

packages/firestore/test/unit/model/field_value.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { GeoPoint } from '../../../src/api/geo_point';
2020
import { Timestamp } from '../../../src/api/timestamp';
2121
import * as fieldValue from '../../../src/model/field_value';
2222
import { primitiveComparator } from '../../../src/util/misc';
23-
import * as typeUtils from '../../../src/util/types';
2423
import {
2524
blob,
2625
dbId,
@@ -40,12 +39,12 @@ describe('FieldValue', () => {
4039

4140
it('can parse integers', () => {
4241
const primitiveValues = [
43-
typeUtils.MIN_SAFE_INTEGER,
42+
Number.MIN_SAFE_INTEGER,
4443
-1,
4544
0,
4645
1,
4746
2,
48-
typeUtils.MAX_SAFE_INTEGER
47+
Number.MAX_SAFE_INTEGER
4948
];
5049
const values = primitiveValues.map(v => wrap(v));
5150

@@ -62,10 +61,10 @@ describe('FieldValue', () => {
6261

6362
it('can parse doubles', () => {
6463
const primitiveValues = [
65-
typeUtils.MIN_SAFE_INTEGER - 1,
64+
Number.MIN_SAFE_INTEGER - 1,
6665
-1.1,
6766
0.1,
68-
typeUtils.MAX_SAFE_INTEGER + 1,
67+
Number.MAX_SAFE_INTEGER + 1,
6968
NaN,
7069
Infinity,
7170
-Infinity
@@ -438,8 +437,8 @@ describe('FieldValue', () => {
438437
[wrap(NaN)],
439438
[wrap(-Infinity)],
440439
[wrap(-Number.MAX_VALUE)],
441-
[wrap(typeUtils.MIN_SAFE_INTEGER - 1)],
442-
[wrap(typeUtils.MIN_SAFE_INTEGER)],
440+
[wrap(Number.MIN_SAFE_INTEGER - 1)],
441+
[wrap(Number.MIN_SAFE_INTEGER)],
443442
[wrap(-1.1)],
444443
// Integers and Doubles order the same.
445444
[new fieldValue.IntegerValue(-1), new fieldValue.DoubleValue(-1)],
@@ -453,8 +452,8 @@ describe('FieldValue', () => {
453452
[wrap(Number.MIN_VALUE)],
454453
[new fieldValue.IntegerValue(1), new fieldValue.DoubleValue(1)],
455454
[wrap(1.1)],
456-
[wrap(typeUtils.MAX_SAFE_INTEGER)],
457-
[wrap(typeUtils.MAX_SAFE_INTEGER + 1)],
455+
[wrap(Number.MAX_SAFE_INTEGER)],
456+
[wrap(Number.MAX_SAFE_INTEGER + 1)],
458457
[wrap(Infinity)],
459458

460459
// timestamps

packages/firestore/test/unit/remote/node/serializer.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ import {
6262
import { Code, FirestoreError } from '../../../../src/util/error';
6363
import { Indexable } from '../../../../src/util/misc';
6464
import * as obj from '../../../../src/util/obj';
65-
import * as types from '../../../../src/util/types';
6665
import { addEqualityMatcher } from '../../../util/equality_matcher';
6766
import {
6867
bound,
68+
byteStringFromString,
6969
dbId,
7070
deletedDoc,
7171
deleteMutation,
@@ -81,8 +81,7 @@ import {
8181
transformMutation,
8282
version,
8383
wrap,
84-
wrapObject,
85-
byteStringFromString
84+
wrapObject
8685
} from '../../../util/helpers';
8786
import { ByteString } from '../../../../src/util/byte_string';
8887

@@ -196,13 +195,13 @@ describe('Serializer', () => {
196195

197196
it('converts IntegerValue', () => {
198197
const examples = [
199-
types.MIN_SAFE_INTEGER,
198+
Number.MIN_SAFE_INTEGER,
200199
-100,
201200
-1,
202201
0,
203202
1,
204203
100,
205-
types.MAX_SAFE_INTEGER
204+
Number.MAX_SAFE_INTEGER
206205
];
207206
for (const example of examples) {
208207
verifyFieldValueRoundTrip({
@@ -501,11 +500,11 @@ describe('Serializer', () => {
501500

502501
it('converts a long key', () => {
503502
const actual = s.toName(
504-
key('users/' + types.MAX_SAFE_INTEGER + '/profiles/primary')
503+
key('users/' + Number.MAX_SAFE_INTEGER + '/profiles/primary')
505504
);
506505
expect(actual).to.deep.equal(
507506
'projects/p/databases/d/documents/users/' +
508-
types.MAX_SAFE_INTEGER.toString() +
507+
Number.MAX_SAFE_INTEGER.toString() +
509508
'/profiles/primary'
510509
);
511510
});

0 commit comments

Comments
 (0)