Skip to content

Commit

Permalink
Merge pull request #17 from geolonia/add-object-search
Browse files Browse the repository at this point in the history
各検索関数に、オブジェクトを渡せるように修正
  • Loading branch information
sugama-satsuki authored Aug 2, 2024
2 parents 50a4cf4 + d4c79f2 commit d68ae58
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class GeoJsonlookfor {
/* *****************
* "keyword"を含む項目があるfeatureを検索する
* *****************/
match(keyword: string) {
match(keyword: string, geometryType?: 'Point' | 'MultiPoint' | 'LineString' | 'MultiLineString' | 'Polygon' | 'MultiPolygon') {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
Expand All @@ -31,9 +31,9 @@ export class GeoJsonlookfor {
}

/* *****************
* "keywords"配列内の文字列で検索を行う
* "keywords"配列内の文字列でOR検索を行う
* *****************/
orMatch(keywords: string[]) {
orMatch(keywords: string[] | { [key: string]: any }) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
Expand All @@ -43,7 +43,13 @@ export class GeoJsonlookfor {
this.geojson = {
"type": "FeatureCollection",
"features": features.filter((feature: any) => {
return keywords.some((keyword) => JSON.stringify(feature).includes(keyword));
if(Array.isArray(keywords)){
return (keywords as string[]).some((keyword) => JSON.stringify(feature).includes(keyword));
} else {
return Object.keys(keywords).some((key) => {
return key in feature.properties && feature.properties[key].includes((keywords as any)[key])
});
}
})
};

Expand All @@ -53,7 +59,10 @@ export class GeoJsonlookfor {
}
}

andMatch(keywords: string[]) {
/* *****************
* "keywords"配列内の文字列でAND検索を行う
* *****************/
andMatch(keywords: string[] | { [key: string]: any }) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
Expand All @@ -63,7 +72,13 @@ export class GeoJsonlookfor {
this.geojson = {
"type": "FeatureCollection",
"features": features.filter((feature: any) => {
return keywords.every((keyword) => JSON.stringify(feature).includes(keyword));
if(Array.isArray(keywords)){
return (keywords as string[]).every((keyword) => JSON.stringify(feature).includes(keyword));
} else {
return Object.keys(keywords).every((key) => {
return key in feature.properties && feature.properties[key].includes((keywords as any)[key])
});
}
})
};

Expand All @@ -74,9 +89,9 @@ export class GeoJsonlookfor {
}

/* *****************
* "keyword"を含まないfeatureを検索する
* "keyword"を含まないfeatureをNOT検索する
* *****************/
notMatch(keyword: string | string[]) {
notMatch(keywords: string | string[] | { [key: string]: any }) {
try {
if (this.geojson === undefined || this.geojson === null || typeof this.geojson !== 'object' || typeof this.geojson === 'string') {
throw new Error('Invalid GeoJSON');
Expand All @@ -86,9 +101,14 @@ export class GeoJsonlookfor {
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));
if(Array.isArray(keywords) || typeof keywords === 'string'){
const keywordArr = Array.isArray(keywords) ? keywords : [keywords];
return !keywordArr.some((keyword) => JSON.stringify(feature).includes(keyword));
} else {
return !Object.keys(keywords).some((key) => {
return key in feature.properties && feature.properties[key].includes((keywords as any)[key])
});
}
})
};

Expand All @@ -99,6 +119,7 @@ export class GeoJsonlookfor {
}



/* *****************
* geojsonを返す
* *****************/
Expand Down
22 changes: 22 additions & 0 deletions src/utile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


// const filterParticularProperty = (geojson: any, conditionProperty: {[key: string]: string}) => {
// try {
// if (geojson === undefined || geojson === null || typeof geojson !== 'object' || typeof geojson === 'string') {
// throw new Error('Invalid GeoJSON');
// }
// const features = geojson.features;
// const keys = Object.keys(conditionProperty);

// geojson = {
// "type": "FeatureCollection",
// "features": features.filter((feature: any) => {
// return feature.properties[].includes(JSON.stringify(conditionProperty));
// })
// };

// return geojson;
// } catch (err: any) {
// throw new Error(err);
// }
// };
Loading

0 comments on commit d68ae58

Please sign in to comment.