/*
Copyright (c) 2013, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License.
See the accompanying LICENSE file for terms.
*/

'use strict';

// The `pathTo()` function is written in ES3 so it's serializable and able to
// run in all JavaScript environments.
module.exports = function pathTo(routeMap) {
    return function (routeName, context) {
        var route = routeMap[routeName],
            path, keys, i, len, key, param, regex;

        if (!route) { return ''; }

        path = route.path;
        keys = route.keys;

        if (context && (len = keys.length)) {
            for (i = 0; i < len; i += 1) {
                key   = keys[i];
                param = key.name || key;
                regex = new RegExp('[:*]' + param + '\\b');
                path  = path.replace(regex, context[param]);
            }
        }

        // Replace missing params with empty strings.
        return path.replace(/([:*])([\w\-]+)?/g, '');
    };
};

@@ -912,16 +912,168 @@
 trings.%0A
+        // pathTo is not correctly handling express optional parameters of the form /blogs/:id/:tag?%0A        // lop off the ? off the end of the result%0A
         
@@ -1112,16 +1112,36 @@
 -%5D+)?/g,
+ '').replace(/%5B?%5D$/,
  '');%0A  

Fix issue with pathTo and optional express paths like /blog/:id/:tag?