Compare commits
10 Commits
fc6bfdb113
...
fddfe4113c
Author | SHA1 | Date |
---|---|---|
Nemo | fddfe4113c | |
Nemo | 2c02026a8f | |
Nemo | 9bb0367232 | |
Nemo | 86e276b81c | |
Nemo | 794f82a4e2 | |
Nemo | a5adba4a5c | |
Nemo | 2461fc5ace | |
Nemo | f1eb4a8c80 | |
Nemo | e1cb3c9054 | |
Nemo | 79b3302227 |
|
@ -4,3 +4,4 @@ pincode-regex.js linguist-generated
|
|||
tests/jasmine-3.5.0/* linguist-vendored
|
||||
# regex.txt is generated (see src/generate.js)
|
||||
regex.txt linguist-generated
|
||||
regexes.txt linguist-generated
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
on: push
|
||||
name: Tests
|
||||
jobs:
|
||||
node:
|
||||
strategy:
|
||||
matrix:
|
||||
node: ["12", "14", "16", "18", "20"]
|
||||
name: Node.js
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: ${{matrix.node}}
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
php:
|
||||
strategy:
|
||||
matrix:
|
||||
# https://www.php.net/supported-versions.php
|
||||
php: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]
|
||||
name: PHP
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{matrix.php}}
|
||||
tools: phpunit, composer
|
||||
- run: composer install --no-interaction
|
||||
- run: ./vendor/bin/phpunit
|
||||
ruby:
|
||||
strategy:
|
||||
matrix:
|
||||
ruby: ["2.6", "2.7", "3.0", "3.1"]
|
||||
name: Ruby
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: ${{ matrix.ruby }}
|
||||
bundler-cache: true
|
||||
- run: bundle install
|
||||
- run: bundle exec rake
|
18
.npmignore
18
.npmignore
|
@ -4,6 +4,7 @@ tests/
|
|||
phpunit.xml
|
||||
node_modules/
|
||||
src/*.php
|
||||
src/*.rb
|
||||
.travis.yml
|
||||
src/generate.js
|
||||
HACKING.md
|
||||
|
@ -13,3 +14,20 @@ src/pincode
|
|||
*.toml
|
||||
Pipfile
|
||||
.editorconfig
|
||||
regexes.txt
|
||||
__pypackages__
|
||||
.github
|
||||
.gitattributes
|
||||
.phpunit*
|
||||
CHANGELOG.md
|
||||
CONTRIBUTING.md
|
||||
CODE_OF_CONDUCT.md
|
||||
Gemfile*
|
||||
Rakefile
|
||||
bin/
|
||||
phpunit.*
|
||||
*.gem
|
||||
*.csv
|
||||
pincode-regex.js
|
||||
pincode-validator.gemspec
|
||||
src/pincode.egg-info/
|
||||
|
|
35
.travis.yml
35
.travis.yml
|
@ -1,35 +0,0 @@
|
|||
jobs:
|
||||
include:
|
||||
- language: php
|
||||
php: '7.2'
|
||||
install: composer install
|
||||
- language: php
|
||||
php: '7.3'
|
||||
install: composer install
|
||||
- language: php
|
||||
php: '7.4'
|
||||
install: composer install
|
||||
- language: node_js
|
||||
node_js: '10'
|
||||
- language: node_js
|
||||
node_js: '12'
|
||||
- language: node_js
|
||||
node_js: '13'
|
||||
- language: python
|
||||
python: '3.5'
|
||||
script: nosetests tests/test.py
|
||||
- language: python
|
||||
python: '3.6'
|
||||
script: nosetests tests/test.py
|
||||
- language: python
|
||||
python: '3.7'
|
||||
script: nosetests tests/test.py
|
||||
- language: python
|
||||
python: '3.8'
|
||||
script: nosetests tests/test.py
|
||||
- language: ruby
|
||||
ruby: '2.5'
|
||||
- language: ruby
|
||||
ruby: '2.6'
|
||||
- language: ruby
|
||||
ruby: '2.7'
|
|
@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at me@captnemo.in. All
|
||||
reported by contacting the project team at coc@captnemo.in. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
# Contribution Guidelines
|
||||
|
||||
## Generating the regex
|
||||
|
||||
1. Download the latest CSV file from <https://data.gov.in/resources/all-india-pincode-directory-till-last-month>.
|
||||
2. Copy all the pincodes to a pincodes.csv file
|
||||
3. Generate all unique pincodes by running `npm run build`
|
||||
|
||||
## Regex Notes
|
||||
|
||||
Note that the regexes are all PCRE compatible, but do not include `/` at the beginning or end. This is for cross-compatibility and ease of use.
|
||||
The two files are:
|
||||
|
||||
- `regex.txt` - This is the regex for validating a pincode. It is a single regex.
|
||||
- `regexes.txt` - It is a list of regexes, one per line. One regex is available per area code (the first digit of the PIN, which goes from 1-8).
|
||||
|
||||
## Generating the browser-version
|
||||
|
||||
1. Make sure development dependencies are installed.
|
||||
2. `npm run browserify`.
|
||||
3. Test it by opening `tests/index.html` in your browser.
|
||||
|
||||
# PHP Release
|
||||
|
||||
1. `git tag v1.2.3`
|
||||
2. `git push --tags`
|
||||
|
||||
## Ruby Development
|
||||
|
||||
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
||||
|
||||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
||||
|
||||
Run `gem build pincode-validator.gemspec` to build the gem, and `tar --to-stdout -xf *.gem data.tar.gz | tar -zt` to validate the list of files inside the gem before publishing it.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
Please see [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for the code of conduct.
|
32
README.md
32
README.md
|
@ -4,7 +4,7 @@ Validate a [Postal Index Number][wiki] for India with a few regexes and zero fal
|
|||
|
||||
## Why?
|
||||
|
||||
A simple `\d{6}` approach marks a lot of invalid pincodes as valid. Out of the 900000 possible combinations, only approximately `155600` are valid pincodes in India. A simple example is `111111` which is an invalid pincode, but any simple 6 digit-check will pass it as a valid one. File a PR to support any other language.
|
||||
A simple `\d{6}` approach marks a lot of invalid pincodes as valid. Out of the 900000 possible combinations, only approximately `19000` are valid pincodes in India. A simple example is `111111` which is an invalid pincode, but any simple 6 digit-check will pass it as a valid one.
|
||||
|
||||
## Source
|
||||
|
||||
|
@ -12,7 +12,11 @@ The source for the data is the ["All India Pincode Directory"](https://data.gov.
|
|||
|
||||
## Usage
|
||||
|
||||
The `regex.txt` file is 32KB in size, so you can easily use it wherever you want, including browsers. If you are using any of the packages below, this is already delivered compressed.
|
||||
The `regex.txt` file is 32KB in size, so you can easily use it wherever you want, including browsers. If you are using any of the packages below, this is already delivered compressed. You can use the regex directly, or via a few helper methods.
|
||||
|
||||
## Supported Language Versions
|
||||
|
||||
This project only supports [supported versions](https://endoflife.date) of various languages.
|
||||
|
||||
### PHP
|
||||
|
||||
|
@ -22,7 +26,13 @@ To use the PHP package:
|
|||
|
||||
```php
|
||||
use PIN\Validator as P;
|
||||
// validates a given pincode
|
||||
// returns boolean
|
||||
P::validate('110011'); // returns true;
|
||||
|
||||
// Searches for all valid pincodes in a given string.
|
||||
// returns array(string)
|
||||
P::search('bangalore 560029'); // returns ["560029"]
|
||||
```
|
||||
|
||||
### Node.js
|
||||
|
@ -41,6 +51,8 @@ P.exactRegex.match('560029')
|
|||
"address with pincode (560029)".matchAll(P.regex)
|
||||
````
|
||||
|
||||
Please see `tests/validate.js` for more examples.
|
||||
|
||||
## Ruby
|
||||
|
||||
Add this line to your application's Gemfile:
|
||||
|
@ -57,6 +69,16 @@ Or install it yourself as:
|
|||
|
||||
$ gem install pincode_validator
|
||||
|
||||
```ruby
|
||||
require 'pincode_validator'
|
||||
|
||||
Pin::valid?('560029') # returns true
|
||||
Pin::valid?('111111') # returns false
|
||||
|
||||
Pin::search('my pincode is 244713') # returns ['244713']
|
||||
Pin::search('my pincode is 244713 or 560029') # returns ['244713', '560029']
|
||||
```
|
||||
|
||||
### Browser
|
||||
|
||||
To use it in the browser, download the `pincode-regex.js` file and include it in your browser. `Pincode` is available as a Global variable.
|
||||
|
@ -68,7 +90,7 @@ Pincode.validate("560029"); // returns true
|
|||
</script>
|
||||
```
|
||||
|
||||
You can use githack for directly using this in your code: <https://rawcdn.githack.com/captn3m0/india-pincode-regex/1.0.4/pincode-regex.js> (Make sure you use the latest version). Please watch the repo to get notified of new releases.
|
||||
You can use githack for directly using this in your code: <https://rawcdn.githack.com/captn3m0/india-pincode-regex/v2.0.0/pincode-regex.js> (Make sure you use the latest version). Please watch the repo to get notified of new releases.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
|
@ -76,8 +98,8 @@ Everyone interacting in the this project’s codebases, issue trackers, chat roo
|
|||
|
||||
## Contributing
|
||||
|
||||
- See [`HACKING.md`](HACKING.md) for some development details.
|
||||
- Pull requests are welcome for adding libraries in other languages (in the same repo).
|
||||
- See [`CONTRIBUTING.md`](CONTRIBUTING.md) for some development details and contribution guidelines
|
||||
- Pull requests are welcome for adding libraries in other languages (in the same repo). Python support is WIP, and I'd love to have support for other languages as well.
|
||||
- This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
||||
|
||||
## License
|
||||
|
|
|
@ -14,6 +14,26 @@
|
|||
"PIN\\": "src/"
|
||||
}
|
||||
},
|
||||
"archive": {
|
||||
"exclude": [
|
||||
"/tests",
|
||||
"/regex.txt",
|
||||
"/.editorconfig",
|
||||
"/phpunit.xml",
|
||||
|
||||
"/.npmignore",
|
||||
"/package.json",
|
||||
"/pincode-regex.js",
|
||||
"/src/generate.js",
|
||||
"/src/index.js",
|
||||
"/pyproject.toml",
|
||||
"/src/pincode/__init__.py",
|
||||
|
||||
"/pincode-validator.gemspec",
|
||||
"/bin",
|
||||
"/src/pincode_validator.rb"
|
||||
]
|
||||
},
|
||||
"require": {},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.0 || ^8.0 || ^10.0"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "pincode-validator",
|
||||
"version": "1.0.4",
|
||||
"version": "2.0.0",
|
||||
"description": "A simple regex based validator for PIN codes in India",
|
||||
"main": "src/index.js",
|
||||
"directories": {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
[project]
|
||||
name = "pincode"
|
||||
version = "1.0.4"
|
||||
version = "2.0.0"
|
||||
description = "A simple offline pincode validator for India"
|
||||
authors = [
|
||||
{name = "Nemo", email = "python@captnemo.in"},
|
||||
]
|
||||
dependencies = ["requests"]
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.7"
|
||||
requires-python = ">=3.8"
|
||||
license = {text = "MIT"}
|
||||
keywords = ["pincode", "regex", "offline", "pin", "validator"]
|
||||
classifiers = [
|
||||
|
@ -24,6 +24,7 @@ Documentation = "https://github.com/captn3m0/india-pincode-regex/wiki"
|
|||
[tool.pdm]
|
||||
includes = ["CHANGELOG.md", "README.md", "regex.txt", "src/pincode"]
|
||||
excludes = [
|
||||
".github/",
|
||||
"src/*.js",
|
||||
"src/*.php",
|
||||
"tests/*.php",
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -3,34 +3,53 @@
|
|||
namespace PIN;
|
||||
|
||||
class Validator {
|
||||
static $regex;
|
||||
static $exactRegex;
|
||||
static $regexes;
|
||||
|
||||
public static function init(){
|
||||
if(!self::$regex) {
|
||||
self::$regex = "/" . trim(file_get_contents('regex.txt')) . "/";
|
||||
self::$exactRegex = "/^" . trim(file_get_contents('regex.txt')) . "$/";
|
||||
public static function init() {
|
||||
if(!self::$regexes) {
|
||||
self::$regexes = array_map(function($line) {
|
||||
return '/' . trim($line) . '/';
|
||||
}, array_filter(file('regexes.txt')));
|
||||
}
|
||||
}
|
||||
|
||||
public static function validate(string $pin) {
|
||||
self::init();
|
||||
|
||||
fwrite(STDERR, var_dump(self::$exactRegex, TRUE));
|
||||
|
||||
if (strlen($pin) === 6 and preg_match(self::$exactRegex, $pin) === 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function search(string $address) {
|
||||
/**
|
||||
* Validate a PIN code
|
||||
* @param string $pin
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate(string $pin) : bool{
|
||||
self::init();
|
||||
preg_match_all(self::$regex, $address, $matches);
|
||||
|
||||
return array_map(function($match) {
|
||||
return $match[0];
|
||||
}, $matches);
|
||||
foreach (self::$regexes as $regex) {
|
||||
if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for PIN codes in a string
|
||||
* @param string $address
|
||||
* @return array
|
||||
*/
|
||||
public static function search(string $address){
|
||||
self::init();
|
||||
$results = [];
|
||||
|
||||
foreach (self::$regexes as $regex) {
|
||||
preg_match_all($regex, $address, $matches);
|
||||
|
||||
$results = array_reduce($matches, function($res, $match) {
|
||||
if (isset($match[0]) and in_array($match[0], $res, true) === false){
|
||||
$res[] = $match[0];
|
||||
}
|
||||
return $res;
|
||||
}, $results);
|
||||
}
|
||||
|
||||
return array_values($results);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,5 +32,10 @@ readInterface.on("line", function (line) {
|
|||
});
|
||||
|
||||
readInterface.on("close", function () {
|
||||
console.log("(" + regexes.join('|') + ")");
|
||||
// We maintain two files, one with a single regex using (|) for each segment
|
||||
fs.writeFileSync("regex.txt", "(" + regexes.join('|') + ")");
|
||||
// And another with 8 regexes, one for each area code.
|
||||
// The latter is required for some languages, which have a regex character limit
|
||||
// (Currently PHP)
|
||||
fs.writeFileSync("regexes.txt", regexes.join("\n"));
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module PincodeValidator
|
||||
VERSION = "1.0.4"
|
||||
VERSION = "2.0.0"
|
||||
FILENAME='regex.txt'
|
||||
|
||||
class Error < StandardError; end
|
||||
|
|
|
@ -18,10 +18,10 @@ class SimpleTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
// public function testSearch() {
|
||||
// $this->assertSame(P::search("my pincode is 560029"), ["560029"]);
|
||||
// $this->assertSame(P::search("560029"), ["560029"]);
|
||||
// $this->assertSame(P::search("560029 244713"), ["560029", "244713"]);
|
||||
// $this->assertSame(P::search("address 560038 bangalore"), ["560038"]);
|
||||
// }
|
||||
public function testSearch() {
|
||||
$this->assertSame(P::search("560029"), ["560029"]);
|
||||
$this->assertSame(P::search("my pincode is 560029"), ["560029"]);
|
||||
$this->assertSame(P::search("560029 244713"), ["244713", "560029"]);
|
||||
$this->assertSame(P::search("address 560038 bangalore"), ["560038"]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue