feat(nodejs): add create static method

This commit is contained in:
leeching 2018-06-27 14:52:20 +08:00
parent c7104902e3
commit 7a020f52aa
3 changed files with 8 additions and 4 deletions

View File

@ -55,8 +55,8 @@ 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|广东|深圳|鹏博士' }
``` ```

View File

@ -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)) {

View File

@ -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(() => {