dev-utils-plus
Version:
Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math
142 lines • 3.37 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;
/**
* Removes duplicate elements from an array
*/
function unique(arr) {
return [...new Set(arr)];
}
/**
* Shuffles an array in place using Fisher-Yates algorithm
*/
function shuffle(arr) {
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = shuffled[i];
const jValue = shuffled[j];
if (temp !== undefined && jValue !== undefined) {
shuffled[i] = jValue;
shuffled[j] = temp;
}
}
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
*/
function chunk(arr, size) {
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
*/
function flatten(arr) {
return arr.reduce((flat, item) => {
return flat.concat(Array.isArray(item) ? flatten(item) : item);
}, []);
}
/**
* 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
*/
function countBy(arr) {
return arr.reduce((counts, item) => {
const key = String(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
*/
function last(arr) {
return arr[arr.length - 1];
}
/**
* Gets the first element of an array
*/
function first(arr) {
return arr[0];
}
//# sourceMappingURL=index.js.map