meshtastic/src/utils/showcase.ts

23 lines
609 B
TypeScript
Raw Normal View History

2021-11-30 02:46:31 -08:00
export const sortBy = <T>(array: T[], getter: (item: T) => unknown): T[] => {
2023-05-11 18:45:34 -07:00
const sortedArray = [...array];
sortedArray.sort((a, b) =>
getter(a) > getter(b) ? 1 : getter(b) > getter(a) ? -1 : 0,
);
return sortedArray;
2021-11-30 02:46:31 -08:00
};
export const difference = <T>(...arrays: T[][]): T[] => {
2023-05-11 18:45:34 -07:00
return arrays.reduce((a, b) => a.filter((c) => !b.includes(c)));
2021-11-30 02:46:31 -08:00
};
export const toggleListItem = <T>(list: T[], item: T): T[] => {
2023-05-11 18:45:34 -07:00
const itemIndex = list.indexOf(item);
if (itemIndex === -1) {
return list.concat(item);
} else {
const newList = [...list];
newList.splice(itemIndex, 1);
return newList;
}
2021-11-30 02:46:31 -08:00
};