@stackflow/plugin-history-sync
Advanced tools
Comparing version 1.6.0-canary.0 to 1.6.0
@@ -248,5 +248,22 @@ "use strict"; | ||
// src/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) | ||
); | ||
@@ -253,0 +270,0 @@ } |
{ | ||
"name": "@stackflow/plugin-history-sync", | ||
"version": "1.6.0-canary.0", | ||
"version": "1.6.0", | ||
"repository": { | ||
@@ -48,6 +48,6 @@ "type": "git", | ||
"devDependencies": { | ||
"@stackflow/config": "^1.0.1-canary.0", | ||
"@stackflow/config": "^1.1.0", | ||
"@stackflow/core": "^1.0.13", | ||
"@stackflow/esbuild-config": "^1.0.3", | ||
"@stackflow/react": "^1.2.0-canary.0", | ||
"@stackflow/react": "^1.2.0", | ||
"@swc/core": "^1.6.6", | ||
@@ -54,0 +54,0 @@ "@swc/jest": "^0.2.36", |
@@ -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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
195302
3483
1