Revert "Revert "Permission phase 2""

This reverts commit 8b9244b079.
This commit is contained in:
=
2024-10-18 16:03:36 +05:30
parent 45b9de63f0
commit 93218d5a3f
100 changed files with 3390 additions and 1615 deletions

View File

@@ -13,3 +13,22 @@ export const groupBy = <T, Key extends string | number | symbol>(
acc[groupId].push(item);
return acc;
}, {} as Record<Key, T[]>);
/**
* Given a list of items returns a new list with only
* unique items. Accepts an optional identity function
* to convert each item in the list to a comparable identity
* value
*/
export const unique = <T, K extends string | number | symbol>(
array: readonly T[],
toKey?: (item: T) => K
): T[] => {
const valueMap = array.reduce((acc, item) => {
const key = toKey ? toKey(item) : (item as unknown as string | number | symbol);
if (acc[key]) return acc;
acc[key] = item;
return acc;
}, {} as Record<string | number | symbol, T>);
return Object.values(valueMap);
};

View File

@@ -0,0 +1,20 @@
/**
* Omit a list of properties from an object
* returning a new object with the properties
* that remain
*/
export const omit = <T, TKeys extends keyof T>(obj: T, keys: TKeys[]): Omit<T, TKeys> => {
if (!obj) return {} as Omit<T, TKeys>;
if (!keys || keys.length === 0) return obj as Omit<T, TKeys>;
return keys.reduce(
(acc, key) => {
// Gross, I know, it's mutating the object, but we
// are allowing it in this very limited scope due
// to the performance implications of an omit func.
// Not a pattern or practice to use elsewhere.
delete acc[key];
return acc;
},
{ ...obj }
);
};