Skip to content

Commit

Permalink
Added Country class to query country information and Region class to …
Browse files Browse the repository at this point in the history
…lookup the ISO 3166-2 subdivision code for country code and region name
  • Loading branch information
ip2location committed Oct 19, 2022
1 parent 4310332 commit 3971b76
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 5 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,49 @@ for (const x of cidr) {
console.log(tools.cidrToIPV4("10.123.80.0/12"));
console.log(tools.cidrToIPV6("2002:1234::abcd:ffff:c0a8:101/62"));
```

## COUNTRY CLASS

## Methods
Below are the methods supported in this module.

|Method Name|Description|
|---|---|
|getCountryInfo(countryCode)|Returns the country information.|

## Usage

```javascript
const {Country} = require("ip2location-nodejs");

let country = new Country("./IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV");

country.getCountryInfo("US").then(country_info => {
console.log(country_info);
});

country.getCountryInfo("").then(country_info => {
console.log(country_info);
});
```

## REGION CLASS

## Methods
Below are the methods supported in this module.

|Method Name|Description|
|---|---|
|getRegionCode(countryCode, regionName)|Returns the region code for the supplied country code and region name.|

## Usage

```javascript
const {Region} = require("ip2location-nodejs");

let region = new Region("./IP2LOCATION-ISO3166-2.CSV");

region.getRegionCode("US", "California").then(region_code => {
console.log(region_code);
});
```
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ip2location-nodejs",
"version": "9.3.1",
"version": "9.4.1",
"description": "IP2Location geolocation component",
"keywords": [
"ip2location",
Expand All @@ -22,7 +22,8 @@
"types": "src/ip2location.d.ts",
"license": "MIT",
"dependencies": {
"big-integer": "^1.6.47"
"big-integer": "^1.6.47",
"csv-parser": "^3.0.0"
},
"repository": {
"type": "git",
Expand Down
35 changes: 34 additions & 1 deletion src/ip2location.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,37 @@ export class IPTools {
* @returns The array with the starting and ending IPv6 addresses.
*/
cidrToIPV6(cidr: string): string[];
}
}
declare class Country {
/**
* Read the country information CSV file and parse the data.
*
* @param csvFile The full path to the country information CSV file.
*/
constructor(csvFile: any);
/**
* Retrieves the country information.
*
* @param countryCode The country code to get the country information.
* @returns The country information.
*/
getCountryInfo(countryCode?: string): Promise<any[]>;
#private;
}
declare class Region {
/**
* Read the region information CSV file and parse the data.
*
* @param csvFile The full path to the region information CSV file.
*/
constructor(csvFile: any);
/**
* Retrieves the region code for the country code and region name.
*
* @param countryCode The country code to get the region code.
* @param regionName The region name to get the region code.
* @returns The region code.
*/
getRegionCode(countryCode?: string, regionName?: string): Promise<any>;
#private;
}
116 changes: 115 additions & 1 deletion src/ip2location.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ var net = require("net");
var fs = require("fs");
var bigInt = require("big-integer");
var https = require("https");
const csv = require("csv-parser");

// For BIN queries
const VERSION = "9.3.1";
const VERSION = "9.4.1";
const MAX_INDEX = 65536;
const COUNTRY_POSITION = [
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Expand Down Expand Up @@ -1533,8 +1534,121 @@ class IPTools {
}
}

// Country class
class Country {
#fields = Array();
#records = {};
#fd;
#ready = false;

constructor(csvFile) {
if (!fs.existsSync(csvFile)) {
throw new Error("The CSV file " + csvFile + " is not found.");
}
try {
fs.createReadStream(csvFile)
.pipe(csv(true))
.on("data", (data) => {
if (data.country_code) {
this.#records[data.country_code] = data;
} else {
throw new Error("Invalid country information CSV file.");
}
})
.on("end", () => {
this.#ready = true;
});
} catch (err) {
throw new Error("Unable to read " + csvFile + ".");
}
}

// Get country information
async getCountryInfo(countryCode = "") {
while (!this.#ready) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
countryCode = countryCode.trim();
let results = Array();
if (Object.keys(this.#records).length === 0) {
throw new Error("No record available.");
}
if (countryCode != "") {
if (this.#records[countryCode]) {
results.push(this.#records[countryCode]);
}
} else {
for (const elem in this.#records) {
results.push(this.#records[elem]);
}
}
return results;
}
}

// Region class
class Region {
#fields = Array();
#records = {};
#fd;
#ready = false;

constructor(csvFile) {
if (!fs.existsSync(csvFile)) {
throw new Error("The CSV file " + csvFile + " is not found.");
}
try {
fs.createReadStream(csvFile)
.pipe(csv(true))
.on("data", (data) => {
if (data.subdivision_name) {
if (!this.#records[data.country_code]) {
this.#records[data.country_code] = Array();
}
this.#records[data.country_code].push({
code: data.code,
name: data.subdivision_name,
});
} else {
throw new Error("Invalid region information CSV file.");
}
})
.on("end", () => {
this.#ready = true;
});
} catch (err) {
throw new Error("Unable to read " + csvFile + ".");
}
}

// Get region code
async getRegionCode(countryCode = "", regionName = "") {
while (!this.#ready) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
countryCode = countryCode.trim();
regionName = regionName.trim();
if (Object.keys(this.#records).length === 0) {
throw new Error("No record available.");
}
if (this.#records[countryCode]) {
for (let x = 0; x < this.#records[countryCode].length; x++) {
let elem = this.#records[countryCode][x];
if (regionName.toUpperCase() == elem.name.toUpperCase()) {
return elem.code;
}
}
return null;
} else {
return null;
}
}
}

module.exports = {
IP2Location: IP2Location,
IP2LocationWebService: IP2LocationWebService,
IPTools: IPTools,
Country: Country,
Region: Region,
};
18 changes: 17 additions & 1 deletion src/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {IP2Location, IP2LocationWebService, IPTools} = require("ip2location-nodejs");
const {IP2Location, IP2LocationWebService, IPTools, Country, Region} = require("ip2location-nodejs");

let ip2location = new IP2Location();

Expand Down Expand Up @@ -84,3 +84,19 @@ for (const x of cidr) {
}
console.log(tools.cidrToIPV4("10.123.80.0/12"));
console.log(tools.cidrToIPV6("2002:1234::abcd:ffff:c0a8:101/62"));

let country = new Country("./IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV");

country.getCountryInfo("US").then(country_info => {
console.log(country_info);
});

country.getCountryInfo("").then(country_info => {
console.log(country_info);
});

let region = new Region("./IP2LOCATION-ISO3166-2.CSV");

region.getRegionCode("US", "California").then(region_code => {
console.log(region_code);
});

0 comments on commit 3971b76

Please sign in to comment.