All files / src/utils record.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 5/5
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 2x     1x 4x               1x 26x           1x 9x 9x 19x 7x    
export function keys<T extends Record<string, unknown>>(record: T): (keyof T)[] {
    return Object.keys(record);
}
 
export function values<T extends Record<string, unknown>>(record: T): T[keyof T][] {
    return Object.values(record) as T[keyof T][];
}
 
type Entry<T extends Record<PropertyKey, unknown>> = {
    key: keyof T;
    value: T[keyof T];
};
 
export function entries<T extends Record<string, unknown>>(record: T): Entry<T>[] {
    return (Object.entries(record) as [keyof T, T[keyof T]][]).map(entry => ({
        key: entry[0],
        value: entry[1]
    }));
}
 
export function mapRecord<T extends Record<string, unknown>, U>(record: T, callbackFn: (value: T[keyof T], key: keyof T) => U): Record<keyof T, U> {
    const newRecord = {} as Record<keyof T, U>;
    for (const entry of entries(record))
        newRecord[entry.key] = callbackFn(entry.value, entry.key);
    return newRecord;
}