Skip to content

Issue: Add Yarn Calculator Feature#34

Description

@Sh-TB

馃悰 Issue: Add Yarn Calculator Feature

Description

I'd like to propose adding a Yarn Calculator feature to help users estimate how much yarn they need for their projects. This is a common need for crocheters/knitters when planning new projects.

Current Issue

Looking at the codebase, I noticed there's no built-in way to calculate yarn requirements based on pattern specifications. Users currently have to manually estimate or use external tools.

Proposed Solution

Add a new API endpoint /api/calculator/yarn that calculates estimated yardage and number of skeins needed based on:

  • Pattern area (square inches/cm)
  • Gauge (stitches per inch/cm)
  • Yarn weight category
## Code Implementation

Here's the implementation I've prepared:

```javascript
// Helper function to calculate yardage based on pattern specifications
function calculateYardage(pattern_area, gauge, yarn_weight) {
  // Base yardage per square inch for different yarn weights
  const yardagePerSquareInch = {
    'Lace': 8.5,
    'Super Fine': 7.5,
    'Fine': 6.5,
    'Light': 5.5,
    'Medium': 4.5,
    'Bulky': 3.5,
    'Super Bulky': 2.5,
    'Jumbo': 1.5
  };

  // Calculate total stitches needed
  const totalStitches = pattern_area * (gauge * gauge);
  
  // Get base yardage multiplier for yarn weight
  const multiplier = yardagePerSquareInch[yarn_weight] || 4.5;
  
  // Estimate total yardage
  return Math.round(totalStitches * multiplier);
}

// Yarn Calculator endpoint
app.post('/api/calculator/yarn', authMiddleware, async (req, res) => {
  try {
    const { pattern_area, gauge, yarn_weight } = req.body;
    
    // Validation
    if (!pattern_area || !gauge || !yarn_weight) {
      return res.status(400).json({ 
        error: 'Missing required fields: pattern_area, gauge, yarn_weight' 
      });
    }
    
    if (pattern_area <= 0 || gauge <= 0) {
      return res.status(400).json({ 
        error: 'Pattern area and gauge must be positive numbers' 
      });
    }
    
    // Calculate yardage
    const estimatedYardage = calculateYardage(pattern_area, gauge, yarn_weight);
    
    // Calculate skeins needed (assuming 200 yards per skein as default)
    // This could be made configurable based on actual yarn data
    const yardsPerSkein = 200;
    const skeins_needed = Math.ceil(estimatedYardage / yardsPerSkein);
    
    res.json({ 
      estimatedYardage,
      skeins_needed,
      pattern_area,
      gauge,
      yarn_weight
    });
  } catch (error) {
    console.error('Error calculating yarn requirements:', error);
    res.status(500).json({ error: 'Failed to calculate yarn requirements' });
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions