Skip to content

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

Merged
merged 17 commits into from
Mar 19, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

Small PR with some helpers that will be used in NumericIncrementTransforms and other places.


/** Returns true if `value` is NaN. */
export function isNanValue(value?: api.Value | null): boolean {
return !!value && isNaN(Number(value.doubleValue));
Copy link
Contributor

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));?

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Mar 18, 2020
Copy link
Contributor

@wilhuff wilhuff left a 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));
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Mar 18, 2020
@schmidt-sebastian schmidt-sebastian changed the base branch from mrschmidt/valuesrename to mrschmidt/rewritefieldvalue March 18, 2020 23:44
@schmidt-sebastian schmidt-sebastian merged commit 99618a1 into mrschmidt/rewritefieldvalue Mar 19, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/smallhelpers branch March 26, 2020 03:05
@firebase firebase locked and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants