Python package and other updates
- Break down regex into one-per area code - Improves tests - Adds python package - Adds editorconfig - Travis Updates
This commit is contained in:
parent
f42f70d156
commit
f24d67d914
|
@ -0,0 +1,12 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
# indent_size = 2
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[regex.txt]
|
||||
insert_final_newline = true
|
|
@ -1,3 +1,4 @@
|
|||
/vendor/
|
||||
/node_modules/
|
||||
composer.lock
|
||||
composer.lock
|
||||
__pycache__/
|
||||
|
|
10
.travis.yml
10
.travis.yml
|
@ -14,4 +14,12 @@ jobs:
|
|||
- language: node_js
|
||||
node_js: '12'
|
||||
- language: node_js
|
||||
node_js: '13'
|
||||
node_js: '13'
|
||||
- language: python
|
||||
node_js: '3.5'
|
||||
- language: python
|
||||
node_js: '3.6'
|
||||
- language: python
|
||||
node_js: '3.7'
|
||||
- language: python
|
||||
node_js: '3.8'
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
[[source]]
|
||||
name = "pypi"
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[packages]
|
||||
|
||||
[requires]
|
||||
python_version = "3.5"
|
|
@ -0,0 +1,15 @@
|
|||
[tool.poetry]
|
||||
name = "pincode"
|
||||
version = "0.1.0"
|
||||
description = "A simple offline pincode validator for India"
|
||||
authors = ["Nemo <python@captnemo.in>"]
|
||||
license = "MIT"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.5"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry>=0.12"]
|
||||
build-backend = "poetry.masonry.api"
|
|
@ -12,20 +12,23 @@ const readInterface = readline.createInterface({
|
|||
console: false
|
||||
});
|
||||
|
||||
let t1 = new Trie(),
|
||||
t2 = new Trie();
|
||||
regexes = [];
|
||||
|
||||
for (var i = 0; i < 8; i++) {
|
||||
regexes.push(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)
|
||||
}
|
||||
// First character of the PIN
|
||||
let areaCode = parseInt(line.charAt(0), 10);
|
||||
let areaCodeIndex = areaCode - 1;
|
||||
regexes[areaCodeIndex].add(line);
|
||||
}
|
||||
});
|
||||
|
||||
readInterface.on("close", function() {
|
||||
console.log(t1.toRegExp("u"));
|
||||
console.log(t2.toRegExp("u"));
|
||||
});
|
||||
for(i in regexes) {
|
||||
console.log(regexes[i].toRegExp());
|
||||
}
|
||||
});
|
||||
|
|
11
src/index.js
11
src/index.js
|
@ -1,9 +1,12 @@
|
|||
const readline = require("readline");
|
||||
const fs = require("fs");
|
||||
|
||||
const regexes = fs.readFileSync(__dirname + '/../regex.txt', 'utf8').split("\n")
|
||||
// We drop the opening / and ending /u
|
||||
.map(function(r) {return new RegExp(r.slice(1, -2));})
|
||||
const regexes = fs.readFileSync(__dirname + '/../regex.txt', 'utf8')
|
||||
.split("\n")
|
||||
// Remove empty lines
|
||||
.filter(function(r) {return r.length > 1})
|
||||
// Remove the opening and closing slashes
|
||||
.map(function(r) {return new RegExp(r.slice(1,-1));})
|
||||
|
||||
module.exports = {
|
||||
validate: function(pin) {
|
||||
|
@ -14,4 +17,4 @@ module.exports = {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
import re
|
||||
|
||||
regex = list()
|
||||
f = open('regex.txt')
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
if (len(line) > 10):
|
||||
# Remove the \n at the end
|
||||
regex.append(re.compile(line[1:-2]))
|
||||
|
||||
class Pincode:
|
||||
@staticmethod
|
||||
def validate(code):
|
||||
for r in regex:
|
||||
if r.match(code) != None:
|
||||
return True
|
||||
return False
|
|
@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
|
|||
class SimpleTest extends TestCase {
|
||||
const PINS = ['244713', '560029', '560030', '110011'];
|
||||
|
||||
const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9'];
|
||||
const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111'];
|
||||
|
||||
public function testSamplePins() {
|
||||
foreach(self::PINS as $pin) {
|
||||
|
@ -17,4 +17,4 @@ class SimpleTest extends TestCase {
|
|||
$this->assertFalse(P::validate($pin), "$pin should be invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
from pincode import *
|
||||
from nose.tools import assert_equals
|
||||
|
||||
def test_valid_pincodes():
|
||||
VALID_PINS = ['244713', '560029', '560030', '110011']
|
||||
for pin in VALID_PINS:
|
||||
assert_equals(True, Pincode.validate(pin), pin + " should be valid")
|
||||
|
||||
def test_invalid_pincodes():
|
||||
INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111'];
|
||||
for pin in INVALID_PINS:
|
||||
assert_equals(False, Pincode.validate(pin), pin + " should be invalid")
|
|
@ -1,14 +1,18 @@
|
|||
const pincode = require('../src/index');
|
||||
const assert = require('assert');
|
||||
|
||||
assert.strictEqual(true, pincode.validate('110011'));
|
||||
assert.strictEqual(true, pincode.validate('244713'));
|
||||
assert.strictEqual(true, pincode.validate('560029'));
|
||||
assert.strictEqual(true, pincode.validate('560030'));
|
||||
assert.strictEqual(false, pincode.validate('111111'));
|
||||
assert.strictEqual(false, pincode.validate('999999'));
|
||||
assert.strictEqual(false, pincode.validate('99999'));
|
||||
assert.strictEqual(false, pincode.validate('9999'));
|
||||
assert.strictEqual(false, pincode.validate('999'));
|
||||
assert.strictEqual(false, pincode.validate('99'));
|
||||
assert.strictEqual(false, pincode.validate('9'));
|
||||
// A few correct ones
|
||||
|
||||
assert.strictEqual(pincode.validate('110011'), true, '110011');
|
||||
assert.strictEqual(pincode.validate('244713'), true, '244713');
|
||||
assert.strictEqual(pincode.validate('560029'), true, '560029');
|
||||
assert.strictEqual(pincode.validate('560030'), true, '560030');
|
||||
|
||||
// Incorrect
|
||||
assert.strictEqual(pincode.validate('111111'), false, '111111');
|
||||
assert.strictEqual(pincode.validate('999999'), false, '999999');
|
||||
assert.strictEqual(pincode.validate('99999'), false, '99999');
|
||||
assert.strictEqual(pincode.validate('9999'), false, '9999');
|
||||
assert.strictEqual(pincode.validate('999'), false, '999');
|
||||
assert.strictEqual(pincode.validate('99'), false, '99');
|
||||
assert.strictEqual(pincode.validate('9'), false, '9');
|
||||
|
|
Loading…
Reference in New Issue