dev-utils-plus
Version:
Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math
274 lines • 7.46 kB
JavaScript
;
/**
* Array utility functions for common operations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.unique = unique;
exports.shuffle = shuffle;
exports.groupBy = groupBy;
exports.chunk = chunk;
exports.flatten = flatten;
exports.intersection = intersection;
exports.difference = difference;
exports.sortBy = sortBy;
exports.range = range;
exports.countBy = countBy;
exports.compact = compact;
exports.last = last;
exports.first = first;
exports.take = take;
exports.drop = drop;
exports.max = max;
exports.min = min;
/**
* Removes duplicate elements from an array
* @param arr - The array to remove duplicates from
* @returns A new array with unique elements
* @example
* unique([1, 2, 2, 3, 3, 4]) // [1, 2, 3, 4]
*/
function unique(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
return [...new Set(arr)];
}
/**
* Shuffles an array using Fisher-Yates algorithm
* @param arr - The array to shuffle
* @returns A new shuffled array
* @example
* shuffle([1, 2, 3, 4]) // [3, 1, 4, 2] (random order)
*/
function shuffle(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
/**
* Groups array elements by a key function
*/
function groupBy(arr, keyFn) {
return arr.reduce((groups, item) => {
const key = keyFn(item);
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(item);
return groups;
}, {});
}
/**
* Chunks an array into smaller arrays of specified size
* @param arr - The array to chunk
* @param size - The size of each chunk (must be positive integer)
* @returns Array of chunked arrays
* @example
* chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
*/
function chunk(arr, size) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
if (!Number.isInteger(size) || size <= 0) {
throw new Error('Size must be a positive integer');
}
const chunks = [];
for (let i = 0; i < arr.length; i += size) {
chunks.push(arr.slice(i, i + size));
}
return chunks;
}
/**
* Flattens a nested array to a single level
* @param arr - The array to flatten
* @param depth - Maximum depth to flatten (default: Infinity)
* @returns A new flattened array
* @example
* flatten([1, [2, 3], [4, [5, 6]]]) // [1, 2, 3, 4, 5, 6]
* flatten([1, [2, [3, [4]]]], 2) // [1, 2, 3, [4]]
*/
function flatten(arr, depth = Infinity) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
function flattenRecursive(array, currentDepth) {
const result = [];
for (const item of array) {
if (Array.isArray(item) && currentDepth > 0) {
result.push(...flattenRecursive(item, currentDepth - 1));
}
else {
result.push(item);
}
}
return result;
}
return flattenRecursive(arr, depth);
}
/**
* Finds the intersection of multiple arrays
*/
function intersection(...arrays) {
if (arrays.length === 0)
return [];
if (arrays.length === 1)
return arrays[0] || [];
return arrays.reduce((intersect, arr) => {
return intersect.filter(item => arr.includes(item));
});
}
/**
* Finds the difference between two arrays
*/
function difference(arr1, arr2) {
return arr1.filter(item => !arr2.includes(item));
}
/**
* Sorts an array of objects by a specific key
*/
function sortBy(arr, key, direction = 'asc') {
return [...arr].sort((a, b) => {
const aVal = a[key];
const bVal = b[key];
if (aVal < bVal)
return direction === 'asc' ? -1 : 1;
if (aVal > bVal)
return direction === 'asc' ? 1 : -1;
return 0;
});
}
/**
* Creates a range of numbers
*/
function range(start, end, step = 1) {
const result = [];
for (let i = start; i <= end; i += step) {
result.push(i);
}
return result;
}
/**
* Counts occurrences of elements in an array
* @param arr - The array to count elements from
* @param keyFn - Optional function to generate custom keys
* @returns Object with element counts
* @example
* countBy([1, 2, 2, 3, 3, 3]) // {'1': 1, '2': 2, '3': 3}
* countBy(['apple', 'banana', 'apple'], x => x.length) // {'5': 2, '6': 1}
*/
function countBy(arr, keyFn = (item) => String(item)) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
return arr.reduce((counts, item) => {
const key = keyFn(item);
counts[key] = (counts[key] || 0) + 1;
return counts;
}, {});
}
/**
* Removes falsy values from an array
*/
function compact(arr) {
return arr.filter(Boolean);
}
/**
* Gets the last element of an array
* @param arr - The array to get the last element from
* @returns The last element or undefined if array is empty
* @example
* last([1, 2, 3]) // 3
* last([]) // undefined
*/
function last(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
return arr[arr.length - 1];
}
/**
* Gets the first element of an array
* @param arr - The array to get the first element from
* @returns The first element or undefined if array is empty
* @example
* first([1, 2, 3]) // 1
* first([]) // undefined
*/
function first(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
return arr[0];
}
/**
* Takes the first n elements from an array
* @param arr - The array to take elements from
* @param n - Number of elements to take
* @returns Array with first n elements
* @example
* take([1, 2, 3, 4, 5], 3) // [1, 2, 3]
*/
function take(arr, n) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
if (!Number.isInteger(n) || n < 0) {
throw new Error('n must be a non-negative integer');
}
return arr.slice(0, n);
}
/**
* Drops the first n elements from an array
* @param arr - The array to drop elements from
* @param n - Number of elements to drop
* @returns Array with first n elements removed
* @example
* drop([1, 2, 3, 4, 5], 2) // [3, 4, 5]
*/
function drop(arr, n) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
if (!Number.isInteger(n) || n < 0) {
throw new Error('n must be a non-negative integer');
}
return arr.slice(n);
}
/**
* Finds the maximum element in an array
* @param arr - Array of numbers
* @returns Maximum value or undefined if empty
* @example
* max([1, 5, 3, 9, 2]) // 9
*/
function max(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
if (arr.length === 0)
return undefined;
return Math.max(...arr);
}
/**
* Finds the minimum element in an array
* @param arr - Array of numbers
* @returns Minimum value or undefined if empty
* @example
* min([1, 5, 3, 9, 2]) // 1
*/
function min(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
}
if (arr.length === 0)
return undefined;
return Math.min(...arr);
}
//# sourceMappingURL=index.js.map