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

precise-locations

v1.0.3

Published

A package that provides locations with their precise coordinates

Downloads

7

Readme

Precise Locations

This project provides a robust and feature-rich Node.js package for managing locations and their associated geographic coordinates. The API includes functionalities for retrieving all locations, fetching coordinates by location name, validating location data, calculating distances between locations, and logging operations.

Table of Contents

Installation

Prerequisites

Ensure you have Node.js installed on your machine. You can download it from here.

Steps

  1. Clone the repository:

    git clone https://github.com/anesu398/precise-locations.git
    cd precise-locations
  2. Install the necessary dependencies:

    npm install
  3. Ensure that the locations.json file exists in the root directory and contains valid location data.

Usage

Starting the API

To start the API, simply run:

node index.js

This will start the API server on the configured port.

Example Usage

You can interact with the API using any HTTP client like curl, Postman, or your browser.

Fetch all locations:

curl http://localhost:3000/locations

Fetch coordinates by location name:

curl http://localhost:3000/locations/coordinates?name=LocationName

Features

  • Retrieve All Locations: Get a list of all available locations with their respective coordinates.
  • Fetch Coordinates by Name: Retrieve the geographic coordinates (latitude and longitude) of a location by its name.
  • Data Validation: Ensure the integrity of location data with validation functions.
  • Distance Calculation: Calculate the distance between two locations using the Haversine formula.
  • Robust Logging: Log all significant operations and errors for easy debugging and monitoring.

API Endpoints

GET /locations Description: Retrieves a list of all locations with their coordinates.

Response:

[
  {
    "name": "Bulawayo",
    "lat": -20.132499,
    "lon": 28.626479,
    "region": "Matabeleland"
  },
  ...
]

GET /locations/coordinates Description: Retrieves the coordinates of a specific location by name.

Query Parameters:

  • name: The name of the location (case-insensitive). Response:
{
  "lat": 12.34,
  "lon": 56.78
}

Error Response:

If the location is not found:

{
  "error": "Location not found"
}

Utilities

utils.js Contains utility functions for validation and distance calculation.

  • validateLocationData(location): Validates the structure of location data.
  • calculateDistance(coords1, coords2): Calculates the distance between two coordinates using the Haversine formula. Example:
const { validateLocationData, calculateDistance } = require('./utils');

// Validate location data for a city in Zimbabwe
const isValid = validateLocationData({ name: "Harare", lat: -17.8251657, lon: 31.03351 });
console.log(`Is Harare data valid? ${isValid}`); // true

// Calculate distance between two cities in Zimbabwe
const distance = calculateDistance(
    { lat: -17.8251657, lon: 31.03351 }, // Harare
    { lat: -20.132499, lon: 28.626479 }   // Bulawayo
);
console.log(`Distance between Harare and Bulawayo: ${distance} km`);

Logging

logger.js Handles logging for the application.

  • logInfo(message): Logs an informational message.
  • logError(message): Logs an error message.
  • Logs are stored in the app.log file in the root directory.

Example:

const { logInfo, logError } = require('./logger');

logInfo('Server started successfully');
logError('Failed to retrieve location data');

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your proposed changes.

Guidelines

  • Ensure your code is well-documented.
  • Write tests for any new features or changes.
  • Follow the project's coding style.

License

This project is licensed under the MIT License. See the file for more details.