leonitousconforti/tinyburg

View on GitHub
packages/nucleus/src/parsing-structs/insert-into-blocks.ts

Summary

Maintainability
A
0 mins
Test Coverage
export const insertIntoBlocks = function <
    T extends Record<string, unknown>,
    U extends keyof T,
    V extends Record<string, unknown>,
>(blocks: T, indexKey: U | undefined, newBlocks: V): T & V {
    const entries = Object.entries(blocks);
    const newBlocksEntries = Object.keys(newBlocks).map((k) => [k, newBlocks[k]])[0];

    if (indexKey) {
        const index = Object.keys(blocks).indexOf(indexKey as string);
        entries.splice(index, 0, newBlocksEntries as never);
    } else {
        entries.push(newBlocksEntries as never);
    }

    return Object.fromEntries(entries) as T & V;
};