Skip to content
Permalink
Browse files

fix: add routesUnion() method to combine routes arrays

- Uses `lodash.unionBy()` instead of `lodash.union()`
- Prefers routes set in `options.routes` to static routes
  • Loading branch information...
delucis authored and NicoPennec committed Feb 21, 2018
1 parent 83cdc28 commit 5d4f5b71ca6fca2a5fa15083768d6bb14f0ce4cf
Showing with 16 additions and 2 deletions.
  1. +16 −2 src/index.js
@@ -1,7 +1,7 @@
const { Minimatch } = require('minimatch')
const sm = require('sitemap')
const isHTTPS = require('is-https')
const { union, uniq } = require('lodash')
const { unionBy, uniq } = require('lodash')
const path = require('path')
const fs = require('fs-extra')
const AsyncCache = require('async-cache')
@@ -101,7 +101,7 @@ function createCache (staticRoutes, options) {
maxAge: options.cacheTime,
load (_, callback) {
promisifyRoute(options.routes)
.then(routes => union(staticRoutes, routes))
.then(routes => routesUnion(staticRoutes, routes))
.then(routes => {
callback(null, routes)
})
@@ -159,3 +159,17 @@ function promisifyRoute (fn) {
}
return promise
}

// Join static and options-defined routes into single array
function routesUnion (staticRoutes, optionsRoutes) {
// Make sure any routes passed as strings are converted to objects with url properties
staticRoutes = staticRoutes.map(ensureRouteIsObject)
optionsRoutes = optionsRoutes.map(ensureRouteIsObject)
// add static routes to options routes, discarding any defined in options
return unionBy(optionsRoutes, staticRoutes, 'url')
}

// Make sure a passed route is an object
function ensureRouteIsObject (route) {
return typeof route === 'object' ? route : { url: route }
}

0 comments on commit 5d4f5b7

Please sign in to comment.
You can’t perform that action at this time.