-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from damianwojtkowski/118-pesel-validation
118 pesel validation
- Loading branch information
Showing
4 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const required = value => (value ? undefined : "Required"); | ||
|
||
export const isPeselValid = pesel => { | ||
if (typeof pesel === "undefined" || !/^[0-9]{11}$/.test(pesel)) | ||
return "Type PESEL"; | ||
|
||
const weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]; | ||
let sum = 0; | ||
|
||
weights.forEach((weight, index) => { | ||
sum += weight * pesel.substring(index, index + 1); | ||
}); | ||
|
||
const checkSum = sum % 10 === 0 ? 0 : 10 - (sum % 10); | ||
|
||
if (checkSum !== +pesel.substring(10, 11)) return "Invalid PESEL"; | ||
}; | ||
|
||
export const composeValidators = validator => value => { | ||
return typeof validator === "undefined" ? required(value) : validator(value); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { isPeselValid } from "./validators"; | ||
|
||
describe("Validators", () => { | ||
it("returns valid pesel", () => { | ||
expect(isPeselValid("64042999928")).toBeUndefined(); | ||
}); | ||
|
||
it("return Type PESEL because of no value", () => { | ||
expect(isPeselValid(undefined)).toBe("Type PESEL"); | ||
}); | ||
|
||
it("return Type PESEL because of too short text", () => { | ||
expect(isPeselValid("9703100302")).toBe("Type PESEL"); | ||
}); | ||
|
||
it("return Invalid PESEL because of wrong numbers", () => { | ||
expect(isPeselValid("97031003021")).toBe("Invalid PESEL"); | ||
}); | ||
}); |