Skip to content

Commit 7e2dfc0

Browse files
authored
Merge pull request #5 from runk/cleanup
Minor cleanup
2 parents 563cc80 + 811c495 commit 7e2dfc0

File tree

4 files changed

+133
-116
lines changed

4 files changed

+133
-116
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ npm-debug.log
33
.DS_Store
44
testing.js
55
*.iml
6-
.idea
6+
.idea

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Simple and fast IP to Country/Region lookup module for nodejs. **Pull Requests a
77
## GEO databases
88

99
You can download fresh geo database from this website: http://ipgeobase.ru/.
10-
Database contains major ranges for all countries worldwide and presice ranges for most Russian cities/regions.
11-
Also it could be extended with another geo data.
10+
Database contains major ranges for all countries worldwide and precise ranges for most Russian cities/regions.
11+
It could also be extended with other generic geo data.
1212

1313
## Installation
1414

@@ -17,22 +17,22 @@ Also it could be extended with another geo data.
1717
## Usage
1818

1919
var geo = require('geotools');
20-
var res = geo.lookup("87.229.134.24");
21-
/*
22-
* output is: {
23-
* country: 'RU',
24-
* region: 1056,
25-
* city: 'Березовский',
26-
* regionName: 'Свердловская область',
27-
* district: 'Уральский федеральный округ',
28-
* lat: '56.912811',
29-
* lon: '60.804699'
30-
* }
20+
var res = geo.lookup('87.229.134.24');
21+
/**
22+
* Output is:
23+
* {
24+
* country: 'RU',
25+
* region: 1056,
26+
* city: 'Березовский',
27+
* regionName: 'Свердловская область',
28+
* district: 'Уральский федеральный округ',
29+
* lat: '56.912811',
30+
* lon: '60.804699'
31+
* }
3132
*/
3233

3334
## Speed / Benchmark
3435

3536
Approximate speed on average laptop is about 1.5M calls per second. Benchmark is simple, but it gives you an idea about module's performance. Test it yourself:
3637

3738
node ./lib/bench.js
38-

lib/geotools.js

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,111 @@
11
var path = require('path');
22

33
var gindex = [],
4-
gCities = {},
5-
poweredUp = false;
4+
gCities = {},
5+
poweredUp = false;
66

77
var GEO_FIELD_MIN = 0,
8-
GEO_FIELD_MAX = 1,
9-
GEO_FIELD_COUNTRY = 2,
10-
GEO_FIELD_REGION = 3,
11-
GEO_DIR_DATA = path.join(__dirname, '..', 'geo');
8+
GEO_FIELD_MAX = 1,
9+
GEO_FIELD_COUNTRY = 2,
10+
GEO_FIELD_REGION = 3,
11+
GEO_DIR_DATA = path.join(__dirname, '..', 'geo');
1212

1313
// Load and parse geo base
1414
function _powerup() {
15-
var fs = require('fs');
16-
var cidr = fs.readFileSync(path.join(GEO_DIR_DATA, 'cidr_optim.txt'));
17-
cidr = cidr.toString().split('\n');
18-
19-
for (var i = cidr.length - 1; i >= 0; i--) {
20-
if (!cidr[i]) continue;
21-
var line = cidr[i].split('\t');
22-
gindex.push([
23-
parseInt(line[0]), // GEO_FIELD_MIN
24-
parseInt(line[1]), // GEO_FIELD_MAX
25-
line[3], // GEO_FIELD_COUNTRY
26-
(line[4] == "-") ? 0 : parseInt(line[4]) // GEO_FIELD_REGION
27-
]);
28-
}
29-
30-
var rawCities = fs.readFileSync(path.join(GEO_DIR_DATA, 'cities.txt'), 'utf-8');
31-
var cities = rawCities.split('\n');
32-
33-
for (i = 0; i < cities.length; i++) {
34-
var city = cities[i].split('\t');
35-
gCities[city[0]] = {
36-
city: city[1],
37-
regionName: city[2],
38-
district: city[3],
39-
lat: city[4],
40-
lon: city[5]
41-
};
42-
}
15+
var fs = require('fs');
16+
var cidr = fs.readFileSync(path.join(GEO_DIR_DATA, 'cidr_optim.txt'));
17+
cidr = cidr.toString().split('\n');
18+
19+
for (var i = cidr.length - 1; i >= 0; i--) {
20+
if (!cidr[i]) continue;
21+
var line = cidr[i].split('\t');
22+
gindex.push([
23+
parseInt(line[0]), // GEO_FIELD_MIN
24+
parseInt(line[1]), // GEO_FIELD_MAX
25+
line[3], // GEO_FIELD_COUNTRY
26+
(line[4] == "-") ? 0 : parseInt(line[4]) // GEO_FIELD_REGION
27+
]);
28+
}
29+
30+
var rawCities = fs.readFileSync(path.join(GEO_DIR_DATA, 'cities.txt'), 'utf-8');
31+
var cities = rawCities.split('\n');
32+
33+
for (i = 0; i < cities.length; i++) {
34+
var city = cities[i].split('\t');
35+
gCities[city[0]] = {
36+
city: city[1],
37+
regionName: city[2],
38+
district: city[3],
39+
lat: city[4],
40+
lon: city[5]
41+
};
42+
}
4343

44-
gindex.sort(function(a, b) {
45-
return a[GEO_FIELD_MIN] - b[GEO_FIELD_MIN];
46-
});
44+
gindex.sort(function(a, b) {
45+
return a[GEO_FIELD_MIN] - b[GEO_FIELD_MIN];
46+
});
4747

48-
poweredUp = true;
48+
poweredUp = true;
4949
}
5050

5151
function _normalize(row) {
52-
var cityInfo = gCities[row[GEO_FIELD_REGION]];
52+
var cityInfo = gCities[row[GEO_FIELD_REGION]];
5353

54-
var result = {
55-
country: row[GEO_FIELD_COUNTRY],
56-
region: row[GEO_FIELD_REGION]
57-
};
54+
var result = {
55+
country: row[GEO_FIELD_COUNTRY],
56+
region: row[GEO_FIELD_REGION]
57+
};
5858

59-
for (var key in cityInfo) {
60-
if (cityInfo.hasOwnProperty(key)) {
61-
result[key] = cityInfo[key];
62-
}
59+
for (var key in cityInfo) {
60+
if (cityInfo.hasOwnProperty(key)) {
61+
result[key] = cityInfo[key];
6362
}
63+
}
6464

65-
return result;
65+
return result;
6666
}
6767

6868
// IP to long converter
6969
module.exports.ip2long = function(ip) {
70-
ip = ip.split('.');
71-
var c = ip.length;
72-
ip[0] = parseInt(ip[0]) || 0;
73-
ip[1] = parseInt(ip[1]) || 0;
74-
ip[2] = parseInt(ip[2]) || 0;
75-
ip[3] = parseInt(ip[3]) || 0;
76-
77-
return ip[0] * (c === 1 || 16777216)
78-
+ ip[1] * (c <= 2 || 65536)
79-
+ ip[2] * (c <= 3 || 256)
80-
+ ip[3] * 1;
70+
ip = ip.split('.');
71+
var c = ip.length;
72+
ip[0] = parseInt(ip[0]) || 0;
73+
ip[1] = parseInt(ip[1]) || 0;
74+
ip[2] = parseInt(ip[2]) || 0;
75+
ip[3] = parseInt(ip[3]) || 0;
76+
77+
return ip[0] * (c === 1 || 16777216)
78+
+ ip[1] * (c <= 2 || 65536)
79+
+ ip[2] * (c <= 3 || 256)
80+
+ ip[3] * 1;
8181
};
8282

8383
// Binary search through the geo index
8484
module.exports.lookup = function(ip) {
8585

86-
if (!ip) return -1;
87-
if (!poweredUp) _powerup();
86+
if (!ip) return -1;
87+
if (!poweredUp) _powerup();
8888

89-
var find = this.ip2long(ip);
89+
var find = this.ip2long(ip);
9090

91-
var low = 0, high = gindex.length - 1, i;
91+
var low = 0, high = gindex.length - 1, i;
9292

93-
while (low <= high) {
94-
i = Math.floor((low + high) / 2);
95-
if (gindex[i][GEO_FIELD_MIN] > find) { high = i - 1; continue; }
96-
if (gindex[i][GEO_FIELD_MIN] < find) { low = i + 1; continue; }
97-
break;
98-
}
93+
while (low <= high) {
94+
i = Math.floor((low + high) / 2);
95+
if (gindex[i][GEO_FIELD_MIN] > find) { high = i - 1; continue; }
96+
if (gindex[i][GEO_FIELD_MIN] < find) { low = i + 1; continue; }
97+
break;
98+
}
9999

100-
// checking given index and two neighbouring indexes at i-1 and i+1
101-
if (find >= gindex[i][GEO_FIELD_MIN] && find <= gindex[i][GEO_FIELD_MAX])
102-
return _normalize(gindex[i]);
100+
// checking given index and two neighbouring indexes at i-1 and i+1
101+
if (find >= gindex[i][GEO_FIELD_MIN] && find <= gindex[i][GEO_FIELD_MAX])
102+
return _normalize(gindex[i]);
103103

104-
if ((i !== 0) && (find >= gindex[i-1][GEO_FIELD_MIN] && find <= gindex[i-1][GEO_FIELD_MAX]))
105-
return _normalize(gindex[i-1]);
104+
if ((i !== 0) && (find >= gindex[i-1][GEO_FIELD_MIN] && find <= gindex[i-1][GEO_FIELD_MAX]))
105+
return _normalize(gindex[i-1]);
106106

107-
if ((i !== gindex.length - 1) && (find >= gindex[i+1][GEO_FIELD_MIN] && find <= gindex[i+1][GEO_FIELD_MAX]))
108-
return _normalize(gindex[i+1]);
107+
if ((i !== gindex.length - 1) && (find >= gindex[i+1][GEO_FIELD_MIN] && find <= gindex[i+1][GEO_FIELD_MAX]))
108+
return _normalize(gindex[i+1]);
109109

110-
return null;
110+
return null;
111111
};

package.json

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
{
2-
"name": "geotools",
3-
"version": "0.1.0",
4-
"homepage": "https://github.com/runk/geotools",
5-
"description": "IP to Country/Region lookup module",
6-
"keywords": ["geo", "geobase", "geo lookup", "ip base", "geocode"],
7-
"author": "Shirokov Dmitry <[email protected]>",
8-
"dependencies": {},
9-
"repository": {
10-
"type":"git",
11-
"url":"[email protected]:runk/geotools.git"
12-
},
13-
"bugs": {
14-
15-
"url":"http://github.com/runk/geotools/issues"
16-
},
17-
"main": "index.js",
18-
"engine": { "node": ">=0.8.0", "npm": "1" },
19-
"scripts": {
20-
"test": "mocha ./test/geotools.js"
21-
},
22-
"licences": [{
23-
"type":"LGPL2.1",
24-
"url": "http://www.gnu.org/licenses/lgpl-2.1.txt"
25-
}]
2+
"name": "geotools",
3+
"version": "0.1.0",
4+
"homepage": "https://github.com/runk/geotools",
5+
"description": "IP to Country/Region lookup module",
6+
"keywords": [
7+
"geo",
8+
"geobase",
9+
"geo lookup",
10+
"ip base",
11+
"geocode"
12+
],
13+
"author": "Shirokov Dmitry <[email protected]>",
14+
"contributors": [
15+
"Yuriy Bushev @bushev"
16+
],
17+
"dependencies": {},
18+
"repository": {
19+
"type": "git",
20+
"url": "[email protected]:runk/geotools.git"
21+
},
22+
"bugs": {
23+
"mail": "[email protected]",
24+
"url": "http://github.com/runk/geotools/issues"
25+
},
26+
"main": "index.js",
27+
"engine": {
28+
"node": ">=0.8.0",
29+
"npm": ">=1"
30+
},
31+
"scripts": {
32+
"test": "mocha --recursive test"
33+
},
34+
"licences": [
35+
{
36+
"type": "LGPL2.1",
37+
"url": "http://www.gnu.org/licenses/lgpl-2.1.txt"
38+
}
39+
],
40+
"devDependencies": {
41+
"mocha": "^2.5.3"
42+
}
2643
}

0 commit comments

Comments
 (0)