-
Notifications
You must be signed in to change notification settings - Fork 218
Handle Firestore timestamp values with precision to the second #123
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
Conversation
src/encoder.ts
Outdated
| nanoString = '000000000'; | ||
| } else { | ||
| nanoString = timeString.substring(20, timeString.length - 1); | ||
| nanoString += _.reduce(new Array(9 - nanoString.length), i => {return i + '0';}, ''); |
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.
Woah... String -> array -> reduce -> parseInt? It may be the c programmer in me, but string manipulation of numbers feels really wonky.
why not something like
let nanos: 0;
if (timeString.length > 20) {
const nanoString = timeString.substring(20, timeString.length - 1);
const trailingZeros = 9 - nanoString.length;
nanos = parseInt(nanoString, 10) * Math.pow(10, trailingZeroes);
}|
@inlined That is way cleaner, thank you. |
src/encoder.ts
Outdated
| } | ||
| let proto = { | ||
|
|
||
| return { |
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.
You can also say return {seconds, nanos}
It felt weird to me at first, but it starts to feel pretty natural and expressive. It also encourages you to use variable names as they'll appear in your return values/API calls which tends to lead to more readable code.
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.
nifty! thanks for the tip!
Description
Fixes bug where timestamp values that didn't have millisecond or nanosecond precision resulted in "Invalid date" when event.data.data() is called.
Code sample