Page MenuHomePhabricator

[web] Keyboard support for typeahead [8/13] - Add positive modulo function
ClosedPublic

Authored by przemek on Dec 27 2022, 4:56 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Dec 2, 3:07 PM
Unknown Object (File)
Sat, Nov 30, 3:03 PM
Unknown Object (File)
Sat, Nov 30, 2:25 PM
Unknown Object (File)
Nov 27 2024, 9:53 PM
Unknown Object (File)
Nov 27 2024, 5:35 PM
Unknown Object (File)
Nov 13 2024, 5:24 AM
Unknown Object (File)
Nov 11 2024, 2:04 PM
Unknown Object (File)
Nov 5 2024, 4:25 AM

Details

Summary

Added function for calculating positive value of number mod base.
It will be needed in further diffs to wrap value back to 0 when it overflows max value (and vice verse)

Test Plan

Typeahead without keyboard support works.
Final tests performed in last diffs.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

What do you mean that it gets the positive modulo? For number = 3 and base = -2 it returns -1

web/utils/typeahead-utils.js
152 ↗(On Diff #20161)

Nit: I don't think it's called "base", but "modulus"
source 1, source 2

Responding to both comments. I should probably call it least positive residue as defined here: https://en.wikipedia.org/wiki/Modulo_operation.
We are not concerned with negative modulus as we just need that function to "wrap" numbers between 0 and <MODULUS - 1>.

inka added a reviewer: tomek.

Since this function is exported, someone might try to use it in the future somewhere else with a negative modulus. I think it'd be good to at least leave a comment that in such case the result will not be positive. Otherwise the code is misleading. Other that that, I agree that least positive residue is better.
That said, I don't want to block you on my opinion on naming variables. So I'm accepting, and asking other reviewers to express their opinion on whether this matters at all.

You're right I'm updating the diff right now. Sorry for that, I meant to do it, but only responded to comment.

Renamed function and added comment.

web/utils/typeahead-utils.js
153 ↗(On Diff #20413)

I think it's a typo?

tomek requested changes to this revision.Dec 30 2022, 9:17 AM

It seems like this isn't typeahead-specific. Can we move it to a different place, probably in lib?

I think we need to add a couple of tests to make sure this is correct.

This revision now requires changes to proceed.Dec 30 2022, 9:17 AM

Moved function to lib and added some simple tests. Made sure they pass.

tomek requested changes to this revision.Jan 4 2023, 2:42 AM

Could you also add some tests where number is negative?

lib/utils/math-utils.js
3 ↗(On Diff #20511)

It might be a little confusing to name a parameter like a type

This revision now requires changes to proceed.Jan 4 2023, 2:42 AM
This revision is now accepted and ready to land.Jan 4 2023, 2:43 AM

Changed name to dividend as it is the thing that is divided.
Added test for negative dividend.