Fixes bug on regex matching not stopping after 6 chars
This commit is contained in:
parent
af8d7d995b
commit
c2af505e2a
|
@ -4,18 +4,18 @@ namespace PIN;
|
||||||
|
|
||||||
class Validator {
|
class Validator {
|
||||||
static $regexes;
|
static $regexes;
|
||||||
|
|
||||||
public static function validate(string $pin) {
|
public static function validate(string $pin) {
|
||||||
if(!self::$regexes) {
|
if(!self::$regexes) {
|
||||||
self::$regexes = array_filter(file('regex.txt'));
|
self::$regexes = array_filter(file('regex.txt'));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (self::$regexes as $regex) {
|
foreach (self::$regexes as $regex) {
|
||||||
if (preg_match($regex, $pin) === 1) {
|
if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/index.js
11
src/index.js
|
@ -1,12 +1,17 @@
|
||||||
const readline = require("readline");
|
const readline = require("readline");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
const regexes = fs.readFileSync(__dirname + '/../regex.txt', 'utf8')
|
const regexes = fs
|
||||||
|
.readFileSync(__dirname + "/../regex.txt", "utf8")
|
||||||
.split("\n")
|
.split("\n")
|
||||||
// Remove empty lines
|
// Remove empty lines
|
||||||
.filter(function(r) {return r.length > 1})
|
.filter(function(r) {
|
||||||
|
return r.length > 1;
|
||||||
|
})
|
||||||
// Remove the opening and closing slashes
|
// Remove the opening and closing slashes
|
||||||
.map(function(r) {return new RegExp(r.slice(1,-1));})
|
.map(function(r) {
|
||||||
|
return new RegExp("^" + r.slice(1, -1) + "$");
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validate: function(pin) {
|
validate: function(pin) {
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
regex = list()
|
regex = list()
|
||||||
f = open('regex.txt')
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
f = open(dir_path + '/../../regex.txt')
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if (len(line) > 10):
|
if (len(line) > 10):
|
||||||
# Remove the \n at the end
|
# Remove the \n at the end
|
||||||
regex.append(re.compile(line[1:-2]))
|
regex.append(re.compile('^' + line[1:-2] + '$'))
|
||||||
|
|
||||||
class Pincode:
|
class Pincode:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
|
||||||
class SimpleTest extends TestCase {
|
class SimpleTest extends TestCase {
|
||||||
const PINS = ['244713', '560029', '560030', '110011'];
|
const PINS = ['244713', '560029', '560030', '110011'];
|
||||||
|
|
||||||
const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111'];
|
const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131'];
|
||||||
|
|
||||||
public function testSamplePins() {
|
public function testSamplePins() {
|
||||||
foreach(self::PINS as $pin) {
|
foreach(self::PINS as $pin) {
|
||||||
|
|
|
@ -7,6 +7,6 @@ def test_valid_pincodes():
|
||||||
assert_equals(True, Pincode.validate(pin), pin + " should be valid")
|
assert_equals(True, Pincode.validate(pin), pin + " should be valid")
|
||||||
|
|
||||||
def test_invalid_pincodes():
|
def test_invalid_pincodes():
|
||||||
INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111'];
|
INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131'];
|
||||||
for pin in INVALID_PINS:
|
for pin in INVALID_PINS:
|
||||||
assert_equals(False, Pincode.validate(pin), pin + " should be invalid")
|
assert_equals(False, Pincode.validate(pin), pin + " should be invalid")
|
||||||
|
|
|
@ -9,6 +9,7 @@ assert.strictEqual(pincode.validate('560029'), true, '560029');
|
||||||
assert.strictEqual(pincode.validate('560030'), true, '560030');
|
assert.strictEqual(pincode.validate('560030'), true, '560030');
|
||||||
|
|
||||||
// Incorrect
|
// Incorrect
|
||||||
|
assert.strictEqual(pincode.validate('1100111'), false, '1100111');
|
||||||
assert.strictEqual(pincode.validate('111111'), false, '111111');
|
assert.strictEqual(pincode.validate('111111'), false, '111111');
|
||||||
assert.strictEqual(pincode.validate('999999'), false, '999999');
|
assert.strictEqual(pincode.validate('999999'), false, '999999');
|
||||||
assert.strictEqual(pincode.validate('99999'), false, '99999');
|
assert.strictEqual(pincode.validate('99999'), false, '99999');
|
||||||
|
|
Loading…
Reference in New Issue