app/javascript/packages/address-search/utils.ts
import type { PostOffice, FormattedLocation } from './types';
export const formatLocations = (postOffices: PostOffice[]): FormattedLocation[] =>
postOffices.map((po: PostOffice, index) => ({
formattedCityStateZip: `${po.city}, ${po.state}, ${po.zip_code_5}-${po.zip_code_4}`,
id: index,
distance: po.distance,
name: po.name,
saturdayHours: po.saturday_hours,
streetAddress: po.address,
sundayHours: po.sunday_hours,
weekdayHours: po.weekday_hours,
isPilot: !!po.is_pilot,
}));
export const snakeCase = (value: string) =>
value
.split(/(?=[A-Z])/)
.join('_')
.toLowerCase();
// snake case the keys of the location
export const transformKeys = (location: object, predicate: (key: string) => string) =>
Object.keys(location).reduce(
(acc, key) => ({
[predicate(key)]: location[key],
...acc,
}),
{},
);