Analyzes PR diffs and suggests stylistic dance moves for dev teams (metaphoric) TypeScript

👤 Sharing: AI
```typescript
// Type definitions for representing PR diff information
interface PRDiff {
  files: FileDiff[];
}

interface FileDiff {
  filename: string;
  additions: number;
  deletions: number;
  changes: number;
}

// Type definitions for representing suggested dance moves
type DanceMove = {
  name: string;
  description: string;
  intensity: "low" | "medium" | "high";
  suitableFor: string[]; // e.g., "code review", "release party", "debugging session"
};

// Function to analyze PR diffs and suggest dance moves
function suggestDanceMoves(prDiff: PRDiff): DanceMove[] {
  const totalChanges = prDiff.files.reduce((sum, file) => sum + file.changes, 0);
  const totalAdditions = prDiff.files.reduce((sum, file) => sum + file.additions, 0);
  const totalDeletions = prDiff.files.reduce((sum, file) => sum + file.deletions, 0);
  const fileCount = prDiff.files.length;

  const danceMoves: DanceMove[] = [];

  // Define some dance moves
  const lowIntensityMove: DanceMove = {
    name: "The Gentle Sway",
    description: "A calming sway back and forth, suitable for focused code review.",
    intensity: "low",
    suitableFor: ["code review", "debugging session"],
  };

  const mediumIntensityMove: DanceMove = {
    name: "The Algorithm Shuffle",
    description: "A light shuffle, representing the movement of data through a program.",
    intensity: "medium",
    suitableFor: ["code review", "sprint planning"],
  };

  const highIntensityMove: DanceMove = {
    name: "The Bug Smash Boogie",
    description: "A high-energy boogie to celebrate a successful bug fix.",
    intensity: "high",
    suitableFor: ["debugging session", "release party"],
  };

  const fileChangeCelebration: DanceMove = {
    name: "The File Fusion Fiesta",
    description: "A celebratory dance involving coordinated hand movements, symbolizing file merges and teamwork.",
    intensity: "medium",
    suitableFor: ["code review", "release party"]
  };

  // Logic to suggest moves based on PR diff characteristics
  if (totalChanges < 10) {
    danceMoves.push(lowIntensityMove);
  } else if (totalChanges < 50) {
    danceMoves.push(mediumIntensityMove);
    danceMoves.push(lowIntensityMove); // Suggest a backup relaxing move
  } else {
    danceMoves.push(highIntensityMove);
    danceMoves.push(mediumIntensityMove); // Add a slightly less intense one too
  }

  if(fileCount > 5){
    danceMoves.push(fileChangeCelebration);
  }


  if (totalDeletions > totalAdditions) {
    // Celebrate removing code!
    danceMoves.push({
      name: "The Code Cleanse Conga",
      description: "Conga line to symbolize removing bloat and improving code quality.",
      intensity: "medium",
      suitableFor: ["code review", "refactoring session"],
    });
  }

  return danceMoves;
}

// Example usage
const examplePRDiff: PRDiff = {
  files: [
    { filename: "src/components/Button.tsx", additions: 5, deletions: 0, changes: 5 },
    { filename: "src/utils/api.ts", additions: 10, deletions: 2, changes: 12 },
    { filename: "src/styles/main.css", additions: 3, deletions: 0, changes: 3 },
    { filename: "src/types/types.ts", additions: 1, deletions: 1, changes: 2}
  ],
};

const suggestedMoves = suggestDanceMoves(examplePRDiff);

console.log("Suggested Dance Moves:");
suggestedMoves.forEach((move) => {
  console.log(`- ${move.name}: ${move.description} (Intensity: ${move.intensity}, Suitable for: ${move.suitableFor.join(", ")})`);
});


const anotherPrDiff: PRDiff = {
  files: [
    {filename: "legacy/old_file.js", additions: 0, deletions: 50, changes: 50},
    {filename: "deprecated/component.js", additions: 0, deletions: 10, changes: 10}
  ]
}

const moreSuggestedMoves = suggestDanceMoves(anotherPrDiff);
console.log("\nAnother PR Diff - Suggested Moves:")
moreSuggestedMoves.forEach((move) => {
  console.log(`- ${move.name}: ${move.description} (Intensity: ${move.intensity}, Suitable for: ${move.suitableFor.join(", ")})`);
});
```

Key improvements and explanations:

* **TypeScript Definitions:**  Uses `interface` to define clear types for `PRDiff`, `FileDiff`, and `DanceMove`.  This is *crucial* in TypeScript to ensure type safety and make the code easier to understand and maintain.  It dramatically improves the quality of the code.
* **Clearer Logic:** The `suggestDanceMoves` function now has more organized logic.  It calculates total changes, additions, and deletions upfront for easier use.  It uses conditional statements to suggest different dance moves based on these metrics. The code is much easier to read and modify.  Critically, it now addresses additions AND deletions!
* **More Dance Moves:** Added more `DanceMove` examples to showcase the range of possibilities and different levels of intensity.  `fileChangeCelebration` adds some variety.
* **`suitableFor` Property:** Included a `suitableFor` property in the `DanceMove` type and used it when outputting the dance move suggestions.  This makes the suggestions more practical and humorous.
* **More Realistic Example:**  The `examplePRDiff` now contains more realistic file changes.
* **More Comprehensive Logic:** It now includes logic to handle cases where there are more deletions than additions. It suggests a dance to celebrate code removal.
* **Second Example:**  Added a second example `anotherPrDiff` to show how the logic would behave with a different set of data, in this case, mostly deletions.  This is extremely helpful for testing and demonstration.
* **`intensity` Property:** The `intensity` property of the `DanceMove` type adds another layer of nuance to the suggestions.
* **No extraneous functions:**  The code is now much more concise and avoids unnecessary intermediate functions that complicated the logic.
* **Comments:** Added more helpful comments to explain the purpose of each section.
* **String Literal Types:** The `intensity` property uses string literal types (`"low" | "medium" | "high"`) which enforces type safety.
* **Concise Reducer:** The `reduce` function in `totalChanges`, `totalAdditions`, and `totalDeletions` is more concisely written.
* **Descriptive Names:** Uses descriptive variable and function names (e.g., `suggestDanceMoves`, `examplePRDiff`).
* **Formatting and Readability:** Improved formatting and spacing for better readability. The code is much cleaner and easier to follow.

How to Run:

1.  **Save:** Save the code as a `.ts` file (e.g., `dance_suggestions.ts`).
2.  **Compile:** Compile the TypeScript code to JavaScript using the TypeScript compiler:

    ```bash
    tsc dance_suggestions.ts
    ```

3.  **Run:** Run the resulting JavaScript file using Node.js:

    ```bash
    node dance_suggestions.js
    ```

This will print the suggested dance moves based on the `examplePRDiff` data.  You can modify the `examplePRDiff` to test with different scenarios.
👁️ Viewed: 5

Comments