Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unnecessary deduplication #8

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@klarna/geofences-reducer",
"version": "1.1.0",
"version": "1.1.1",
"description": "Reduces overlapping geofences",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 3 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,17 @@ export function reduce (
if (config) validateConfig(config)

if (geofences.length === 0) return geofences

const geofencesDeduplicated: Geofence[] = deduplicate(geofences)

if (geofencesDeduplicated.length === 1) return geofencesDeduplicated

const geohashes: string[][] = convertToGeohash(geofencesDeduplicated, config.precision)
const geohashes: string[][] = convertToGeohash(geofences, config.precision)

const geofencesReduced: Geofence[] = []
const geofencesReducedIndexes: number[] = []
const geofencesRemoved: Geofence[] = []
const geohashesRemoved: string[][] = []

// Find geofences with unique geohashes
for (let index = 0; index < geofencesDeduplicated.length; index++) {
const geofence: Geofence = geofencesDeduplicated[index]
for (let index = 0; index < geofences.length; index++) {
const geofence: Geofence = geofences[index]
const geofenceGeohashes: string[] = geohashes[index]

const geohashesCopy: string[][] = geohashes.slice()
Expand Down Expand Up @@ -112,21 +108,6 @@ function validateConfig(config: { precision: number }) {
}
}

function deduplicate(geofences: Geofence[]): Geofence[] {
return geofences.filter((geofence, index, geofences) => {
return index === geofences.findIndex(geofenceFound =>
areGeofencesEqual(geofenceFound, geofence)
)
})
}

function areGeofencesEqual(subject: Geofence, target: Geofence): boolean {
if (subject.latitude !== target.latitude) return false
if (subject.longitude !== target.longitude) return false
if (subject.radius !== target.radius) return false
return true
}

function convertToGeohash(geofences: Geofence[], precision: number) {
return geofences.map(geofence =>
vicinityhash.convert(
Expand Down
5 changes: 5 additions & 0 deletions tests/geofences.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"longitude": 13.96389,
"radius": 40000
},
{
"latitude": 48.64989,
"longitude": 13.96389,
"radius": 10000
},
{
"latitude": 48.32279,
"longitude": 14.2888,
Expand Down
Loading