Giter Club home page Giter Club logo

dms-js's Introduction

dms-conversion

A JavaScript library for converting between decimal degrees and degrees, minutes, and seconds (DMS).

npm version npm license

Node.js CI GitHub issues GitHub

Installation

npm install dms-conversion

Example

TypeScript example (from Jasmine test)

import DmsCoordinates, { parseDms } from "dms-conversion";

describe("DmsCoordinates", () => {
  const long = -122.902336120571;
  const lat = 46.9845854731319;

  it(`(${lat}, ${long}) coordinates should be ~ 46°59′5″ N, 122°54′8″ W`, () => {
    const dmsCoords = new DmsCoordinates(lat, long);
    expect(dmsCoords instanceof DmsCoordinates).toBe(true);

    const { longitude, latitude } = dmsCoords.dmsArrays;

    let [d, m, s, nsew] = longitude;
    expect(d).toBe(122);
    expect(m).toBe(54);
    expect(Math.round(s)).toBe(8);
    expect(nsew).toBe("W");

    [d, m, s, nsew] = latitude;

    expect(d).toBe(46);
    expect(m).toBe(59);
    expect(Math.round(s)).toBe(5);
    expect(nsew).toBe("N");

    expect(dmsCoords.toString()).toMatch(/46°59′4.\d+″ N, 122°54′8.\d+″ W/i);
  });

  it("Regexp should work", () => {
    const v = ["46°59′5″ N", "122°54′8″ W"];
    v.forEach((s) => expect(s.match(DmsCoordinates.dmsRe)).toBeTruthy(true));
    const [x, y] = v.map(parseDms);
    expect(typeof x).toEqual("number");
    expect(typeof y).toEqual("number");
  });

  it("Invalid numbers should throw exception", () => {
    const x = parseDms("");
    expect(isNaN(x)).toBe(true);
    expect(() => {
      const dmsc = new DmsCoordinates(lat, x);
    }).toThrowError(RangeError);
    expect(() => {
      const dmsc = new DmsCoordinates("this should fail" as any, long);
    }).toThrowError(TypeError);
  });
});

Alternatives

nerik/formatcoords

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.