ef.js

Declarative DOM helper experiment

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
// This module "extends" ef.js with a helper function named `map`
import { derived, isReactive, type ReactiveOrStatic } from "../../src/ef.js";

export * from "../../src/ef.js";

/**
 * Applies function `f` for `x` when `x` is not `ReactiveValue`, otherwise
 * Returns a Derived that value is a result of `f(x.get())`.
 *
 * Convenient helper to dealing with `ReactiveOrStatic`.
 * Useful for composing.
 */
export function map<T, P>(
  x: ReactiveOrStatic<T>,
  f: (x: T) => P,
): ReactiveOrStatic<P> {
  if (isReactive(x)) {
    return derived(() => f(x.get()));
  }

  return f(x);
}