Skip to content

Commit

Permalink
Merge pull request #24 from tooreht/feat/rounding
Browse files Browse the repository at this point in the history
Add precision param to DmsCoordinates.toString
  • Loading branch information
JeffJacobson committed Jan 24, 2024
2 parents ccff563 + cbe0049 commit 6fc462c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
12 changes: 6 additions & 6 deletions dms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ export class Dms {
}
/**
* Returns the DMS value as a string.
* @param {number} [precision] - number of digits after the decimal point in seconds
* @param {number} [precision] - number of digits after the decimal point in seconds
* @returns {string}
*/
public toString(precision?: number): string {
const dmsArray = this.getDmsArray();
const second = isNaN(Number(precision)) ? dmsArray[2] : dmsArray[2].toFixed(precision);
return `${dmsArray[0]}°${dmsArray[1]}${second}${dmsArray[3]}`;
const second = isNaN(Number(precision)) ? this.dmsArray[2] : this.dmsArray[2].toFixed(precision);
return `${this.dmsArray[0]}°${this.dmsArray[1]}${second}${this.dmsArray[3]}`;
}
}

Expand Down Expand Up @@ -176,10 +175,11 @@ export default class DmsCoordinates {

/**
* Returns the coordinates to a comma-separated string.
* @param {number} [precision] - number of digits after the decimal point in seconds
* @returns {string}
*/
public toString() {
return [this.latitude, this.longitude].join(", ");
public toString(precision?: number) {
return [this.latitude, this.longitude].map(dms => dms.toString(precision)).join(", ");
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dms-conversion",
"version": "3.1.3",
"version": "3.1.4",
"description": "A JavaScript library for converting between decimal degrees and degrees, minutes, and seconds (DMS).",
"main": "dist/node/dms.js",
"browser": "dist/browser/dms.js",
Expand Down
40 changes: 28 additions & 12 deletions spec/dmsSpec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DmsCoordinates, { parseDms } from "../dms";
import DmsCoordinates, { Dms, parseDms } from "../dms";

describe("DmsCoordinates", () => {
const long = -122.902336120571;
Expand Down Expand Up @@ -60,17 +60,33 @@ describe("DmsCoordinates", () => {
expect(dmsCoordsWest[1]).toBeLessThan(0);
});

describe('toString() rounding', ()=>{
describe('Dms#toString() rounding', () => {
let dmsCoord: Dms;
beforeEach(() => {
dmsCoord = new Dms(lat, 'lat');
});

it('should be able to round digits in second', () => {
expect(dmsCoord.toString(3)).toEqual('46°59′4.508″ N');
});

it('should not round when precision not provided', () => {
expect(dmsCoord.toString()).toMatch(/46°59′4.50770327\d+″ N/i);
});
});

describe('DmsCoordinates#toString() rounding', () => {
let dmsCoords: DmsCoordinates;
beforeEach(()=>{
beforeEach(() => {
dmsCoords = new DmsCoordinates(lat, long);
})
it('should be able to round digits in second', ()=>{
expect(dmsCoords.latitude.toString(3)).toEqual('46°59′4.508″ N');
})

it('should not round when precision not provided', ()=>{
expect(dmsCoords.latitude.toString()).toMatch(/46°59′4.50770327\d+″ N/i);
})
})
});

it('should be able to round digits in second', () => {
expect(dmsCoords.toString(3)).toEqual('46°59′4.508″ N, 122°54′8.410″ W');
});

it('should not round when precision not provided', () => {
expect(dmsCoords.toString()).toMatch(/46°59′4.50770327\d+″ N, 122°54′8.41003405\d+″ W/i);
});
});
});

0 comments on commit 6fc462c

Please sign in to comment.