Initial commit
commit
9a56544692
|
@ -0,0 +1,3 @@
|
|||
/vendor/
|
||||
/node_modules/
|
||||
composer.lock
|
|
@ -0,0 +1,17 @@
|
|||
# india-pincode-regex
|
||||
|
||||
Validate a [Postal Index Number][wiki] for India with a regex. The regexes are available in `regex.txt`. There are 2 regexes. The first validates PINs starting from 1-4, and the second validates the ones starting from 5-8. The reason for the split is to keep the regex size small. Currently they are both at 16K each.
|
||||
|
||||
## Source
|
||||
|
||||
The source for the data is the ["All India Pincode Directory"](https://data.gov.in/resources/all-india-pincode-directory) dataset on data.gov.in.
|
||||
|
||||
## Usage
|
||||
|
||||
The `regex.txt` file is 32KB in size, so you can easily use it wherever you want, including browsers.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under the [MIT License](https://nemo.mit-license.org/). See LICENSE file for details.
|
||||
|
||||
[wiki]: https://en.wikipedia.org/wiki/Postal_Index_Number
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "captn3m0/india-pincode-regex",
|
||||
"description": "A simple regex based validator for PIN codes in India",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nemo",
|
||||
"email": "composer@captnemo.in"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PIN\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "india-pincode-regex",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"jsesc": {
|
||||
"version": "2.5.2",
|
||||
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz",
|
||||
"integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==",
|
||||
"dev": true
|
||||
},
|
||||
"regenerate": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz",
|
||||
"integrity": "sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==",
|
||||
"dev": true
|
||||
},
|
||||
"regexgen": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/regexgen/-/regexgen-1.3.0.tgz",
|
||||
"integrity": "sha1-sCLzjlEgu0b9zTf6y5EWjxXm/no=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"jsesc": "^2.3.0",
|
||||
"regenerate": "^1.3.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"name": "india-pincode-regex",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple regex based validator for PIN codes in India",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "tests"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/captn3m0/india-pincode-regex.git"
|
||||
},
|
||||
"keywords": [
|
||||
"india",
|
||||
"pincode",
|
||||
"regex",
|
||||
"validator",
|
||||
"PIN"
|
||||
],
|
||||
"author": "Nemo <npm@captnemo.in>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/captn3m0/india-pincode-regex/issues"
|
||||
},
|
||||
"homepage": "https://github.com/captn3m0/india-pincode-regex#readme",
|
||||
"devDependencies": {
|
||||
"regexgen": "^1.3.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit bootstrap = "vendor/autoload.php"
|
||||
backupGlobals = "false"
|
||||
backupStaticAttributes = "false"
|
||||
colors = "true"
|
||||
convertErrorsToExceptions = "true"
|
||||
convertNoticesToExceptions = "true"
|
||||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "false"
|
||||
stopOnFailure = "false">
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Project Test Suite">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
</phpunit>
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace PIN;
|
||||
|
||||
class Validator {
|
||||
static $regexes;
|
||||
|
||||
public static function validate(string $pin) {
|
||||
if(!self::$regexes) {
|
||||
self::$regexes = array_filter(file('regex.txt'));
|
||||
}
|
||||
|
||||
foreach (self::$regexes as $regex) {
|
||||
if (preg_match($regex, $pin) === 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
const fs = require("fs");
|
||||
const readline = require("readline");
|
||||
const { Trie } = require("regexgen");
|
||||
|
||||
if (!process.argv[2]) {
|
||||
console.error("Please pass pincode filename (unique only) as argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: fs.createReadStream(process.argv[2]),
|
||||
console: false
|
||||
});
|
||||
|
||||
let t1 = new Trie(),
|
||||
t2 = new Trie();
|
||||
|
||||
readInterface.on("line", function(line) {
|
||||
if (line.length === 6) {
|
||||
if (['1', '2', '3', '4'].includes(line.charAt(0))) {
|
||||
t1.add(line);
|
||||
} else {
|
||||
t2.add(line)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
readInterface.on("close", function() {
|
||||
console.log(t1.toRegExp("u"));
|
||||
console.log(t2.toRegExp("u"));
|
||||
});
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
use PIN\Validator as P;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SimpleTest extends TestCase {
|
||||
const PINS = ['244713', '560029', '560030'];
|
||||
|
||||
const INVALID_PINS = ['999999'];
|
||||
|
||||
public function testSamplePins() {
|
||||
foreach(self::PINS as $pin) {
|
||||
$this->assertTrue(P::validate($pin), "$pin should be valid");
|
||||
}
|
||||
|
||||
foreach(self::INVALID_PINS as $pin) {
|
||||
$this->assertFalse(P::validate($pin), "$pin should be invalid");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue