<!--
title: Validate pattern matching
domain: ai-agents
topic: Algorithms
language: TypeScript
source: vercel/ai
updated: 2025-04-29
url: https://awesomereviewers.com/reviewers/ai-validate-pattern-matching/
-->

When implementing algorithms that involve pattern matching or data parsing, ensure robustness across all edge cases and clearly document the algorithm's behavior. This is particularly important for:

1. Prefix matching and partial results - When matching strings against a set of possible values (like enums), consider how to handle ambiguous partial matches:

```typescript
// Instead of returning any partial match:
const possibleEnumValues = enumValues.filter(enumValue =>
  enumValue.startsWith(result)
);

// Consider uniqueness in your algorithm:
if (possibleEnumValues.length > 1) {
  // Handle ambiguous case - either return only the partial result
  // or require a complete match
  return { partial: result };
} else if (possibleEnumValues.length === 1) {
  // Return the full value when there's only one possible match
  return { complete: possibleEnumValues[0] };
}
```

2. Binary format detection - When parsing binary data formats, account for metadata headers and format variations:

```typescript
// Check for format-specific headers before pattern matching
const stripMetadata = (data: Uint8Array) => {
  // Detect specific header patterns (like ID3 for MP3)
  if (data[0] === 0x49 && data[1] === 0x44 && data[2] === 0x33) {
    // Calculate header size and return data without header
    const headerSize = calculateHeaderSize(data);
    return data.slice(headerSize);
  }
  return data; // No metadata found
};

// Then perform format detection on clean data
const detectedFormat = detectFormat(stripMetadata(rawData));
```

3. Iteration correctness - When processing collections as part of your algorithm, verify that you're iterating over the correct data structure:

```typescript
// Incorrect: iterating over empty/wrong object
for (const key in emptyObject) { /* ... */ }

// Correct: iterate over the object containing actual data
for (const key in dataObject) {
  if (dataObject[key] !== undefined) {
    processedData[key] = dataObject[key];
  }
}
```

Always test pattern matching algorithms with comprehensive test cases covering edge cases like empty inputs, partial matches, and format variations.
