Skip to content

Commit

Permalink
Feat/number count validation (#3)
Browse files Browse the repository at this point in the history
* feat; adding nodemon as a dev dependency

* feat: input length validation
  • Loading branch information
eyopa21 authored Aug 20, 2024
1 parent 85eb3d1 commit 81ee068
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 9 deletions.
214 changes: 210 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
"scripts": {
"build": "tsc",
"test": "jest",
"start": "node dist/index.js",
"start": "nodemon dist/index.js",
"prepublishOnly": "npm run build"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^18.0.0",
"jest": "^29.0.0",
"nodemon": "^3.1.4",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/?(*.)+(spec|test).ts"]
"testMatch": [
"**/?(*.)+(spec|test).ts"
]
},
"license": "MIT",
"author": "Hulunlante Worku",
Expand All @@ -30,7 +32,7 @@
"type": "git",
"url": "git+https://github.com/HuluWZ/phone-formater-eth.git"
},
"bugs": {
"bugs": {
"url": "https://github.com/HuluWZ/phone-formater-eth/issues"
},
"keywords": [
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export function formatPhone(phone: string) {
const formatted_phone = phone.replace(/[^+\d]/g, "");
const phone_length = formatted_phone?.toString().length;
if (phone_length === 13 && formatted_phone.startsWith("+251")) {
if (phone_length < 9 || phone_length > 13) {
return 'INVALID_PHONE_NUMBER'
}
else if (phone_length === 13 && formatted_phone.startsWith("+251")) {
return formatted_phone;
} else if (phone_length === 12 && formatted_phone.startsWith("251")) {
return `+${formatted_phone}`;
Expand Down

0 comments on commit 81ee068

Please sign in to comment.