HermannBjorgvin / Kennitala

Icelandic national ID (kennitölur) utilities for servers and clients.
MIT License
36 stars 10 forks source link

generatePerson sometimes generates invalid id #35

Open Herdismaria opened 1 week ago

Herdismaria commented 1 week ago

It looks like either the generatePerson function or info has some bug in it. If I use generatePerson to create a kennitala and then use info to parse it, info sometimes results in invalid kennitala.

it('should return a valid national id', () => {
      const nationalId = generatePerson(new Date())!
      const nationalIdInfo = info(nationalId)
      expect(nationalIdInfo?.valid).toEqual(true)
    })

Example of a kennitala created with generatePerson

  {
      kt: '0101210190',
      valid: false,
      type: 'invalid',
      birthday: Invalid Date,
      birthdayReadable: '',
      age: NaN
    }

This test is flaky. I'm using version 2.0.6.

Herdismaria commented 1 week ago

Also should the Date parameter be optional for generatePerson? it looks like it's required now :)

HermannBjorgvin commented 1 week ago

Thanks for the bug report @Herdismaria

I think the tests might be failing because like the KT you generated shows it has a 7th-8th digits of 01 which would be illegal for persons since they start generating personal ID's at 20 and up.

Looks like the bug is residing in ./src/generation.ts where startingIncrement should have a default value of 20.

I'll push out a fix for after work today. Apologies for the inconvenience. If you want a quick fix you can add 20 as an optional parameter to your generatePerson function call.

const generatePerson = (
  date: Date,
  startingIncrement?: number
): string | undefined => {
  return generateKennitala(date, personDayDelta, startingIncrement);
};
HermannBjorgvin commented 1 week ago

Also should the Date parameter be optional for generatePerson? it looks like it's required now :)

Interesting thought. So if the Date parameter is optional we could generate a random Date as a default parameter?

Would be a cool feature

Herdismaria commented 5 days ago

According to the docs the date parameter should be optional, just thought it might have changed with the new version 🤷‍♀️

Docs:

image

Function export function generatePerson(date: Date): string;