Serial Pan Card Generation Algorithm.
In any fintech company in India, the user’s pan number is commonly used as primary identification.
A common strategy used in creating test users in such an application is generating a Random pan number
but one drawback with this approach is any newly generated pan should be checked against the database if it already exists to avoid a collision.
The better way is to serially generate the pan numbers to avoid a collision.
Algorithm to serially generate a pan card number for a given number (x)
.
generatePan: function (id) {
const denomination = [26, 10, 10, 10, 10, 26, 26, 26, 26, 26];
var current = id;
var final = '';
for (var i = 0; i < 10; i++) {
var part = current % denomination[i];
var char = denomination[i] === 26 ? String.fromCharCode(65 + part) : part;
final = `${char}${final}`;
current = parseInt(current / denomination[i]);
}
return final;
}
- This could be used be imported in test cases to generate unique pan numbers
- Also to mask pan data for production dumps.
The series would go as:
[
'AAAAA0000A',
'AAAAA0000B',
'AAAAA0000C',
....
'AAAAA0000Z',
'AAAAA0001A',
'AAAAA0001B'
]
generatePan(0) // => AAAAA0000A
generatePan(random_number) // => FGKNN4725Z
© 2023 bsybin