feat(nodejs): add create static method
This commit is contained in:
parent
c7104902e3
commit
7a020f52aa
|
|
@ -55,9 +55,9 @@ example result:
|
||||||
|
|
||||||
node:
|
node:
|
||||||
```
|
```
|
||||||
> const IP2Region = require('./ip2region.js');
|
> let ip2region = require('./ip2region.js');
|
||||||
> const dbService = new IP2Region({dbPath: './ip2region.db'});
|
> let dbService = ip2region.create('./ip2region.db');
|
||||||
> dbService.binarySearchSync('101.105.35.57')
|
> dbService.binarySearchSync(' 101.105.35.57')
|
||||||
{ city: 0, region: '中国|0|广东|深圳|鹏博士' }
|
{ city: 0, region: '中国|0|广东|深圳|鹏博士' }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,10 @@ function getLong(buffer, offset) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class IP2Region {
|
class IP2Region {
|
||||||
|
static create(dbPath) {
|
||||||
|
return new IP2Region({dbPath});
|
||||||
|
}
|
||||||
|
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
const { dbPath } = options;
|
const { dbPath } = options;
|
||||||
if (!dbPath || !fs.existsSync(dbPath)) {
|
if (!dbPath || !fs.existsSync(dbPath)) {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ describe('ip2region', () => {
|
||||||
let instance;
|
let instance;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
instance = new IP2Region({dbPath: path.join(__dirname, '../../data/ip2region.db')});
|
instance = IP2Region.create(path.join(__dirname, '../../data/ip2region.db'));
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue