示例工程 27-framework-comparison

27-framework-comparison/app/src/lib/decision.ts

typescript · 1.7 KB

export type FrameworkId = "react" | "vue" | "svelte";

export type Weights = {
  bundle: number;
  ecosystem: number;
  team: number;
  migration: number;
};

export type FrameworkScore = {
  id: FrameworkId;
  label: string;
  scores: Weights;
};

export type RankedFramework = {
  id: FrameworkId;
  label: string;
  total: number;
};

export const DEFAULT_WEIGHTS: Weights = {
  bundle: 3,
  ecosystem: 4,
  team: 3,
  migration: 3,
};

export const PRESET_WEIGHTS: Record<"balanced" | "team" | "bundle", Weights> = {
  balanced: DEFAULT_WEIGHTS,
  team: { bundle: 1, ecosystem: 1, team: 5, migration: 1 },
  bundle: { bundle: 5, ecosystem: 1, team: 1, migration: 1 },
};

export const FRAMEWORKS: FrameworkScore[] = [
  {
    id: "react",
    label: "React 19",
    scores: { bundle: 2, ecosystem: 5, team: 5, migration: 3 },
  },
  {
    id: "vue",
    label: "Vue 3",
    scores: { bundle: 4, ecosystem: 5, team: 4, migration: 4 },
  },
  {
    id: "svelte",
    label: "Svelte 5",
    scores: { bundle: 5, ecosystem: 4, team: 3, migration: 2 },
  },
];

export function score(framework: FrameworkScore, weights: Weights): number {
  return (
    framework.scores.bundle * weights.bundle +
    framework.scores.ecosystem * weights.ecosystem +
    framework.scores.team * weights.team +
    framework.scores.migration * weights.migration
  );
}

export function rankFrameworks(weights: Weights): RankedFramework[] {
  return FRAMEWORKS.map((framework) => ({
    id: framework.id,
    label: framework.label,
    total: score(framework, weights),
  })).sort((a, b) => b.total - a.total);
}

export function winner(weights: Weights): RankedFramework {
  return (
    rankFrameworks(weights)[0] ?? {
      id: "react",
      label: "React 19",
      total: 0,
    }
  );
}