Skip to content

Commit

Permalink
compatibility with browserify (nojacko#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dukei authored Aug 13, 2022
1 parent 9b8a2c1 commit d14d23b
Show file tree
Hide file tree
Showing 16 changed files with 2,324 additions and 2,342 deletions.
123 changes: 50 additions & 73 deletions lib/cjs/country-to-code.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,50 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.countryToAlpha3 = exports.countryToAlpha2 = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const latinize_1 = require("./utils/latinize");
const strings_1 = require("./utils/strings");
const rootDir = __dirname.replace(/(src|lib)([\\\/]+[a-z]+)?$/, "");
const dataDir = path.join(rootDir, "data");
const alpha2s = JSON.parse(fs.readFileSync(path.join(dataDir, "iso-alpha-2.json")).toString());
const alpha3s = JSON.parse(fs.readFileSync(path.join(dataDir, "iso-alpha-3.json")).toString());
const alpha23s = JSON.parse(fs.readFileSync(path.join(dataDir, "iso-alpha-2-to-3.json")).toString());
const names = JSON.parse(fs.readFileSync(path.join(dataDir, "names.json")).toString());
/**
* Convert almost anything to an ISO 3166-1 alpha-2 code.
* @param str String to convert. Note: any is used so we can test non-string values returning null for CommonJS version.
*/
const countryToAlpha2 = (str) => {
// Check it's a string at least 2 chars long
if (typeof str !== "string" || str.length < 2) {
return null;
}
const country = strings_1.removeSpaces(strings_1.normalize(latinize_1.latinize(str)));
// Too short
if (country.length < 2) {
return null;
}
// Already ISO 3166 alpha 2
if (country.length === 2 && alpha2s.includes(country)) {
return country;
}
// Is ISO 3166 alpha 3
if (country.length === 3 && alpha3s[country]) {
return alpha3s[country];
}
// Exact match
if (names[country]) {
return names[country];
}
return null;
};
exports.countryToAlpha2 = countryToAlpha2;
/**
* Convert almost anything to an ISO 3166-1 alpha-3 code.
* @param str String to convert. Note: any is used so we can test non-string values returning null for CommonJS version.
*/
const countryToAlpha3 = (str) => {
const alpha2 = exports.countryToAlpha2(str);
if (alpha2) {
return alpha23s[alpha2];
}
return null;
};
exports.countryToAlpha3 = countryToAlpha3;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.countryToAlpha3 = exports.countryToAlpha2 = void 0;
const latinize_1 = require("./utils/latinize");
const strings_1 = require("./utils/strings");
const alpha2s = require("../../data/iso-alpha-2.json");
const alpha3s = require("../../data/iso-alpha-3.json");
const alpha23s = require("../../data/iso-alpha-2-to-3.json");
const names = require("../../data/names.json");
/**
* Convert almost anything to an ISO 3166-1 alpha-2 code.
* @param str String to convert. Note: any is used so we can test non-string values returning null for CommonJS version.
*/
const countryToAlpha2 = (str) => {
// Check it's a string at least 2 chars long
if (typeof str !== "string" || str.length < 2) {
return null;
}
const country = (0, strings_1.removeSpaces)((0, strings_1.normalize)((0, latinize_1.latinize)(str)));
// Too short
if (country.length < 2) {
return null;
}
// Already ISO 3166 alpha 2
if (country.length === 2 && alpha2s.includes(country)) {
return country;
}
// Is ISO 3166 alpha 3
if (country.length === 3 && alpha3s[country]) {
return alpha3s[country];
}
// Exact match
if (names[country]) {
return names[country];
}
return null;
};
exports.countryToAlpha2 = countryToAlpha2;
/**
* Convert almost anything to an ISO 3166-1 alpha-3 code.
* @param str String to convert. Note: any is used so we can test non-string values returning null for CommonJS version.
*/
const countryToAlpha3 = (str) => {
const alpha2 = (0, exports.countryToAlpha2)(str);
if (alpha2) {
return alpha23s[alpha2];
}
return null;
};
exports.countryToAlpha3 = countryToAlpha3;
12 changes: 6 additions & 6 deletions lib/cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.countryToAlpha3 = exports.countryToAlpha2 = void 0;
const country_to_code_1 = require("./country-to-code");
Object.defineProperty(exports, "countryToAlpha2", { enumerable: true, get: function () { return country_to_code_1.countryToAlpha2; } });
Object.defineProperty(exports, "countryToAlpha3", { enumerable: true, get: function () { return country_to_code_1.countryToAlpha3; } });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.countryToAlpha3 = exports.countryToAlpha2 = void 0;
const country_to_code_1 = require("./country-to-code");
Object.defineProperty(exports, "countryToAlpha2", { enumerable: true, get: function () { return country_to_code_1.countryToAlpha2; } });
Object.defineProperty(exports, "countryToAlpha3", { enumerable: true, get: function () { return country_to_code_1.countryToAlpha3; } });
124 changes: 64 additions & 60 deletions lib/cjs/scripts/generate.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const latinize_1 = require("../utils/latinize");
const strings_1 = require("../utils/strings");
const rootDir = __dirname.replace(/(src|lib)([\/\\a-z])*$/, "");
const dataDir = path.join(rootDir, "data");
const alpha2sFiles = path.join(dataDir, "iso-alpha-2.json");
const alpha2s = JSON.parse(fs.readFileSync(alpha2sFiles).toString());
const namesFile = path.join(dataDir, "names.json");
const names = {};
for (const alpha2 of alpha2s) {
const countryFile = path.join(dataDir, "country", `${alpha2}.json`);
const countryData = JSON.parse(fs.readFileSync(countryFile).toString());
// Create new set to avoid duplicates
const set = new Set(countryData.names);
for (const name of Array.from(set)) {
if (typeof name === 'string') {
set.delete(name);
const latinized = latinize_1.latinize(name);
set.add(strings_1.removeSpaces(strings_1.normalize(latinized)));
set.add(strings_1.removeSpaces(strings_1.normalize(strings_1.removeSpaces(latinized))));
}
}
// Populate names
set.forEach((name) => {
if (typeof name === 'string' && name.length) {
if (names[name]) {
console.log(`CLASH! "${name}" [${alpha2}] has been seen for ${names[name]}`);
}
else {
names[name] = alpha2;
}
}
});
}
fs.writeFileSync(namesFile, JSON.stringify(names, null, 2));
console.log("Generate complete...");
console.log(`* Countries: ${alpha2s.length};`);
console.log(`* Names: ${Object.keys(names).length}`);
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const latinize_1 = require("../utils/latinize");
const strings_1 = require("../utils/strings");
const rootDir = __dirname.replace(/(src|lib)([\/\\a-z])*$/, "");
const dataDir = path.join(rootDir, "data");
const alpha2sFiles = path.join(dataDir, "iso-alpha-2.json");
const alpha2s = JSON.parse(fs.readFileSync(alpha2sFiles).toString());
const namesFile = path.join(dataDir, "names.json");
const names = {};
for (const alpha2 of alpha2s) {
const countryFile = path.join(dataDir, "country", `${alpha2}.json`);
const countryData = JSON.parse(fs.readFileSync(countryFile).toString());
// Create new set to avoid duplicates
const set = new Set(countryData.names);
for (const name of Array.from(set)) {
if (typeof name === 'string') {
set.delete(name);
const latinized = (0, latinize_1.latinize)(name);
set.add((0, strings_1.removeSpaces)((0, strings_1.normalize)(latinized)));
set.add((0, strings_1.removeSpaces)((0, strings_1.normalize)((0, strings_1.removeSpaces)(latinized))));
}
}
// Populate names
set.forEach((name) => {
if (typeof name === 'string' && name.length) {
if (names[name]) {
console.log(`CLASH! "${name}" [${alpha2}] has been seen for ${names[name]}`);
}
else {
names[name] = alpha2;
}
}
});
}
fs.writeFileSync(namesFile, JSON.stringify(names, null, 2));
console.log("Generate complete...");
console.log(`* Countries: ${alpha2s.length};`);
console.log(`* Names: ${Object.keys(names).length}`);
Loading

0 comments on commit d14d23b

Please sign in to comment.