igir
Version:
🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.
71 lines (70 loc) • 2.13 kB
JavaScript
/**
* A collection of static array utility functions.
*/
export default {
/**
* Filter elements in an array to only unique values, using the result of a mapper function to
* test for equality. Usage:
*
* <code>
* ['a', 'b', 'c', 'a', 'A', 'C'].filter(ArrayPoly.filterUniqueMapped((str) => str.toUpperCase());
* </code>
*/
filterUniqueMapped(mapper) {
const seenMappedValues = new Set();
return (value, _idx, values) => {
if (values.length <= 1) {
// Arrays that are empty or only have one element are already unique
return true;
}
const mapped = mapper(value);
if (!seenMappedValues.has(mapped)) {
seenMappedValues.add(mapped);
return true;
}
return false;
};
},
/**
* Reduce elements in an array to chunks of size {@link limit}.
*
* <code>
* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(ArrayPoly.reduceChunk(3), []);
* </code>
*/
reduceChunk(limit) {
return (previous, _current, idx, array) => {
if (idx === 0) {
if (limit <= 0) {
return [array];
}
const chunks = [];
for (let i = 0; i < array.length; i += limit) {
const chunk = array.slice(i, i + limit);
chunks.push(chunk);
}
return chunks;
}
return previous;
};
},
/**
* Reduce elements in an array to only unique values. Usage:
*
* <code>
* [1, 2, 3, 1, 1, 3].reduce(ArrayPoly.reduceUnique(), []);
* </code>
*/
reduceUnique() {
return (previous, _current, idx, array) => {
if (array.length <= 1) {
// Arrays that are empty or only have one element are already unique
return array;
}
if (idx === 0) {
return [...new Set(array)];
}
return previous;
};
},
};