Skip to content

Commit

Permalink
Merge pull request #14 from geolonia/add-other-search
Browse files Browse the repository at this point in the history
AND検索、OR検索、NOT検索処理を追加
  • Loading branch information
sugama-satsuki authored Jul 31, 2024
2 parents d14c90f + c13e3e2 commit 50a4cf4
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 64 deletions.
81 changes: 77 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export class GeoJsonlookfor {
this.geojson = geojson;
}

// "keyword"を含む項目があるfeatureを検索する
lookfor(keyword: string) {
/* *****************
* "keyword"を含む項目があるfeatureを検索する
* *****************/
match(keyword: string) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
}
const features = this.geojson.features
const features = this.geojson.features;

this.geojson = {
"type": "FeatureCollection",
Expand All @@ -28,7 +30,78 @@ export class GeoJsonlookfor {
}
}

// geojsonを返す
/* *****************
* "keywords"配列内の文字列で検索を行う
* *****************/
orMatch(keywords: string[]) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
}
const features = this.geojson.features;

this.geojson = {
"type": "FeatureCollection",
"features": features.filter((feature: any) => {
return keywords.some((keyword) => JSON.stringify(feature).includes(keyword));
})
};

return this;
} catch (err: any) {
throw new Error(err);
}
}

andMatch(keywords: string[]) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
}
const features = this.geojson.features;

this.geojson = {
"type": "FeatureCollection",
"features": features.filter((feature: any) => {
return keywords.every((keyword) => JSON.stringify(feature).includes(keyword));
})
};

return this;
} catch (err: any) {
throw new Error(err);
}
}

/* *****************
* "keyword"を含まないfeatureを検索する
* *****************/
notMatch(keyword: string | string[]) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
}
const features = this.geojson.features;

this.geojson = {
"type": "FeatureCollection",
"features": features.filter((feature: any) => {
const keywords = Array.isArray(keyword) ? keyword : [keyword];
// return !JSON.stringify(feature).includes(keyword);
return !keywords.some((keyword) => JSON.stringify(feature).includes(keyword));
})
};

return this;
} catch (err: any) {
throw new Error(err);
}
}


/* *****************
* geojsonを返す
* *****************/
getGeoJSON() {
return this.geojson
}
Expand Down
18 changes: 16 additions & 2 deletions test/test.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
35.97501042924834
],
"type": "Point"
},
"id": 1
}
},
{
"type": "Feature",
Expand Down Expand Up @@ -77,6 +76,21 @@
"type": "Point"
}
},
{
"type": "Feature",
"properties": {
"name": "オーガニックカフェ GEO",
"address": "埼玉県入間郡越生町大谷",
"category": "カフェ"
},
"geometry": {
"coordinates": [
139.30085900992,
35.975010429248
],
"type": "Point"
}
},
{
"type": "Feature",
"properties": {
Expand Down
66 changes: 8 additions & 58 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('The first test', () => {
// スイーツショップを検索する
it('should be lookfor sweetsshop.', () => {
const gl = new GeoJsonlookfor(geojson);
const res1 = gl.lookfor('スイーツショップ').getGeoJSON();
const res1 = gl.match('スイーツショップ').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand All @@ -43,7 +43,7 @@ describe('The first test', () => {
]
}, res1 );

const res2 = gl.lookfor('銭湯').getGeoJSON();
const res2 = gl.match('銭湯').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [ ]
Expand All @@ -54,7 +54,7 @@ describe('The first test', () => {
// "スイーツ"を含む項目があるfeatureを検索する
it('should be lookfor features that include "sweets".', () => {
const gl = new GeoJsonlookfor(geojson);
const res1 = gl.lookfor('スイーツ').getGeoJSON();
const res1 = gl.match('スイーツ').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('The first test', () => {
// "さいたま市"を含むfeatureを検索する
it('should be lookfor features include "さいたま市".', () => {
const gl = new GeoJsonlookfor(geojson);
const res1 = gl.lookfor('さいたま市').getGeoJSON();
const res1 = gl.match('さいたま市').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand All @@ -147,66 +147,18 @@ describe('The first test', () => {
]
}, res1 );

const res2 = gl.lookfor('那覇市').getGeoJSON();
const res2 = gl.match('那覇市').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": []
}, res2 );
});

// "スイーツ"を含み、"上尾市"を含むfeatureを検索する
it('should be lookfor features include "スイーツ" and "上尾市".', () => {
const gl = new GeoJsonlookfor(geojson);
const res1 = gl.lookfor('スイーツ').lookfor('上尾市').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "パティスリーGeolonia",
"address": "埼玉県上尾市弁財二丁目",
"category": "スイーツ"
},
"geometry": {
"coordinates": [
139.57772266590507,
35.97221769999193
],
"type": "Point"
}
},
{
"type": "Feature",
"properties": {
"name": "パティスリーぷりこ",
"address": "埼玉県上尾市中分三丁目",
"category": "スイーツ"
},
"geometry": {
"coordinates": [
139.54952061301026,
35.978737345548765
],
"type": "Point"
}
}
]
}, res1 );

const res2 = gl.lookfor('スイーツ').lookfor('那覇市').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": []
}, res2 );

});

// stringを引数として渡す
it('string as argument', () => {
try {
const gl = new GeoJsonlookfor(pmtile);
const res1 = gl.lookfor('家電').getGeoJSON();
const res1 = gl.match('家電').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand Down Expand Up @@ -237,7 +189,7 @@ describe('The first test', () => {
it('json as argument', () => {
try {
const gl = new GeoJsonlookfor(json);
const res1 = gl.lookfor('家電').getGeoJSON();
const res1 = gl.match('家電').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand Down Expand Up @@ -268,7 +220,7 @@ describe('The first test', () => {
it('csv as argument', () => {
try {
const gl = new GeoJsonlookfor(csv);
const res1 = gl.lookfor('家電').getGeoJSON();
const res1 = gl.match('家電').getGeoJSON();
assert.deepEqual( {
"type": "FeatureCollection",
"features": [
Expand All @@ -294,6 +246,4 @@ describe('The first test', () => {
}

});


});
Loading

0 comments on commit 50a4cf4

Please sign in to comment.