Skip to content

Commit

Permalink
Atlas #2 - Refactoring TypesenseUtils toFeature to use turfjs; Adding…
Browse files Browse the repository at this point in the history
… toFeatureCollection
  • Loading branch information
dleadbetter committed Mar 18, 2024
1 parent 8a2134b commit ae98e67
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/core-data/src/utils/Typesense.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow

import { feature, featureCollection, point } from '@turf/turf';
import { history } from 'instantsearch.js/es/lib/routers';
import TypesenseInstantsearchAdapter from 'typesense-instantsearch-adapter';
import _ from 'underscore';
import type { RuntimeConfig } from '../types/RuntimeConfig';
import type { TypesenseSearchResult } from '../types/typesense/SearchResult';

Expand Down Expand Up @@ -88,10 +90,16 @@ const createTypesenseAdapter = (config: RuntimeConfig, options = {}) => (
*/
const normalizeResults = (results: Array<TypesenseSearchResult>) => results.filter((h) => h.coordinates);

const toFeature = (result: TypesenseSearchResult) => ({
id: parseInt(result.record_id, 10),
type: 'Feature',
properties: {
const toFeature = (result: TypesenseSearchResult) => {
let geometry;

if (result.coordinates) {
geometry = point(result.coordinates);
} else {
geometry = result.geometry;
}

const properties = {
id: result.record_id,
ccode: [],
title: result.name,
Expand All @@ -100,17 +108,22 @@ const toFeature = (result: TypesenseSearchResult) => ({
name: result.name,
names: result.names.map((toponym: string) => ({ toponym })),
type: result.type
},
geometry: {
type: 'Point',
coordinates: result.coordinates.slice().reverse()
}
});
};

const id = parseInt(result.record_id, 10);

return feature(geometry, properties, { id });
};

const toFeatureCollection = (results: Array<TypesenseSearchResult>) => (
featureCollection(_.map(results, toFeature))
);

export default {
createCachedHits,
createRouting,
createTypesenseAdapter,
normalizeResults,
toFeature
toFeature,
toFeatureCollection
};

0 comments on commit ae98e67

Please sign in to comment.