Skip to content

This utility verifies the validity of geographic coordinates

License

Notifications You must be signed in to change notification settings

fitiskin/is-valid-coords

Folders and files

NameName
Last commit message
Last commit date

Latest commit

12268b7 · Oct 6, 2024

History

57 Commits
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Jan 6, 2022
Oct 6, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024
Oct 5, 2024

Repository files navigation

isValidCoords

Workflow status npm version Package size License

A small, dependency-free utility function for validating coordinates, written in TypeScript.

Install

npm

npm install --save is-valid-coords

yarn

yarn add is-valid-coords

Usage

These two utilities could be helpful in situations where you receive coordinates from a third-party system without strict typing, and you prefer to validate them before passing them on or normalizing them into a specific format for further manipulation.

isValidCoords

isValidCoords utility supports various input parameters and ensures that the latitude is a number between -90 and 90, and the longitude is a number between -180 and 180.

import isValidCoords from "is-valid-coords";

// true
isValidCoords(55.7558, 37.6173);
isValidCoords([55.7558, 37.6173]);
isValidCoords("55.7558,37.6173");
isValidCoords({ lat: 55.7558, lng: 37.6173 });
isValidCoords({ lat: 55.7558, lon: 37.6173 });
isValidCoords({ lat: 55.7558, long: 37.6173 });
isValidCoords({ latitude: 55.7558, longitude: 37.6173 });

// false
isValidCoords();
isValidCoords(-100, 200);
isValidCoords([]);
isValidCoords('');
isValidCoords(null);
isValidCoords({});

getValidCoords

getValidCoords accepts the same input parameters as isValidCoords and performs the same checks, but it returns an array with coordinates like [latitude, longitude] for valid input, and null for invalid input.

import { getValidCoords } from "is-valid-coords";

const coords1 = getValidCoords("55.7558, 37.6173"); // [55.7558, 37.6173]
const coords2 = getValidCoords(100, 500); // null

Variants

// number arguments
isValidCoords(55.7558, 37.6173);

// array
isValidCoords([55.7558, 37.6173]);

// string
isValidCoords("55.7558,37.6173");

// object with properties:
// - latitude, lat
// - longitude, lng, lon, long
isValidCoords({
  latitude: 55.7558,
  longitude: 37.6173,
});