array-diff-helper
v1.0.0
Published
A TypeScript utility for comparing arrays and showing their differences
Maintainers
Readme
array-diff-helper 🎯
A TypeScript utility for comparing arrays and showing their differences. This library helps you identify what elements were added, removed, or remained unchanged between two arrays.
✨ Features
- 🎯 TypeScript support with full type definitions
- ⚡ Efficient comparison using Set data structure
- 🔄 Handles arrays of any type
- 🛡️ Comprehensive error handling
- ✅ 100% test coverage
📦 Installation
npm install array-diff-helper🚀 Usage
import { arrayDiff } from "array-diff-helper";
// Basic usage
const oldArray = ["apple", "banana", "cherry"];
const newArray = ["banana", "cherry", "date"];
const result = arrayDiff(oldArray, newArray);
console.log(result);
/*
Output:
{
added: ['date'],
removed: ['apple'],
unchanged: ['banana', 'cherry']
}
*/
// With different types
const numbers = [1, 2, 3];
const mixed = [1, 2, "four"];
const numberResult = arrayDiff(numbers, mixed);
console.log(numberResult);
/*
Output:
{
added: ['four'],
removed: [3],
unchanged: [1, 2]
}
*/📚 API
arrayDiff<T>(oldArray: T[], newArray: T[]): ArrayDiffResult<T>
Compares two arrays and returns an object containing:
- ➕
added: Elements that are in the new array but not in the old array - ➖
removed: Elements that are in the old array but not in the new array - 🔄
unchanged: Elements that are present in both arrays
Parameters
oldArray: The original array to compare fromnewArray: The new array to compare to
Returns
An object of type ArrayDiffResult<T> containing three arrays:
interface ArrayDiffResult<T> {
added: T[];
removed: T[];
unchanged: T[];
}Throws
- ❌ Error if either input is not an array
🛠️ Development
# Install dependencies
npm install
# Run tests
npm test
# Build the project
npm run build
# Run linter
npm run lint📄 License
MIT
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
