All files / react-classic/src props.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

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 27 28  1x                         1x 23x   23x 7x 1x 7x 6x 6x 7x   23x 23x  
import type { ReactiveProps } from './types.js';
import { isRef } from './ref.js';
 
/**
 * Resolves reactive props by unwrapping ref values.
 *
 * This function takes an object of reactive props and returns a new object
 * where any ref values have been replaced with their current values.
 * Non-ref values are passed through unchanged.
 *
 * @template T - The type of the props object
 * @param props - An object containing reactive props, where values may be refs or plain values
 * @returns A new object with the same keys but with ref values unwrapped
 */
export function resolveProps<T>(props: ReactiveProps<T>) {
  const resolvedProps: T = {} as never;
 
  for (const [key, value] of Object.entries(props)) {
    if (isRef(value)) {
      resolvedProps[key as keyof T] = value.value as T[keyof T];
    } else {
      resolvedProps[key as keyof T] = value as T[keyof T];
    }
  }
 
  return resolvedProps;
}