Fixes bug on regex matching not stopping after 6 chars
This commit is contained in:
parent
af8d7d995b
commit
c2af505e2a
|
@ -11,7 +11,7 @@ class Validator {
|
|||
}
|
||||
|
||||
foreach (self::$regexes as $regex) {
|
||||
if (preg_match($regex, $pin) === 1) {
|
||||
if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
11
src/index.js
11
src/index.js
|
@ -1,12 +1,17 @@
|
|||
const readline = require("readline");
|
||||
const fs = require("fs");
|
||||
|
||||
const regexes = fs.readFileSync(__dirname + '/../regex.txt', 'utf8')
|
||||
const regexes = fs
|
||||
.readFileSync(__dirname + "/../regex.txt", "utf8")
|
||||
.split("\n")
|
||||
// Remove empty lines
|
||||
.filter(function(r) {return r.length > 1})
|
||||
.filter(function(r) {
|
||||
return r.length > 1;
|
||||
})
|
||||
// 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 = {
|
||||
validate: function(pin) {
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
regex = list()
|
||||
f = open('regex.txt')
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
f = open(dir_path + '/../../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]))
|
||||
regex.append(re.compile('^' + line[1:-2] + '$'))
|
||||
|
||||
class Pincode:
|
||||
@staticmethod
|
||||
|
|
|
@ -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', '111111'];
|
||||
const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131'];
|
||||
|
||||
public function testSamplePins() {
|
||||
foreach(self::PINS as $pin) {
|
||||
|
|
|
@ -7,6 +7,6 @@ def test_valid_pincodes():
|
|||
assert_equals(True, Pincode.validate(pin), pin + " should be valid")
|
||||
|
||||
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:
|
||||
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');
|
||||
|
||||
// Incorrect
|
||||
assert.strictEqual(pincode.validate('1100111'), false, '1100111');
|
||||
assert.strictEqual(pincode.validate('111111'), false, '111111');
|
||||
assert.strictEqual(pincode.validate('999999'), false, '999999');
|
||||
assert.strictEqual(pincode.validate('99999'), false, '99999');
|
||||
|
|
Loading…
Reference in New Issue