How to use the methods.reduce function in methods

To help you get started, we’ve selected a few methods examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github YahooArchive / express-map / index.js View on Github external
function getRouteMap(annotations) {
    /* jshint validthis:true */
    if (!Array.isArray(annotations)) {
        annotations = [].slice.call(arguments);
    }

    // Gather all mapped/named routePaths that have the specified `annotations`.
    var appAnnotations = this.annotations,
        routes         = this.findAll(annotations.concat('name'));

    // Creates a mapping of name -> route object. The route objects are shallow
    // copies of a route's primary metadata.
    return methods.reduce(function (map, method) {
        var methodRoutes = routes[method];

        if (!methodRoutes) { return map; }

        methodRoutes.forEach(function (route) {
            var pathAnnotations = appAnnotations[route.path],
                name            = pathAnnotations.name,
                entry;

            // Return early if the route has no canonical `name`, or if that
            // name has already been mapped, in which case none of its `names`
            // will be mapped.
            if (!name || map[name]) { return; }

            // The entry that represents the route in the route map.
            entry = {