Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/dk/cvr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('dk/cvr', () => {
expect(result.isValid && result.compact).toEqual('13585628');
});

test.each(['DK13585628', '13585628', 'DK19319970'])('validate:%s', value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
});
Comment on lines +17 to +21

Choose a reason for hiding this comment

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

medium

The new test cases are great for verifying the fix. To make them more robust, you could also assert the compact form of the number, similar to the test on line 11. This would ensure that the cleaning logic works correctly for these inputs in addition to the validation logic.

  test.each([
    ['DK13585628', '13585628'],
    ['13585628', '13585628'],
    ['DK19319970', '19319970'],
  ])('validate:%s', (value, expected) => {
    const result = validate(value);

    expect(result.isValid && result.compact).toBe(expected);
  });


it('validate:1234567', () => {
const result = validate('1234567');

Expand Down
2 changes: 1 addition & 1 deletion src/dk/cvr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const impl: Validator = {
modulus: 11,
weights: [2, 7, 6, 5, 4, 3, 2, 1],
});
if (String((11 - sum) % 10) !== check) {
if (String((11 - (sum % 11)) % 11) !== check) {

Choose a reason for hiding this comment

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

medium

The updated checksum logic is correct. However, it can be slightly simplified. The sum variable, which is the result of weightedSum, is already the result of a modulo 11 operation. Therefore, the additional sum % 11 within the check is redundant and can be removed for better readability.

Suggested change
if (String((11 - (sum % 11)) % 11) !== check) {
if (String((11 - sum) % 11) !== check) {

return { isValid: false, error: new exceptions.InvalidChecksum() };
}

Expand Down