Skip to content

Conversation

@HauklandJ
Copy link
Contributor

@HauklandJ HauklandJ commented Nov 10, 2025

Description

For a discussion of the name of the function, see https://math.stackexchange.com/questions/5107917/is-there-a-term-for-a-xmoda

Related Issue(s)

  • closes #{issue number}

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
    • Creator of the original issue (or service owner) has been contacted for manual testing (or will be contacted when released in alpha)
    • No testing done/necessary
  • Automated tests
    • Unit test(s) have been added/updated
    • Cypress E2E test(s) have been added/updated
    • No automatic tests are needed here (no functional changes/additions)
    • I want someone to help me make some tests
  • UU/WCAG (follow these guidelines until we have our own)
    • I have tested with a screen reader/keyboard navigation/automated wcag validator
    • No testing done/necessary (no DOM/visual changes)
    • I want someone to help me perform accessibility testing
  • User documentation @ altinn-studio-docs
    • Has been added/updated
    • No functionality has been changed/added, so no documentation is needed
    • I will do that later/have created an issue
  • Support in Altinn Studio
    • Issue(s) created for support in Studio
    • This change/feature does not require any changes to Altinn Studio
  • Sprint board
    • The original issue (or this PR itself) has been added to the Team Apps project and to the current sprint board
    • I don't have permissions to do that, please help me out
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping
    • I don't have permissions to add labels, please help me out

Summary by CodeRabbit

  • Refactor
    • Algorithmic improvements to organisation and person lookup validation for more robust, consistent check‑digit verification and greater reliability.
    • No changes to external behavior or public APIs — validation outcomes, interfaces and user-facing behavior remain unchanged.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 10, 2025

📝 Walkthrough

Walkthrough

Replaces mod11-based check-digit logic in OrganisationLookup and PersonLookup validation modules with a modular additive-inverse approach; updates digit extraction and weight application. Public function names and signatures are unchanged.

Changes

Cohort / File(s) Summary
Organisation validation update
src/layout/OrganisationLookup/validation.ts
Replaced fixed-digit destructuring with indexed digit array extraction and derived orgnr_digits; replaced hard-coded weight destructuring with a weights array and loop to compute the weighted sum; swapped previous mod11 computation for a modularAdditiveInverse-based check-digit calculation and comparison to the last digit (k1); preserved exported function signatures.
Person validation update
src/layout/PersonLookup/validation.ts
Replaced direct last-digit access with digits.at(-2) / digits.at(-1) extraction; refactored K1/K2 calculations to compute expected values via modularAdditiveInverse(..., 11) (followed by % 11 where applied), removing prior special-case mod11 branches while keeping validation comparisons and public signatures intact.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Review focus:
    • correctness and boundary behavior of modularAdditiveInverse vs. previous mod11 logic (especially how 0, 10, 11 were handled before)
    • digit extraction changes (.at(-2) / .at(-1) and array indexing) for short or malformed inputs
    • weights loop correctness and any off-by-one errors in weighted-sum computation

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete. It lacks detail about the actual changes and only provides a link reference. Most verification/QA sections are presented but unchecked, and no concrete issue number is specified. Expand the description with specific details about the refactoring changes, check relevant verification boxes, provide a concrete issue number, and clarify which testing was performed.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main changes: refactoring identifier validation logic for readability and renaming a function (modularAdditiveInverse).
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/function-name

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00d00b2 and 2ae5527.

📒 Files selected for processing (2)
  • src/layout/OrganisationLookup/validation.ts (1 hunks)
  • src/layout/PersonLookup/validation.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/layout/OrganisationLookup/validation.ts
  • src/layout/PersonLookup/validation.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Install
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Type-checks, eslint, unit tests and SonarCloud

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@HauklandJ HauklandJ added kind/other Pull requests containing chores/repo structure/other changes ignore-for-release Pull requests to be ignored in release notes backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels Nov 10, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

♻️ Duplicate comments (1)
src/layout/OrganisationLookup/validation.ts (1)

58-58: Duplicate function definition across files.

This is the same modular_additive_inverse function defined in PersonLookup/validation.ts. See the comment in that file for recommendations on extracting to a shared utility module.

🧹 Nitpick comments (3)
src/layout/PersonLookup/validation.ts (2)

50-51: The two-step modulo operation obscures intent.

The pattern of calling modular_additive_inverse() followed by % 11 suggests the helper function doesn't fully implement the modular additive inverse. This makes the code less clear.

Consider incorporating the final modulo operation into the helper function:

-  let calculated_k1 = modular_additive_inverse(sum1, 11);
-  calculated_k1 = calculated_k1 % 11;
+  const calculated_k1 = modular_additive_inverse(sum1, 11) % 11;

Or refactor the helper to return values in the range [0, base-1]:

const modular_additive_inverse = (value: number, base: number): number => {
  const remainder = value % base;
  return remainder === 0 ? 0 : base - remainder;
};

64-65: Same two-step modulo pattern as K1 calculation.

Same issue as lines 50-51. Consider the same refactor.

src/layout/OrganisationLookup/validation.ts (1)

50-51: Same two-step modulo pattern as PersonLookup.

Same pattern as in PersonLookup/validation.ts. Refer to the recommendations in that file for consolidating this into a single operation or refactoring the helper function.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2517d53 and e7019ae.

📒 Files selected for processing (2)
  • src/layout/OrganisationLookup/validation.ts (1 hunks)
  • src/layout/PersonLookup/validation.ts (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Avoid using any and unnecessary type casts (as Type) in TypeScript; prefer precise typings and refactor existing casts/anys
For TanStack Query, use objects to manage query keys and functions, and centralize shared options via queryOptions

Files:

  • src/layout/OrganisationLookup/validation.ts
  • src/layout/PersonLookup/validation.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Type-checks, eslint, unit tests and SonarCloud
  • GitHub Check: Install
🔇 Additional comments (1)
src/layout/OrganisationLookup/validation.ts (1)

45-48: Readability improvement from explicit loop.

The explicit loop over weights is clearer and more maintainable than the previous implementation.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
src/layout/OrganisationLookup/validation.ts (1)

40-41: Add digit validation to prevent NaN in calculations.

The past review comment correctly identified that the length check doesn't validate that all characters are digits. If orgNr contains non-numeric characters, map(Number) will produce NaN values, leading to incorrect calculations.

As suggested in the previous review, consider adding digit validation:

-  if (orgNr.length !== 9) {
+  if (orgNr.length !== 9 || !/^\d{9}$/.test(orgNr)) {
     return false;
   }
🧹 Nitpick comments (3)
src/layout/OrganisationLookup/validation.ts (3)

43-48: LGTM: Clear and correct weighted sum calculation.

The weights array and loop-based sum calculation are correct and readable. The refactor improves clarity over the previous approach.

Optionally, you could use a more functional style with reduce:

const sum = orgnr_digits.slice(0, 8).reduce((acc, digit, i) => acc + digit * weights[i], 0);

However, the current imperative approach is perfectly fine and may be more readable to some developers.


50-51: Consider simplifying the two-step modulo calculation.

The calculation is correct, but applying modulo twice (once in the helper, once explicitly) is slightly unclear. The standard mod 11 check digit formula is typically written as a single expression.

You could simplify by inlining:

-  let calculated_k1 = modular_additive_inverse(sum, 11);
-  calculated_k1 = calculated_k1 % 11;
+  const calculated_k1 = (11 - (sum % 11)) % 11;

Or keep the helper but make it include the final modulo operation (see comment on line 58).


58-58: Consider including the final modulo in the helper function.

The function name modular_additive_inverse suggests a complete modular arithmetic operation, but the implementation returns base - (value % base) without the final modulo. When value % base == 0, this returns base instead of 0, requiring the caller to apply % 11 again.

You could make the helper self-contained:

-const modular_additive_inverse = (value: number, base: number): number => base - (value % base);
+const modular_additive_inverse = (value: number, base: number): number => (base - (value % base)) % base;

This would eliminate the need for the additional modulo operation on line 51.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e7019ae and 00d00b2.

📒 Files selected for processing (1)
  • src/layout/OrganisationLookup/validation.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Avoid using any and unnecessary type casts (as Type) in TypeScript; prefer precise typings and refactor existing casts/anys
For TanStack Query, use objects to manage query keys and functions, and centralize shared options via queryOptions

Files:

  • src/layout/OrganisationLookup/validation.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Type-checks, eslint, unit tests and SonarCloud
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Install

Copy link
Contributor

@danielskovli danielskovli left a comment

Choose a reason for hiding this comment

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

Looks good to me 🙂

@sonarqubecloud
Copy link

@HauklandJ HauklandJ merged commit c687cc9 into main Nov 10, 2025
16 checks passed
@HauklandJ HauklandJ deleted the fix/function-name branch November 10, 2025 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-ignore This PR is a new feature and should not be cherry-picked onto release branches ignore-for-release Pull requests to be ignored in release notes kind/other Pull requests containing chores/repo structure/other changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants