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