@architect/inventory 3.5.3 → 3.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@architect/inventory",
3
- "version": "3.5.3",
3
+ "version": "3.5.4",
4
4
  "description": "Architect project resource enumeration utility",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -11,18 +11,15 @@ let { httpMethods } = require('../../../lib')
11
11
  * - Finally, ensure root captures rank below a root literal
12
12
  */
13
13
  module.exports = function sortHTTP (http) {
14
+ let param = /\/:/
15
+
14
16
  // Construct the tree from HTTP methods
15
17
  let tree = {}
16
18
  http.forEach(({ method, path }) => {
17
19
  if (!tree[method]) tree[method] = []
18
20
  let parts = path.split('/').filter(Boolean)
19
21
  let depth = parts.length
20
- let item = { depth, path }
21
- let param = /\/:/
22
- if (path.match(param)) {
23
- item.hasParam = true
24
- item.paramIndex = path.match(param).index // If multiple, we want the earliest
25
- }
22
+ let item = { depth, path, parts, hasParam: param.test(path) }
26
23
  if (parts.length) {
27
24
  if (parts[depth - 1] === '*') item.trailingCapture = 'catchall'
28
25
  if (parts[depth - 1].startsWith(':')) item.trailingCapture = 'param'
@@ -40,18 +37,28 @@ module.exports = function sortHTTP (http) {
40
37
  .sort((a, b) => b.depth - a.depth)
41
38
  // Sort within a given depth
42
39
  .sort((a, b) => {
43
- // Handle root (depth: 0)
40
+ // Depth is already correct
44
41
  if (a.depth - b.depth < 0) return
45
- if (a.hasParam && b.hasParam) {
46
- // Sort at the earliest param
47
- if (a.paramIndex < b.paramIndex) return 1
48
- if (a.paramIndex > b.paramIndex) return -1
49
- // Then sort alphabetically
50
- if (a.path < b.path) return -1
51
- if (a.path > b.path) return 1
42
+ // jic we aren't in the same depth (this prob won't be run)
43
+ if (a.depth !== b.depth) return b.depth - a.depth
44
+ // Sort static parts vs params by position in path
45
+ if (a.hasParam || b.hasParam) {
46
+ for (let i = 0; i < a.depth; i++) {
47
+ let aPart = a.parts[i]
48
+ let bPart = b.parts[i]
49
+ let aPartIsParam = aPart.startsWith(':')
50
+ let bPartIsParam = bPart.startsWith(':')
51
+ // If position is both params, move onto the next position
52
+ if (aPartIsParam && bPartIsParam) continue
53
+ // Static parts get priority over params
54
+ if (aPartIsParam) return 1
55
+ if (bPartIsParam) return -1
56
+ // Sort alphabetically
57
+ if (aPart < bPart) return -1
58
+ if (aPart > bPart) return 1
59
+ }
52
60
  }
53
- if (a.hasParam) return 1
54
- if (b.hasParam) return -1
61
+ // Final alphabetical sort
55
62
  if (a.path < b.path) return -1
56
63
  if (a.path > b.path) return 1
57
64
  })