mirror of
https://github.com/LukeHagar/sveltesociety.dev.git
synced 2025-12-06 12:47:44 +00:00
* Experiment with SSR and query params * Require all tags to be present * Fix when no query param present * Add tag links * Working add/remove tags * Fix templates route * Remove old category/tags selectors * Button style * More styling * Remove unused code * Remove @sindresorhus/slugify * Format and disable prerender * Improve css grid * Use searchParams.getAll * Replace itemsjs with filter/sort functions * Always show selected tags * Add data-sveltekit-noscroll * Move sortableFields to prop level * Simplify code * Fix sortArray for dates * Add an icon to clear filters * Convert tags to kebab-case * Add category to tags * Delete category field * Remove duplicated tags * Merge more tags
35 lines
996 B
TypeScript
35 lines
996 B
TypeScript
import dayjs from 'dayjs';
|
|
|
|
export const filterArray = <T>(arr: T[], filter: string): T[] => {
|
|
if (!filter) {
|
|
return arr;
|
|
}
|
|
// cast to string and lowercase to have non-dependant type and case search
|
|
filter = String(filter).toLowerCase();
|
|
return arr.filter((object) =>
|
|
// get only values from iterated objects
|
|
Object.values(object).some((objValue) => {
|
|
// casting field values to the same shape
|
|
return String(objValue).toLowerCase().includes(filter);
|
|
})
|
|
);
|
|
};
|
|
|
|
export const sortArray = <T>(arr: T[], filter: { value: string; asc: boolean }): T[] => {
|
|
return arr.toSorted((a, b) => {
|
|
if (filter.asc) {
|
|
if (filter.value === 'date') {
|
|
return dayjs(a[filter.value]) > dayjs(b[filter.value]) ? 1 : -1;
|
|
} else {
|
|
return a[filter.value] > b[filter.value] ? 1 : -1;
|
|
}
|
|
} else {
|
|
if (filter.value === 'date') {
|
|
return dayjs(a[filter.value]) < dayjs(b[filter.value]) ? 1 : -1;
|
|
} else {
|
|
return a[filter.value] < b[filter.value] ? 1 : -1;
|
|
}
|
|
}
|
|
});
|
|
};
|