npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-address-functions

v1.1.10

Published

google address helper functions

Downloads

1,638

Readme

google-address-functions

Functions for interacting with Google Places API to handle address details.

Installation

yarn add google-address-functions

const {
  getPlaceDetails,
  getPlaceIdFromText,
  composeAddressFromDetails,
} = require('google-address-functions');

To compose correct address type use composeAddressFromDetails:

type Address = {
  id: string;
  city: string;
  country: string;
  formattedAddress: string;
  addressNote?: string | null;
  number?: string | null;
  state?: string | null;
  street?: string | null;
  zipCode?: string | null;
  coordinates?: Array<number> | null;
};

type ComposeAddressFromDetailsArgs = {
  response: Place; // https://developers.google.com/maps/documentation/places/web-service/details#Place
  placeId: string;
}

/**
 * Composes an address from the details provided.
 *
 * @param {ComposeAddressFromDetailsArgs} args - Object containing the response from the Google Places API and a placeId.
 * @returns {Address} - The composed address object.
 *
 * @example
 * const placeDetails = {
 *   // Details from the Google Places API response
 * };
 * const placeId = "ChIJN1t_tDeuEmsRUsoyG83frY4";
 *
 * const address: Address = composeAddressFromDetails({ response: placeDetails, placeId });
 *
 * @see https://developers.google.com/maps/documentation/places/web-service/details#Place for details on the response object.
 */

const address: Address = composeAddressFromDetails<ComposeAddressFromDetailsArgs>({ response, placeId });

Autocomplete (new) functionality:

composeAddressFromDetailsNew Function

This function composeAddressFromDetailsNew transforms structured address components and location details into a standardized address format. It is particularly useful for applications integrating with Google Maps Geocoding API or similar services.

Function Signature

Parameters

  • addressComponents (Array): An array of address components obtained from a geocoding service.
  • location ({ lat: number, lng: number }): The geographic coordinates (latitude and longitude) of the place.
  • id (string): A unique identifier for the place.
  • formattedAddress (string): Full address
  • types (array): current place types array
  • displayName (string): place name

Return Value

The function returns an Address

// All this fields returned with new version of https://developers.google.com/maps/documentation/places/web-service/place-details
type ComposeAddressFromDetailsNewArg = {|
  addressComponents: Array<AddressComponent>,
  location: { lat: number, lng: number },
  id: string, // placeId
  formattedAddress: string,
  types: Array<string>,
  displayName: string,
|}

/**
 * Compose an Address object from detailed address components and location.
 * @param {ComposeAddressFromDetailsNewArg} params - The input parameters object.
 * @returns {Address} - The composed address object.
 */

const composeAddressFromDetailsNew = ({ addressComponents, location, id, formattedAddress, types, display }: ComposeAddressFromDetailsNewArg): Address

Usage

Example usage of composeAddressFromDetailsNew:

const getPlaceDetailsNewResponse = {
    id: "ChIJi1uPs_1x5kcRbh8M8XJSNMA",
    types: [
        "city_hall",
        "tourist_attraction",
        "local_government_office",
        "point_of_interest",
        "establishment"
    ],
    formattedAddress: "75004 Paris, France",
    addressComponents: [
        {
            longText: "Paris",
            shortText: "Paris",
            types: [
                "locality",
                "political"
            ],
            languageCode: "en"
        },
        {
            longText: "Paris",
            shortText: "Paris",
            types: [
                "administrative_area_level_2",
                "political"
            ],
            languageCode: "en"
        },
        {
            longText: "Île-de-France",
            shortText: "IDF",
            types: [
                "administrative_area_level_1",
                "political"
            ],
            languageCode: "en"
        },
        {
            longText: "France",
            shortText: "FR",
            types: [
                "country",
                "political"
            ],
            languageCode: "en"
        },
        {
            longText: "75004",
            shortText: "75004",
            types: [
                "postal_code"
            ],
            languageCode: "en-US"
        }
    ],
    location: {
        latitude: 48.8564826,
        longitude: 2.3524135
    }
}

const address: Address = composeAddressFromDetailsNew(...getPlaceDetailsNewResponse);
console.log(address);

The placesAutocompleteNew function:

The placesAutocompleteNew function returns methods to fetch address suggestions and details using the Google Places API.


type PlacesFunctions = {
  getSuggestions: (actualQuery: string) => Promise<SuggestionOption[]>,
  getPlaceDetails: (placeId: string) => Promise<Address>,
}

const { placesAutocompleteNew } = require('google-address-functions');

const { getSuggestions, getPlaceDetails }: PlacesFunctions = placesAutocompleteNew({
  googleApiKey: 'YOUR_GOOGLE_API_KEY',
  searchLength: 3, // after which character send request
});

getSuggestions('1600 Amphitheatre Parkway').then((suggestions) => {
  console.log(suggestions);
});

getPlaceDetails('ChIJi1uPs_1x5kcRbh8M8XJSNMA').then((address) => {
  console.log(address)
})