india-pincode-regex/src/generate.js

42 lines
1.2 KiB
JavaScript
Raw Permalink 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.trim());
2021-12-19 13:48:41 +00:00
}
2020-02-24 18:02:18 +00:00
}
});
2021-12-19 13:48:41 +00:00
readInterface.on("close", function () {
2023-07-02 08:51:35 +00:00
// We maintain two files, one with a single regex using (|) for each segment
fs.writeFileSync("regex.txt", "(" + regexes.join('|') + ")");
// And another with 8 regexes, one for each area code.
// The latter is required for some languages, which have a regex character limit
// (Currently PHP)
fs.writeFileSync("regexes.txt", regexes.join("\n"));
});