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

geonames.js

v3.0.6

Published

REST api to fetch countries, regions, cities etc. A flexible library for browser and node.js usage built on top http://www.geonames.org/

Downloads

7,263

Readme

geonames.js v3.0.3 NEW (see changelog)

geonames.js is a flexible library for browser and Nodejs built on top geonames.org webservices.

It provides a simple API to dynamically fetch countries, states, regions, cities and other milion geographical names.

img

1. Installation

npm install --save geonames.js

or

yarn add geonames.js

2. Requirements

You have to register (it's free) on Geonames.org in order to get the username that will be necessary for the API to work.

geonames.js depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can use a polyfill.

3. Usage:

You can fetch almost anything by taking advantage of the huge amount of information provided by geonames.org. It contains over 10 million geographical names and consists of over 9 million unique features whereof 2.8 million populated places and 5.5 million alternate names.

The list of available options in the API is in here under the webservice column.

  • Import the library:

    • server usage (NodeJS)
       const { Geonames } = require('geonames.js') /* commonJS */
    • browser usage (React, Angular, Vue etc.)
       import Geonames from 'geonames.js'; /* es module */
  • Usage:

    Initialize the Geoames using your settings:

    free WS

    const geonames = Geonames({
      username: 'myusername',
      lan: 'en',
      encoding: 'JSON'
    });

    commercial WS

    To use the commercial tier just define your token:

    const geonames = Geonames({
      username: 'myusername',
      token: 'mytoken',
      lan: 'en',
      encoding: 'JSON'
    });

    Since the library return promises, you can use either async/await or promise-based syntax

    plain call

    // async/await
    try{
      const continents = await geonames.search({q: 'CONT'}) //get continents
    }catch(err){
      console.error(err);
    }
      
    // promise
    geonames.search({q: 'CONT'}) //get continents
    .then(resp => {
      console.log(resp.geonames);
    })
    .catch(err => console.error(err));

    chaining calls

    // async/await
    try{
      const countries = await geonames.countryInfo({}) //get continents
      const states = await geonames.children({geonameId: countries.geonames[0].geonameId})
      const regions = await geonames.children({geonameId: states.geonames[0].geonameId});
      const cities = await geonames.children({geonameId: regions.geonames[0].geonameId});
      console.log(cities.geonames);
    }catch(err){
      console.error(err);
    }
    
    // promise
    geonames.countryInfo({}) 
    .then(countries => {
      return geonames.children({geonameId: countries.geonames[0].geonameId})
    })
    .then(states => {
      return geonames.children({geonameId: states.geonames[0].geonameId});
    })
    .then(regions => {
      return geonames.children({geonameId: regions.geonames[0].geonameId});
    })
    .then(cities => {
      console.log(cities.geonames);
    })
    .catch(err => console.log(err));

4. Contribution:

Feel free to contribute; any help is really appreciated :)

run with:

yarn build-dev (dev bundle)
yarn build (prod bundle)
yarn build:all (both - for packaging)
USERNAME=myusername yarn test (unit testing)

5. Changelog v3.0.3:

  • Added support for latest Geonames api (address, geoCodeAddress, streetNameLookup)
  • Added typescript autocomplete for all the api's
  • change function constructor to plain function
  • updated dependencies

img

6. License:

MIT 2017 License Karim Abdelcadir