-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor core loo compression and filtering methods into their …
…own library files (#1259) Also fix the method of reducing down the loos array according to the enabled filters
- Loading branch information
Showing
13 changed files
with
174 additions
and
143 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Loo } from '../api-client/graphql'; | ||
import { Filter } from '../config'; | ||
import { LooProperties } from './loo'; | ||
|
||
export enum FILTER_TYPE { | ||
NO_PAYMENT = 0b00000001, | ||
ALL_GENDER = 0b00000010, | ||
AUTOMATIC = 0b00000100, | ||
ACCESSIBLE = 0b00001000, | ||
BABY_CHNG = 0b00010000, | ||
RADAR = 0b00100000, | ||
} | ||
|
||
export const genLooFilterBitmask = (loo: { properties: LooProperties }) => { | ||
const { | ||
noPayment = 0, | ||
allGender = 0, | ||
automatic = 0, | ||
accessible = 0, | ||
babyChange = 0, | ||
radar = 0, | ||
} = loo.properties; | ||
|
||
const noPaymentValue = noPayment ? FILTER_TYPE.NO_PAYMENT : 0; | ||
const allGenderValue = allGender ? FILTER_TYPE.ALL_GENDER : 0; | ||
const automaticValue = automatic ? FILTER_TYPE.AUTOMATIC : 0; | ||
const accessibleValue = accessible ? FILTER_TYPE.ACCESSIBLE : 0; | ||
const babyChangeValue = babyChange ? FILTER_TYPE.BABY_CHNG : 0; | ||
const radarValue = radar ? FILTER_TYPE.RADAR : 0; | ||
|
||
return ( | ||
noPaymentValue | | ||
allGenderValue | | ||
automaticValue | | ||
accessibleValue | | ||
babyChangeValue | | ||
radarValue | ||
); | ||
}; | ||
|
||
export const getAppliedFiltersAsFilterTypes = (filters: Filter[]) => { | ||
return filters.reduce((p, c) => { | ||
switch (c.id) { | ||
case 'accessible': | ||
p.push(FILTER_TYPE.ACCESSIBLE); | ||
break; | ||
case 'allGender': | ||
p.push(FILTER_TYPE.ALL_GENDER); | ||
break; | ||
case 'automatic': | ||
p.push(FILTER_TYPE.AUTOMATIC); | ||
break; | ||
case 'babyChange': | ||
p.push(FILTER_TYPE.BABY_CHNG); | ||
break; | ||
case 'noPayment': | ||
p.push(FILTER_TYPE.NO_PAYMENT); | ||
break; | ||
case 'radar': | ||
p.push(FILTER_TYPE.RADAR); | ||
break; | ||
} | ||
return p; | ||
}, [] as Array<FILTER_TYPE>); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Loo } from '../api-client/graphql'; | ||
import { FILTER_TYPE, genLooFilterBitmask } from './filter'; | ||
|
||
export type LooProperties = Omit<Loo, '__typename'> & { | ||
geometry: { | ||
coordinates: [number, number]; | ||
}; | ||
}; | ||
|
||
type CompressedLooString = `${string}|${string}|${string}|${number}`; | ||
|
||
type CompressedLooObject = { | ||
id: string; | ||
location: { | ||
lng: number; | ||
lat: number; | ||
}; | ||
filterBitmask: number; | ||
}; | ||
|
||
export const stringifyAndCompressLoos = ( | ||
loos: { id?: string; properties: LooProperties }[] | ||
): CompressedLooString[] => | ||
loos.map((loo) => { | ||
const id = loo.id; | ||
const lat = loo.properties.geometry.coordinates[0].toFixed(4); | ||
const lng = loo.properties.geometry.coordinates[1].toFixed(4); | ||
const filterMask = genLooFilterBitmask(loo); | ||
return `${id}|${lat}|${lng}|${filterMask}` as CompressedLooString; | ||
}); | ||
|
||
export const parseCompressedLoo = ( | ||
compressedLoo: CompressedLooString | ||
): CompressedLooObject => { | ||
const loo = compressedLoo.split('|'); | ||
return { | ||
id: loo[0], | ||
location: { | ||
lng: parseFloat(loo[1]), | ||
lat: parseFloat(loo[2]), | ||
}, | ||
filterBitmask: parseInt(loo[3], 10), | ||
}; | ||
}; | ||
|
||
export const filterCompressedLooByAppliedFilters = ( | ||
compressedLoo: CompressedLooObject, | ||
appliedFilters: Array<FILTER_TYPE> | ||
) => { | ||
if (appliedFilters.length === 0) { | ||
return true; | ||
} | ||
|
||
// Check that the loo passes all of the applied filters. | ||
const passesAll = appliedFilters.reduce((previousValue, currentFilter) => { | ||
const currentFilterInMask = | ||
(compressedLoo.filterBitmask & currentFilter) !== 0; | ||
return previousValue && currentFilterInMask; | ||
}, true); | ||
|
||
return passesAll; | ||
}; |