Skip to content

Commit

Permalink
Merge pull request #8 from klarna-incubator/remove-deduplication
Browse files Browse the repository at this point in the history
fix: remove unnecessary deduplication
  • Loading branch information
filipecorrea committed Sep 28, 2023
2 parents 7bfe2d5 + fb2777d commit 056f70a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
2 changes: 1 addition & 1 deletion 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

0 comments on commit 056f70a

Please sign in to comment.