meshtastic/src/utils/showcase.ts

24 lines
646 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[] => {
const sortedArray = [...array];
sortedArray.sort((a, b) =>
2021-12-07 20:28:01 -08:00
// @ts-ignore
getter(a) > getter(b) ? 1 : getter(b) > getter(a) ? -1 : 0,
2021-11-30 02:46:31 -08:00
);
return sortedArray;
};
export const difference = <T>(...arrays: T[][]): T[] => {
return arrays.reduce((a, b) => a.filter((c) => !b.includes(c)));
};
export const toggleListItem = <T>(list: T[], item: T): T[] => {
const itemIndex = list.indexOf(item);
if (itemIndex === -1) {
return list.concat(item);
} else {
const newList = [...list];
newList.splice(itemIndex, 1);
return newList;
}
};