india-pincode-regex/src/generate.js

39 lines
901 B
JavaScript
Raw Normal View History

2020-02-24 18:02:18 +00:00
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]),
2021-12-19 13:48:41 +00:00
console: false,
2020-02-24 18:02:18 +00:00
});
regexes = [];
2021-12-19 13:48:41 +00:00
// There are 3 Pincodes that start with 9, but we
// ignore those as test offices.
for (var i = 0; i < 8; i++) {
regexes.push(new Trie());
}
2020-02-24 18:02:18 +00:00
2021-12-19 13:48:41 +00:00
readInterface.on("line", function (line) {
2020-02-24 18:02:18 +00:00
if (line.length === 6) {
// First character of the PIN
let areaCode = parseInt(line.charAt(0), 10);
2021-12-19 13:48:41 +00:00
if (areaCode < 9 && areaCode > 0) {
let areaCodeIndex = areaCode - 1;
regexes[areaCodeIndex].add(line);
}
2020-02-24 18:02:18 +00:00
}
});
2021-12-19 13:48:41 +00:00
readInterface.on("close", function () {
for (i in regexes) {
console.log(regexes[i].toRegExp());
}
});