davglass / zipcodes

Zipcode lookup node module
Other
156 stars 83 forks source link

Lookup by code #22

Open ChuckJonas opened 6 years ago

ChuckJonas commented 6 years ago

Would be nice if there was a way to list codes based on a string compare of the code itsself.

EG: zipcodes.lookupByCode('8300'); would return 83001, 83002, 18300, 28300, etc

ChuckJonas commented 6 years ago

I was able to just use the codes property to achieve this:

import * as zipcodes from 'zipcodes';

const codeNames = Object.keys(zipcodes.codes);

export const findMatches = (search: string) => {
    const matchingCodes = codeNames.filter((z) => z.includes(search));
    return matchingCodes.map((m) => zipcodes.codes[m]);
};