@contentstech/stackflow-plugin-history-sync
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -238,5 +238,22 @@ "use strict"; | ||
// src/common/sortActivityRoutes.ts | ||
var paramRe = /^:[\w-]+$/; | ||
var dynamicSegmentValue = 3; | ||
var emptySegmentValue = 1; | ||
var staticSegmentValue = 10; | ||
var splatPenalty = -2; | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments.filter((s) => !isSplat(s)).reduce( | ||
(score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), | ||
initialScore | ||
); | ||
} | ||
function sortActivityRoutes(routes) { | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount | ||
(a, b) => computeScore(b.path) - computeScore(a.path) | ||
); | ||
@@ -243,0 +260,0 @@ } |
@@ -238,5 +238,22 @@ "use strict"; | ||
// src/common/sortActivityRoutes.ts | ||
var paramRe = /^:[\w-]+$/; | ||
var dynamicSegmentValue = 3; | ||
var emptySegmentValue = 1; | ||
var staticSegmentValue = 10; | ||
var splatPenalty = -2; | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments.filter((s) => !isSplat(s)).reduce( | ||
(score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), | ||
initialScore | ||
); | ||
} | ||
function sortActivityRoutes(routes) { | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount | ||
(a, b) => computeScore(b.path) - computeScore(a.path) | ||
); | ||
@@ -243,0 +260,0 @@ } |
@@ -238,5 +238,22 @@ "use strict"; | ||
// src/common/sortActivityRoutes.ts | ||
var paramRe = /^:[\w-]+$/; | ||
var dynamicSegmentValue = 3; | ||
var emptySegmentValue = 1; | ||
var staticSegmentValue = 10; | ||
var splatPenalty = -2; | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments.filter((s) => !isSplat(s)).reduce( | ||
(score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), | ||
initialScore | ||
); | ||
} | ||
function sortActivityRoutes(routes) { | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount | ||
(a, b) => computeScore(b.path) - computeScore(a.path) | ||
); | ||
@@ -243,0 +260,0 @@ } |
@@ -231,5 +231,22 @@ "use strict"; | ||
// src/common/sortActivityRoutes.ts | ||
var paramRe = /^:[\w-]+$/; | ||
var dynamicSegmentValue = 3; | ||
var emptySegmentValue = 1; | ||
var staticSegmentValue = 10; | ||
var splatPenalty = -2; | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments.filter((s) => !isSplat(s)).reduce( | ||
(score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), | ||
initialScore | ||
); | ||
} | ||
function sortActivityRoutes(routes) { | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount | ||
(a, b) => computeScore(b.path) - computeScore(a.path) | ||
); | ||
@@ -236,0 +253,0 @@ } |
{ | ||
"name": "@contentstech/stackflow-plugin-history-sync", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -38,7 +38,7 @@ import { sortActivityRoutes } from "./sortActivityRoutes"; | ||
expect(routes).toStrictEqual([ | ||
{ activityName: "C", path: "/:hello/:world/:foo/:bar" }, | ||
{ activityName: "A", path: "/detailed/*" }, | ||
{ activityName: "B", path: "/:hello/:world" }, | ||
{ activityName: "C", path: "/:hello/:world/:foo/:bar" }, | ||
{ activityName: "A", path: "*" }, | ||
]); | ||
}); |
import type { ActivityRoute } from "./ActivityRoute"; | ||
import { makeTemplate } from "./makeTemplate"; | ||
const paramRe = /^:[\w-]+$/; | ||
const dynamicSegmentValue = 3; | ||
const emptySegmentValue = 1; | ||
const staticSegmentValue = 10; | ||
const splatPenalty = -2; | ||
const isSplat = (s: string) => s === "*"; | ||
/** | ||
* https://github.com/remix-run/react-router/blob/0f2b167e9b862d5e6b3425438787c8e7b39197f4/packages/router/utils.ts#L742-L773 | ||
*/ | ||
function computeScore(path: string): number { | ||
const segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments | ||
.filter((s) => !isSplat(s)) | ||
.reduce( | ||
(score, segment) => | ||
score + | ||
(paramRe.test(segment) | ||
? dynamicSegmentValue | ||
: segment === "" | ||
? emptySegmentValue | ||
: staticSegmentValue), | ||
initialScore, | ||
); | ||
} | ||
export function sortActivityRoutes<T>( | ||
@@ -8,4 +38,4 @@ routes: ActivityRoute<T>[], | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount, | ||
(a, b) => computeScore(b.path) - computeScore(a.path), | ||
); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
698858
137
8178