0.4.0 • Published 7 months ago
arrays-tool v0.4.0
Arrays Tool
A TypeScript/JavaScript utility library for modern and efficient array operations.
Installation
npm install arrays-tool
Usage
import { ArrayUtils } from 'arrays-tool';
// Example array
const numbers = [1, 2, 3, 4, 5];
// Shuffle array
const shuffled = ArrayUtils.shuffle(numbers);
console.log(shuffled); // [3, 1, 5, 2, 4]
// Reverse array
const reversed = ArrayUtils.reverse(numbers);
console.log(reversed); // [5, 4, 3, 2, 1]
// Get random element
const random = ArrayUtils.random(numbers);
console.log(random); // 3
// Remove duplicates
const withDuplicates = [1, 2, 2, 3, 3, 4];
const unique = ArrayUtils.unique(withDuplicates);
console.log(unique); // [1, 2, 3, 4]
// Merge arrays
const array1 = [1, 2, 3];
const array2 = [3, 4, 5];
const merged = ArrayUtils.merge(array1, array2);
console.log(merged); // [1, 2, 3, 4, 5]
Math Operations
import { ArrayUtils } from 'arrays-tool';
const numbers = [1, 2, 3, 4, 5];
// Calculate sum
const sum = ArrayUtils.sum(numbers);
console.log(sum); // 15
// Calculate average
const avg = ArrayUtils.average(numbers);
console.log(avg); // 3
// Find maximum
const max = ArrayUtils.max(numbers);
console.log(max); // 5
// Find minimum
const min = ArrayUtils.min(numbers);
console.log(min); // 1
// Calculate product
const product = ArrayUtils.product(numbers);
console.log(product); // 120
Features
- 📦 Zero dependencies
- 💪 TypeScript support
- 🚀 High performance
- 🎯 Tree-shaking support
- 📝 Comprehensive documentation
API
shuffle<T>(array: T[]): T[]
Shuffles the array and returns a new array.
reverse<T>(array: T[]): T[]
Reverses the array and returns a new array.
random<T>(array: T[]): T
Selects and returns a random element from the array.
unique<T>(array: T[]): T[]
Removes duplicate elements and returns a new array.
merge<T>(array1: T[], array2: T[]): T[]
Merges two arrays and removes duplicate elements.
Math Operations
sum(array: number[]): number
Calculates the sum of all numbers in the array.
average(array: number[]): number
Calculates the average (mean) of all numbers in the array.
max(array: number[]): number
Returns the maximum number in the array.
min(array: number[]): number
Returns the minimum number in the array.
product(array: number[]): number
Calculates the product of all numbers in the array.
License
MIT © Adem Hatay