示例工程 27-framework-comparison

27-framework-comparison/demo-01-decision/decision.ts

typescript · 1.6 KB

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

type Framework = {
  id: string;
  label: string;
  scores: Weights;
};

const frameworks: Framework[] = [
  {
    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 },
  },
];

const presets: Record<string, Weights> = {
  balanced: { bundle: 3, ecosystem: 4, team: 3, migration: 3 },
  team: { bundle: 1, ecosystem: 1, team: 5, migration: 1 },
  bundle: { bundle: 5, ecosystem: 1, team: 1, migration: 1 },
};

function rank(weights: Weights): Array<{ id: string; total: number }> {
  return frameworks
    .map((framework) => ({
      id: framework.id,
      total:
        framework.scores.bundle * weights.bundle +
        framework.scores.ecosystem * weights.ecosystem +
        framework.scores.team * weights.team +
        framework.scores.migration * weights.migration,
    }))
    .sort((a, b) => b.total - a.total);
}

const result = Object.fromEntries(
  Object.entries(presets).map(([name, weights]) => [name, rank(weights)]),
);

const winners = Object.fromEntries(
  Object.entries(result).map(([name, ranking]) => [name, ranking[0]?.id]),
);

console.log(
  JSON.stringify({
    evt: "smoke",
    ok:
      winners.balanced === "vue" &&
      winners.team === "react" &&
      winners.bundle === "svelte" &&
      result.balanced?.[0]?.total === 56,
    winners,
    result,
  }),
);