-
Notifications
You must be signed in to change notification settings - Fork 934
Add small Proto helpers #2753
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
Add small Proto helpers #2753
Conversation
a9cc1f2
to
3ee49d5
Compare
3ee49d5
to
69ffaf0
Compare
|
||
/** Returns true if `value` is NaN. */ | ||
export function isNanValue(value?: api.Value | null): boolean { | ||
return !!value && isNaN(Number(value.doubleValue)); |
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.
This doesn't actually test that value
is a double. Should this be isDouble(value) && isNaN(Number(value.doubleValue));
?
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.
Ugh JavaScript:
isNaN(Number(null)) => false
isNaN(Number(undefined)) => true
So it looks like we need to test here that value.doubleValue
is set.
@@ -539,3 +539,43 @@ export function normalizeByteString(blob: string | Uint8Array): ByteString { | |||
return ByteString.fromUint8Array(blob); | |||
} | |||
} | |||
|
|||
/** Returns true if `value` is an IntegerValue. */ | |||
export function isInteger(value?: api.Value | null): boolean { |
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.
If we defined an interface like
exporter interface IntegerValue {
integerValue: number;
}
Then we could make this a type guard:
export function isInteger(value?: api.Value | null): value is IntegerValue {
return !!value && 'integerValue' in value;
}
This PR doesn't include any uses of these functions, so it's not clear that this would necessarily be a win, but it seems like this could be helpful.
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.
I updated the PR to allow us to make asserts on the input type, but I kept it in the method declarations for now and did not plumb through separate types. This should allow us to get rid of bangs for future usages.
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.
LGTM
export function isNanValue( | ||
value?: api.Value | null | ||
): value is { doubleValue: 'NaN' | number } { | ||
return !!value && isDouble(value) && isNaN(Number(value.doubleValue)); |
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.
isDouble
already tests !!value
so you can drop it here.
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.
Done
Small PR with some helpers that will be used in NumericIncrementTransforms and other places.