Skip to content

Instantly share code, notes, and snippets.

@vsnikkil
Created September 2, 2021 16:23
Show Gist options
  • Save vsnikkil/957d984c84382c1b57959bd7f26298b7 to your computer and use it in GitHub Desktop.
Save vsnikkil/957d984c84382c1b57959bd7f26298b7 to your computer and use it in GitHub Desktop.
Create reference numbers for invoicing. International or local (Finnish) RF format
export const createReferenceNumbers = (
baseStr: string = "000",
qty = 10,
international = false
): string[] => {
const baseNumber = BigInt(baseStr);
return Array.from({ length: qty }, (_value, idx) => {
const base = String(baseNumber + BigInt(idx));
const checksum: number =
10 -
(Array.from(base).reduceRight(
(acc: number, current: string, idx, arr) => {
return (
acc +
Number.parseInt(current) *
prodCoefficients[
(arr.length - 1 - idx) % lenOfCoefficients
]
);
},
0
) %
10);
const finnishRefNumber = `${base}${checksum === 10 ? 0 : checksum}`;
if (international) {
const internationalChecksum = String(
98n - (BigInt(finnishRefNumber + rfSequence) % 97n)
).padStart(2, "0");
return `RF${internationalChecksum}${finnishRefNumber}`;
} else {
return finnishRefNumber;
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment