Skip to content

Type issue in RLEEncoder #448

Open
Open
@Brian-McBride

Description

Looking through the code, there is this line (repeated a few times)

https://github.com/automerge/automerge/blob/08a456cc2ddc79d8c2f35953db0f1fd8ed418d0f/backend/encoding.js#L695

  • We have sum which is a number (defined above as let sum = 0.
  • sumShift can be undefined. This can be solved earlier in the code with const { count, sumValues, sumShift = 0 } = options;
  • firstValue is returned from decoder.readValue() that has way too many types: string, number, null, undefined, void

A little cleanup in the decoder reduces the return types to string | number | null filtering down to the line in question where firstValue can be of a number or string type.

anyString >>> anyNumber has a lot of side effects. It really just depends if the value can be cast to a number. If it can be cast, it will work as expected. If it can't it will return 0. Returning zero isn't probably correct as sum will not be incremented as a number would. Worse though, if sum += sumShift is falsey, then we end up with sum adding a string to 0. Now we aren't a number but 0some-text-string.

Am I wrong here? Or does the RLEEncoder basically incorrectly generate the sum of strings when using sumValues?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions