var polyline = {};

// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)
//
// Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js)
// by [Mark McClure](http://facstaff.unca.edu/mcmcclur/)

function encode(coordinate, factor) {
    coordinate = Math.round(coordinate * factor);
    coordinate <<= 1;
    if (coordinate < 0) {
        coordinate = ~coordinate;
    }
    var output = '';
    while (coordinate >= 0x20) {
        output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);
        coordinate >>= 5;
    }
    output += String.fromCharCode(coordinate + 63);
    return output;
}

// This is adapted from the implementation in Project-OSRM
// https://github.com/DennisOSRM/Project-OSRM-Web/blob/master/WebContent/routing/OSRM.RoutingGeometry.js
polyline.decode = function(str, precision) {
    var index = 0,
        lat = 0,
        lng = 0,
        coordinates = [],
        shift = 0,
        result = 0,
        byte = null,
        latitude_change,
        longitude_change,
        factor = Math.pow(10, precision || 5);

    // Coordinates have variable length when encoded, so just keep
    // track of whether we've hit the end of the string. In each
    // loop iteration, a single coordinate is decoded.
    while (index < str.length) {

        // Reset shift, result, and byte
        byte = null;
        shift = 0;
        result = 0;

        do {
            byte = str.charCodeAt(index++) - 63;
            result |= (byte & 0x1f) << shift;
            shift += 5;
        } while (byte >= 0x20);

        latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

        shift = result = 0;

        do {
            byte = str.charCodeAt(index++) - 63;
            result |= (byte & 0x1f) << shift;
            shift += 5;
        } while (byte >= 0x20);

        longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

        lat += latitude_change;
        lng += longitude_change;

        coordinates.push([lat / factor, lng / factor]);
    }

    return coordinates;
};

polyline.encode = function(coordinates, precision) {
    if (!coordinates.length) return '';

    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], factor);
        output += encode(a[1] - b[1], factor);
    }

    return output;
};

polyline.fromGeoJSON = function(geojson, precision) {
    if (!(geojson && geojson.geometry && geojson.geometry.type === 'LineString') && geojson.type != 'LineString') {
        throw new Error('Input must be a GeoJSON LineString');
    }
    var coords;
    if (geojson.geometry) {
        coords = geojson.geometry.coordinates;
    } else {
        coords = geojson.coordinates;
    }
    var flipped = [];
    for (var i = 0; i < coords.length; i++) {
        flipped.push(coords[i].slice().reverse());
    }
    return polyline.encode(flipped, precision);
};

if (typeof module === 'object' && module.exports) module.exports = polyline;

@@ -2690,18 +2690,16 @@
 %0A    if 
-(!
 (geojson
@@ -2714,62 +2714,91 @@
 son.
-geometry && geojson.geometry.type === 'LineString') &&
+type === 'Feature') %7B%0A        geojson = geojson.geometry;%0A    %7D%0A    if (!geojson %7C%7C
  geo
@@ -2809,16 +2809,17 @@
 .type !=
+=
  'LineSt
@@ -2908,112 +2908,8 @@
 var 
-coords;%0A    if (geojson.geometry) %7B%0A        coords = geojson.geometry.coordinates;%0A    %7D else %7B%0A        
 coor
@@ -2934,22 +2934,16 @@
 inates;%0A
-    %7D%0A
     var 

clean up validation