Skip to content
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

Update entropy types #1915

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
21 changes: 18 additions & 3 deletions src/util/entropyCreateStateFromJsons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,24 @@ function _frame(
genomeLength: number,
positiveStrand: boolean,
): Frame {
return (positiveStrand ?
(start+phase-1)%3 :
Math.abs((end-phase-genomeLength)%3)) as Frame;
return positiveStrand ?
_mod3(start+phase-1) :
_mod3(end-phase-genomeLength);
}

/**
* Type-safe modulo operation. Return value is unsigned (i.e. non-negative).
*/
function _mod3(n: number): 0 | 1 | 2 {
if (!Number.isInteger(n)) {
throw new Error(`${n} is not an integer.`);
}

/* TypeScript cannot infer the exact range of values from a modulo operation,
* so it is manually provided.
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return (Math.abs(n) % 3) as 0 | 1 | 2
Copy link
Member

Choose a reason for hiding this comment

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

For what it's worth, I find these changes kind of unfortunate (I have no doubt they are just to appease tsc). I'd prefer expanding out the ternary return into a longer function with more comments than additional small functions. I like the cast the best!

The lack of an integer type is pretty frustrating (JS, not TS, to be fair). If the numbers supplied to this function aren't integers we've got bigger problems in Auspice than a floating point frame!

}

/**
Expand Down
Loading