Initial commit

This commit is contained in:
Nemo 2020-02-24 23:32:18 +05:30
commit 9a56544692
11 changed files with 202 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor/
/node_modules/
composer.lock

17
README.md Normal file
View File

@ -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

21
composer.json Normal file
View File

@ -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"
}
}

30
package-lock.json generated Normal file
View File

@ -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"
}
}
}
}

32
package.json Normal file
View File

@ -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"
}
}

25
phpunit.xml Normal file
View File

@ -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>

2
regex.txt Normal file

File diff suppressed because one or more lines are too long

21
src/Validator.php Normal file
View File

@ -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;
}
}

31
src/generate.js Normal file
View File

@ -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
src/validator.js Normal file
View File

20
tests/SimpleTest.php Normal file
View File

@ -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");
}
}
}