googleapis / googleapis/google-cloud-node
FR: Firestore Timestamp should have a comparator method
- Dominant language
- TypeScript
- Stars
- 3.2k
- Forks
- 712
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 99
Description
The JS/TS API for Firestore's Timestamp (both web client [Timestamp](https://firebase.google.com/docs/reference/js/firestore_.timestamp) and node backend [Timestamp](https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/timestamp)) do not include a comparator function. This makes sorting accurately by Timestamp rather painful, as you have to write a function that manually compares both seconds and nanos. An easy workaround is to use `toMillis`, but it's lossy on nanos:
```
array.sort((a, b) => b.ts.toMillis() - a.ts.toMillis())
```
Instead, it would be great if there was a comparator method provided in the same way as the Firestore [Timestamp.compareTo()](https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.Timestamp#com_google_cloud_Timestamp_compareTo_com_google_cloud_Timestamp_) provided by the Java SDK.
```
array.sort((a, b) => b.ts.compareTo(a.ts))
```
Related issue for the Firebase web client SDK: https://github.com/firebase/firebase-js-sdk/issues/7711
Contributor guide
Assessment
This issue has not been assessed yet.