import type { ValueOf } from "$lib/types.js"; // typed Object.keys export function keys(o: T) { return Object.keys(o) as Array<`${keyof T & (string | number | boolean | null | undefined)}`>; } // typed Object.entries export function entries(o: T): [keyof T, T[keyof T]][] { return Object.entries(o) as [keyof T, T[keyof T]][]; } // typed Object.fromEntries export function fromEntries(entries: [keyof T, T[keyof T]][]): T { return Object.fromEntries(entries) as T; } export function omit, K extends keyof T>(obj: T, ...keys: K[]): Omit { const result = {} as Omit; for (const key of Object.keys(obj)) { if (!keys.includes(key as unknown as K)) { result[key as keyof Omit] = obj[key] as ValueOf>; } } return result; } export function pick, K extends keyof T>(obj: T, ...keys: K[]): Pick { const result = {} as Pick; for (const key of keys) { result[key] = obj[key] as ValueOf>; } return result; } // $state.snapshot but types are preserved export function snapshot(s: T): T { return $state.snapshot(s) as T; } /** * Try and get a value from an object, or return undefined. * The key does not need to match the type of the object, so the * returned type is an union of all values, and undefined */ export function tryGet>(obj: T, key: string): T[keyof T] | undefined { return obj[key as keyof T]; } // eslint-disable-next-line @typescript-eslint/no-explicit-any type DeepMergeable = { [key: string]: any }; function isPlainObject(value: unknown): value is Record { return value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype; } export function deepMerge(target: T, source: U): T & U { const result: DeepMergeable = { ...target }; for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { const sourceValue = source[key]; const targetValue = result[key]; // Handle arrays - merge them if (Array.isArray(sourceValue)) { result[key] = Array.isArray(targetValue) ? [...targetValue, ...sourceValue] : [...sourceValue]; continue; } // Handle plain objects (not null, not arrays, not class instances) if (isPlainObject(sourceValue)) { result[key] = Object.prototype.hasOwnProperty.call(result, key) && isPlainObject(result[key]) ? deepMerge(result[key], sourceValue) : deepMerge({}, sourceValue); continue; } // Handle primitives and everything else result[key] = sourceValue; } } return result as T & U; } export function renameKey( obj: T, oldKey: keyof T, newKey: string ): { [K in keyof T as K extends typeof oldKey ? typeof newKey : K]: T[K] } { const entries = Object.entries(obj); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result: any = {}; for (const [key, value] of entries) { if (key === oldKey) { result[newKey] = value; } else { result[key] = value; } } return result; }