From 7a020f52aa02e826bb3ecd4b16c7ba2d8ed436cc Mon Sep 17 00:00:00 2001 From: leeching Date: Wed, 27 Jun 2018 14:52:20 +0800 Subject: [PATCH] feat(nodejs): add create static method --- README.md | 6 +++--- binding/nodejs/ip2region.js | 4 ++++ binding/nodejs/ip2region.spec.js | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 339e275..66fdc63 100644 --- a/README.md +++ b/README.md @@ -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|广东|深圳|鹏博士' } ``` diff --git a/binding/nodejs/ip2region.js b/binding/nodejs/ip2region.js index 686fcff..c6396cb 100644 --- a/binding/nodejs/ip2region.js +++ b/binding/nodejs/ip2region.js @@ -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)) { diff --git a/binding/nodejs/ip2region.spec.js b/binding/nodejs/ip2region.spec.js index a7650ee..0982fd7 100644 --- a/binding/nodejs/ip2region.spec.js +++ b/binding/nodejs/ip2region.spec.js @@ -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(() => {