File size: 3,203 Bytes
1778c9e d7cd63b |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import type { ValueOf } from "$lib/types.js";
// typed Object.keys
export function keys<T extends object>(o: T) {
return Object.keys(o) as Array<`${keyof T & (string | number | boolean | null | undefined)}`>;
}
// typed Object.entries
export function entries<T extends object>(o: T): [keyof T, T[keyof T]][] {
return Object.entries(o) as [keyof T, T[keyof T]][];
}
// typed Object.fromEntries
export function fromEntries<T extends object>(entries: [keyof T, T[keyof T]][]): T {
return Object.fromEntries(entries) as T;
}
export function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K> {
const result = {} as Omit<T, K>;
for (const key of Object.keys(obj)) {
if (!keys.includes(key as unknown as K)) {
result[key as keyof Omit<T, K>] = obj[key] as ValueOf<Omit<T, K>>;
}
}
return result;
}
export function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
const result = {} as Pick<T, K>;
for (const key of keys) {
result[key] = obj[key] as ValueOf<Pick<T, K>>;
}
return result;
}
// $state.snapshot but types are preserved
export function snapshot<T>(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<T extends Record<string, unknown>>(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<string, unknown> {
return value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype;
}
export function deepMerge<T extends DeepMergeable, U extends DeepMergeable>(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<T extends object>(
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;
}
|