Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat iranian postal code #340

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [Get the Remaining Time of the Date](#get-the-remaining-time-of-the-date)
- [Validate and find information of phone number](#validate-and-find-information-of-phone-number).
- [Find capital city by province name ](#find-capital-city-by-province-name)
- [Validate Iranian Postal code](#validate-iranian-postal-code).

## Getting started

Expand Down Expand Up @@ -558,6 +559,23 @@ findCapitalByProvince("آذربایجان شرقی"); // تبریز
findCapitalByProvince("دبی");
```

### Validate Iranian Postal code

**Usage**

> this function validate entry postal code and return boolean for validation confirm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with This function validates the entry postal code and returns a boolean for validation confirmation.


```js
import { iranianPostalCodeValidate } from "@persian-tools/persian-tools";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to validateIranianPostalCode


// valid postal code
iranianPostalCodeValidate("1193653471"); // true

// invalid postal code
iranianPostalCodeValidate("142386473"); // false
iranianPostalCodeValidate("1111111111"); // false
```

### Todo

- [ ] Write Jalaali and Gregorian functions to convert Date together.
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ export { default as remainingTime } from "./modules/remainingTime";
//Find capital city by state name

export { default as findCapitalByProvince } from "./modules/findCapitalByProvince";

//iranian postal code validation
export { default as iranianPostalCodeValidate } from './modules/iranianPostalCode'
27 changes: 27 additions & 0 deletions src/modules/iranianPostalCode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/**
* iranianPostalCodeValidate
*
* @description Takes a postal code and validate with return a boolean.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

@returns boolean

and explain the algorithms you've implemented.

*
*
* @param postalCode as string
* @return if postal code is valid return true , but if postal code is not valid return false
*/
const iranianPostalCodeValidate = (postalCode: string = ""): boolean => {
const pc = postalCode.toString();
if (/\D/g.test(pc)) return false;

return (
pc.length === 10 &&
!pc.slice(0, 5).includes("2") &&
!pc.slice(0, 5).includes("0") &&
pc[4] !== "5" &&
pc[5] !== "0" &&
pc.slice(-4) !== "0000" &&
!pc.split("").every((char) => char === pc[0])
);
}


export default iranianPostalCodeValidate
23 changes: 23 additions & 0 deletions test/iranianPostalCode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { iranianPostalCodeValidate } from "../src";
import { describe, it, expect } from "vitest";

const validPostalCode = "1193653471"
const invalidPostalCodes = [
'142386473',
'12',
'1111111111',
'0423437584',
"test invalid postalCode"
]

describe("iranianPostalCode", () => {
it('valid postal code', () => {
expect(iranianPostalCodeValidate(validPostalCode)).toBeTruthy()
})

it('invalid postal codes', () => {
invalidPostalCodes.forEach((invalidPostalCode) => {
expect(iranianPostalCodeValidate(invalidPostalCode)).toBeFalsy()
})
})
});