koblas / stdnum-js

A JavaScript library to provide functions to handle, parse and validate standard numbers.
MIT License
39 stars 26 forks source link

Turkish Indentification numbers ending in 01/9 or 00 are not validating #40

Closed mmutsaars closed 1 year ago

mmutsaars commented 1 year ago

We found that numbers like 64211142102, 62345678900 should validate as Turkish Identification Numbers but currently are not.

If second to last number is a 0 the checksum validation fails due to sum1 in tckimlik validate returning 10 instead of 0 in those cases.

diff --git a/src/tr/tckimlik.ts b/src/tr/tckimlik.ts
index 22b8a6f..dc2fc72 100644
--- a/src/tr/tckimlik.ts
+++ b/src/tr/tckimlik.ts
@@ -56,11 +56,11 @@ const impl: Validator = {
     const [front, check] = strings.splitAt(value, -2);

     const sum1 =
-      10 -
-      weightedSum(front, {
-        weights: [3, 1],
-        modulus: 10,
-      });
+      (10 -
+        weightedSum(front, {
+          weights: [3, 1],
+          modulus: 10,
+        })) % 10;
     const sum2 =
       (sum1 +
         weightedSum(front, {
diff --git a/src/tr/tckimlik.spec.ts b/src/tr/tckimlik.spec.ts
index cdf3ca1..b95d672 100644
--- a/src/tr/tckimlik.spec.ts
+++ b/src/tr/tckimlik.spec.ts
@@ -25,4 +25,16 @@ describe('tr/tckimlik', () => {

     expect(result.error).toBeInstanceOf(InvalidChecksum);
   });
+
+  it('validate:64211142102', () => {
+    const result = validate('64211142102');
+
+    expect(result.isValid && result.compact).toEqual('64211142102');
+  });
+
+  it('validate:62345678900', () => {
+    const result = validate('62345678900');
+
+    expect(result.isValid && result.compact).toEqual('62345678900');
+  })
 });
koblas commented 1 year ago

Always great to get a PR that fixes an issue.

Thanks