Untitled

 avatar
unknown
plain_text
2 years ago
728 B
2
Indexable
type Categories<T> = Record<string, (keyof T)[]>;
export const categorizeProps = <T>(categories: Categories<T>, changePropName?: (name: string) => string) => {
    const allProps = Object.entries(categories);
    const res: [string, { name: string; table: { category: string } }][] = [];
    allProps.forEach(([category, values]) => {
        values.forEach((value) => {
            res.push([
                value as string,
                {
                    name: changePropName?.(value as string) ?? (value as string),
                    table: {
                        category,
                    },
                },
            ]);
        });
    });

    return Object.fromEntries(res);
};