diff --git a/.travis.yml b/.travis.yml index b77b16d..01510be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,14 @@ -language: php -php: - - '7.2' - - '7.3' - - '7.4' -install: composer install \ No newline at end of file +matrix: + include: + - language: php + php: + - '7.2' + - '7.3' + - '7.4' + install: composer install + - language: node_js + node_js: + - '10' + - '12' + - '13' + - '14' \ No newline at end of file diff --git a/package.json b/package.json index 55c3cfd..3a13d24 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { - "name": "india-pincode-regex", - "version": "1.0.0", + "name": "pincode", + "version": "1.0.2", "description": "A simple regex based validator for PIN codes in India", - "main": "index.js", + "main": "src/index.js", "directories": { "test": "tests" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node tests/validate.js" }, "repository": { "type": "git", @@ -28,5 +28,6 @@ "homepage": "https://github.com/captn3m0/india-pincode-regex#readme", "devDependencies": { "regexgen": "^1.3.0" - } + }, + "dependencies": {} } diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a935d23 --- /dev/null +++ b/src/index.js @@ -0,0 +1,17 @@ +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));}) + +module.exports = { + validate: function(pin) { + for (let i in regexes) { + if (regexes[i].test(pin)) { + return true; + } + } + return false; + } +}; \ No newline at end of file diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php index 9839485..1a8e76f 100644 --- a/tests/SimpleTest.php +++ b/tests/SimpleTest.php @@ -4,9 +4,9 @@ use PIN\Validator as P; use PHPUnit\Framework\TestCase; class SimpleTest extends TestCase { - const PINS = ['244713', '560029', '560030']; + const PINS = ['244713', '560029', '560030', '110011']; - const INVALID_PINS = ['999999']; + const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9']; public function testSamplePins() { foreach(self::PINS as $pin) { diff --git a/tests/validate.js b/tests/validate.js new file mode 100644 index 0000000..9740457 --- /dev/null +++ b/tests/validate.js @@ -0,0 +1,14 @@ +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')); \ No newline at end of file