Skip to content

Commit

Permalink
Simplify url decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nerik committed Oct 10, 2023
1 parent 27b9162 commit 88e0ef2
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions app/scripts/utils/polygon-url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Feature, FeatureCollection, Polygon } from 'geojson';
import gjv from 'geojson-validation';
import { chunk } from 'lodash';
import { decode, encode } from 'google-polyline';
import { AoIFeature } from '$components/common/map/types';
import { toAoIid } from '$components/common/map/utils';
Expand Down Expand Up @@ -75,17 +76,10 @@ export function encodeAois(aois: AoIFeature[]): string {

export function decodeAois(aois: string): AoIFeature[] {
const decoded = JSON.parse(aois) as string[];
const features = decoded.reduce<AoIFeature[]>((acc, current, i) => {
if (i % 3 === 0) {
const decodedFeature = decodeFeature(current) as AoIFeature;
return [...acc, decodedFeature];
} else {
const lastFeature = acc[acc.length - 1];
const prop = i % 3 === 1 ? 'id' : 'selected';
const newFeature = { ...lastFeature, [prop]: current };
acc[acc.length - 1] = newFeature;
return acc;
}
}, []);
const features: AoIFeature[] = chunk(decoded, 3).map((data) => {
const [polygon, id, selected] = data;
const decodedFeature = decodeFeature(polygon) as AoIFeature;
return { ...decodedFeature, id, selected };
});
return features!;
}

0 comments on commit 88e0ef2

Please sign in to comment.