Source: bemap-services-v2/routing/bemap-routing-instruction.js

/**
 * BeNomad BeMap JavaScript API - RoutingInstruction
 *
 * One turn-by-turn instruction in a `RoutingRoute.routingInstructions` array.
 *
 * Wire shape from the BeMap server (cross-checked against an actual response
 * payload):
 *   {
 *     type:                "FOLLOW" | "ENTER_ROUNDABOUT" | "EXIT_ROUNDABOUT"
 *                          | "ENTER_MOTORWAY" | "EXIT_MOTORWAY" | "TAKE_RAMP"
 *                          | "STOP" | "DEPART" | "ARRIVE" | "U_TURN" | ...
 *     geoElementType:      "ROAD" | "ROUNDABOUT" | "MOTORWAY" | "SLIP_ROAD" | ...
 *     manoeuvre:           "STRAIGHT" | "LEFT" | "RIGHT" | "BEAR_LEFT" | "BEAR_RIGHT"
 *                          | "SLIGHT_LEFT" | "SLIGHT_RIGHT" | "U_TURN"
 *     length:              long           // segment length in metres
 *     duration:            long           // segment duration in seconds
 *     coordinate:          { lon, lat }   // instruction point
 *     roundAboutExitNumber:int            // 0 unless type is ENTER_/EXIT_ROUNDABOUT
 *     fromName:            string         // name of the road we're on
 *     toName:              string         // name of the next road
 *     toOn:                string         // additional "on <name>" hint
 *     toRn:                string         // road number ("D955", "A19/E60")
 *     toSi:                string         // sign info / exit identifier
 *     textDist:            string         // human-readable distance ("At 1.6 kilometer")
 *     text:                string         // human-readable instruction
 *   }
 *
 * @since 1.5.0
 * @public
 * @constructor
 */
bemap.RoutingInstruction = function(options) {
    var opts = options || {};

    // Server-canonical fields
    this.type = opts.type || null;
    this.geoElementType = opts.geoElementType || null;
    this.manoeuvre = opts.manoeuvre || null;
    this.length = (typeof opts.length === 'number') ? opts.length : null;
    this.duration = (typeof opts.duration === 'number') ? opts.duration : null;
    this.coordinate = opts.coordinate || null;
    this.roundAboutExitNumber = (typeof opts.roundAboutExitNumber === 'number') ? opts.roundAboutExitNumber : 0;
    this.fromName = opts.fromName || null;
    this.toName = opts.toName || null;
    this.toOn = opts.toOn || null;
    this.toRn = opts.toRn || null;
    this.toSi = opts.toSi || null;
    this.textDist = opts.textDist || null;
    this.text = opts.text || null;
};

/* Server-canonical getters */
bemap.RoutingInstruction.prototype.getType = function() { return this.type; };
bemap.RoutingInstruction.prototype.getGeoElementType = function() { return this.geoElementType; };
bemap.RoutingInstruction.prototype.getManoeuvre = function() { return this.manoeuvre; };
bemap.RoutingInstruction.prototype.getLength = function() { return this.length; };
bemap.RoutingInstruction.prototype.getDuration = function() { return this.duration; };
bemap.RoutingInstruction.prototype.getCoordinate = function() { return this.coordinate; };
bemap.RoutingInstruction.prototype.getRoundAboutExitNumber = function() { return this.roundAboutExitNumber; };
bemap.RoutingInstruction.prototype.getFromName = function() { return this.fromName; };
bemap.RoutingInstruction.prototype.getToName = function() { return this.toName; };
bemap.RoutingInstruction.prototype.getToOn = function() { return this.toOn; };
bemap.RoutingInstruction.prototype.getToRn = function() { return this.toRn; };
bemap.RoutingInstruction.prototype.getToSi = function() { return this.toSi; };
bemap.RoutingInstruction.prototype.getTextDist = function() { return this.textDist; };
bemap.RoutingInstruction.prototype.getText = function() { return this.text; };

/* Compatibility aliases (older naming) */
bemap.RoutingInstruction.prototype.getDistanceM = function() { return this.length; };
bemap.RoutingInstruction.prototype.getDurationS = function() { return this.duration; };
bemap.RoutingInstruction.prototype.getStreetName = function() { return this.toName || this.fromName; };
bemap.RoutingInstruction.prototype.getExitNumber = function() { return this.roundAboutExitNumber; };
bemap.RoutingInstruction.prototype.getRoadShield = function() { return this.toRn; };

bemap.RoutingInstruction.fromJson = function(raw) {
    if (!raw) return new bemap.RoutingInstruction();
    var coord = null;
    if (raw.coordinate) {
        var lon = (raw.coordinate.lon !== undefined)
            ? raw.coordinate.lon
            : (raw.coordinate.longitude !== undefined) ? raw.coordinate.longitude : null;
        var lat = (raw.coordinate.lat !== undefined)
            ? raw.coordinate.lat
            : (raw.coordinate.latitude !== undefined) ? raw.coordinate.latitude : null;
        if (lon !== null && lat !== null) coord = new bemap.Coordinate(lon, lat);
    }
    return new bemap.RoutingInstruction({
        type: raw.type || raw.maneuverType || null,
        geoElementType: raw.geoElementType || null,
        manoeuvre: raw.manoeuvre || raw.maneuver || null,
        length: (typeof raw.length === 'number') ? raw.length : raw.distanceM,
        duration: (typeof raw.duration === 'number') ? raw.duration : raw.durationS,
        coordinate: coord,
        roundAboutExitNumber: raw.roundAboutExitNumber,
        fromName: raw.fromName || raw.streetName || null,
        toName: raw.toName || null,
        toOn: raw.toOn || null,
        toRn: raw.toRn || raw.roadShield || null,
        toSi: raw.toSi || null,
        textDist: raw.textDist || null,
        text: raw.text || raw.instruction || null
    });
};