diff --git a/src/index.ts b/src/index.ts index 7932a74..76dd949 100644 --- a/src/index.ts +++ b/src/index.ts @@ -123,8 +123,19 @@ export class GeoJsonlookfor { /* ***************** * geojsonを返す * *****************/ - getGeoJSON() { + getGeoJSON(number?: number) { + if (number !== undefined) { + this.geojson.features = this.geojson.features.slice(0, number); + } return this.geojson } + + + /* ******************* + * featureの件数を取得する + * *******************/ + getFeatureCount() { + return this.geojson.features.length; + } } diff --git a/test/testByGetDataType.ts b/test/testByGetDataType.ts new file mode 100644 index 0000000..a96f076 --- /dev/null +++ b/test/testByGetDataType.ts @@ -0,0 +1,60 @@ +import { assert } from 'chai'; +import * as fs from 'fs'; +import { GeoJsonlookfor } from '../src/index'; + + +// testデータのディレクトリ名を取得する +const geojson = JSON.parse(fs.readFileSync(`${__dirname}/test.geojson`, 'utf8')); + +/* ******************* + * 取得するデータ別のテスト + * *******************/ +describe('Test by return value', () => { + + // 件数を取得する + it('Get the number of cases', () => { + const gl = new GeoJsonlookfor(geojson); + const res = gl.match('スイーツ').getFeatureCount(); + assert.deepEqual(4, res); + }); + + // 指定した件数分featureを取得する + it('Get the number of cases', () => { + const gl = new GeoJsonlookfor(geojson); + const res = gl.match('スイーツ').getGeoJSON(2); + assert.deepEqual({ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "name": "スイーツショップ", + "address": "埼玉県桶川市川田谷", + "category": "スイーツ" + }, + "geometry": { + "coordinates": [ + 139.517720985072, + 35.99865685174926 + ], + "type": "Point" + } + },{ + "type": "Feature", + "properties": { + "name": "菓子店", + "address": "埼玉県川越市中老袋", + "category": "スイーツ" + }, + "geometry": { + "coordinates": [ + 139.5371783066526, + 35.941979468748585 + ], + "type": "Point" + } + } + ] + }, res); + }); +});