melt
Version:
The next generation of Melt UI. Built for Svelte 5.
26 lines (25 loc) • 852 B
JavaScript
export function findNext(array, condition) {
const index = array.findIndex(condition);
if (index === -1) {
return undefined; // Condition not met
}
const nextIndex = (index + 1) % array.length; // Wrap around
return array[nextIndex];
}
export function findPrev(array, condition) {
const index = array.findIndex(condition);
if (index === -1) {
return undefined; // Condition not met
}
const prevIndex = (index - 1 + array.length) % array.length; // Wrap around
return array[prevIndex];
}
// Map and filter, but using reduce under the hood, for implicit typing.
export function mapAndFilter(array, mapper, filter) {
return array.reduce((acc, item) => {
const mappedItem = mapper(item);
if (filter(mappedItem))
acc.push(mappedItem);
return acc;
}, []);
}