Skip to content

Commit 8e2f931

Browse files
author
Lauren Budorick
authored
Round input floats to original precision if < 5
1 parent 0c22fdc commit 8e2f931

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/controls/geocoder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ Geocoder.prototype = {
178178
if (!input) return;
179179
if (typeof input === 'object' && input.length) {
180180
input = [
181-
utils.wrap(input[0]).toFixed(5),
182-
utils.wrap(input[1]).toFixed(5)
181+
utils.roundWithOriginalPrecision(utils.wrap(input[0]), input[0]),
182+
utils.roundWithOriginalPrecision(utils.wrap(input[1]), input[1])
183183
].join();
184184
}
185185

src/utils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function wrap(n) {
1616
return (w === -180) ? 180 : w;
1717
}
1818

19+
function roundWithOriginalPrecision(input, original) {
20+
let precision = 0;
21+
if (Math.floor(original) !== original) {
22+
precision = original.toString().split('.')[1].length;
23+
}
24+
return input.toFixed(Math.min(precision, 5));
25+
}
26+
1927
function createPoint(coordinates, properties) {
2028
return {
2129
type: 'Feature',
@@ -54,4 +62,4 @@ const format = {
5462
}
5563
};
5664

57-
export default { format, coordinateMatch, createPoint, validCoords, wrap };
65+
export default { format, coordinateMatch, createPoint, validCoords, wrap, roundWithOriginalPrecision };

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require('./test.directions');
1111
// require('./test.options');
1212
require('./test.inputs');
1313
require('./test.instructions');
14+
require('./test.utils');
1415

1516
// close the smokestack window once tests are complete
1617
test('shutdown', (t) => {

test/test.utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const test = require('tape');
4+
import utils from '../src/utils';
5+
6+
test('Directions#utils', tt => {
7+
tt.test('roundWithOriginalPrecision', (t) => {
8+
t.equal(utils.roundWithOriginalPrecision(11.1000000, 11.1), '11.1');
9+
t.equal(utils.roundWithOriginalPrecision(11.1000000, 11.1000000000), '11.1');
10+
t.equal(utils.roundWithOriginalPrecision(11.1234567, 11.1234567890), '11.12346');
11+
t.equal(utils.roundWithOriginalPrecision(11.0000000, 11), '11');
12+
13+
t.end();
14+
});
15+
16+
tt.end();
17+
});

0 commit comments

Comments
 (0)