Comparing version 0.0.1 to 0.0.2
@@ -1,9 +0,10 @@ | ||
### polyline.decode(string) | ||
### polyline.decode(string[, precision]) | ||
Takes a string representation of 1+ coordinate pairs | ||
and returns an array of lat, lon arrays. | ||
and returns an array of lat, lon arrays. If not specified, | ||
precision defaults to 5. | ||
### polyline.encode(array) | ||
### polyline.encode(array[, precision]) | ||
Takes an array of lat, lon arrays and returns an encoded | ||
string. | ||
string. If not specified, precision defaults to 5. |
@@ -5,3 +5,3 @@ { | ||
"description": "Polyline encoding and decoding", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -8,4 +8,4 @@ var polyline = {}; | ||
function encode(coordinate) { | ||
coordinate = Math.round(coordinate * 1e5); | ||
function encode(coordinate, factor) { | ||
coordinate = Math.round(coordinate * factor); | ||
coordinate <<= 1; | ||
@@ -26,4 +26,3 @@ if (coordinate < 0) { | ||
// https://github.com/DennisOSRM/Project-OSRM-Web/blob/master/WebContent/routing/OSRM.RoutingGeometry.js | ||
polyline.decode = function(str) { | ||
polyline.decode = function(str, precision) { | ||
var index = 0, | ||
@@ -37,3 +36,4 @@ lat = 0, | ||
latitude_change, | ||
longitude_change; | ||
longitude_change, | ||
factor = Math.pow(10, -(precision || 5)); | ||
@@ -71,5 +71,3 @@ // Coordinates have variable length when encoded, so just keep | ||
var precision = Math.pow(10, -5); | ||
coordinates.push([lat * precision, lng * precision]); | ||
coordinates.push([lat * factor, lng * factor]); | ||
} | ||
@@ -80,11 +78,12 @@ | ||
polyline.encode = function(coordinates) { | ||
polyline.encode = function(coordinates, precision) { | ||
if (!coordinates.length) return ''; | ||
var output = encode(coordinates[0][0]) + encode(coordinates[0][1]); | ||
var factor = Math.pow(10, precision || 5), | ||
output = encode(coordinates[0][0], factor) + encode(coordinates[0][1], factor); | ||
for (var i = 1; i < coordinates.length; i++) { | ||
var a = coordinates[i], b = coordinates[i - 1]; | ||
output += encode(a[0] - b[0]); | ||
output += encode(a[1] - b[1]); | ||
output += encode(a[0] - b[0], factor); | ||
output += encode(a[1] - b[1], factor); | ||
} | ||
@@ -91,0 +90,0 @@ |
@@ -5,2 +5,4 @@ var assert = require('assert'), | ||
describe('polyline', function() { | ||
var example = [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]; | ||
function compare(coords, against) { | ||
@@ -20,10 +22,8 @@ function a(x) { return Math.round(x * 100) / 100; } | ||
it('decodes a String into an Array of lat/lon pairs', function() { | ||
var coords = polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); | ||
var against = [ | ||
[38.5, -120.2], | ||
[40.7, -120.95], | ||
[43.252, -126.453] | ||
]; | ||
compare(coords, against); | ||
compare(polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'), example); | ||
}); | ||
it('decodes with a custom precision', function() { | ||
compare(polyline.decode('_izlhA~rlgdF_{geC~ywl@_kwzCn`{nI', 6), example); | ||
}); | ||
}); | ||
@@ -37,5 +37,9 @@ | ||
it('encodes an Array of lat/lon pairs into a String', function() { | ||
assert.equal(polyline.encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]), '_p~iF~ps|U_ulLnnqC_mqNvxq`@'); | ||
assert.equal(polyline.encode(example), '_p~iF~ps|U_ulLnnqC_mqNvxq`@'); | ||
}); | ||
it('encodes with a custom precision', function() { | ||
assert.equal(polyline.encode(example, 6), '_izlhA~rlgdF_{geC~ywl@_kwzCn`{nI'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
25998
106
0