Repository: angular/angular
File: packages/core/src/animation/utils.ts (TypeScript)

Code before:
}

/**
 * Retrieves the list of specified enter animations from the lView
 */
export function getLViewEnterAnimations(
  lView: LView,
): Map<number, NodeAnimations<RunEnterAnimationFn>> {
  const animationData = (lView[ANIMATIONS] ??= {});
  return (animationData.enter ??= new Map());
}

/**
 * Retrieves the list of specified leave animations from the lView
 */
export function getLViewLeaveAnimations(
  lView: LView,
): Map<number, NodeAnimations<RunLeaveAnimationFn>> {
  const animationData = (lView[ANIMATIONS] ??= {});
  return (animationData.leave ??= new Map());
}

/**
 * Gets the list of classes from a passed in value
 */
export function getClassListFromValue(value: string | (() => string | string[])): string[] | null {
  const classes = typeof value === 'function' ? value() : value;
  let classList: string[] | null = Array.isArray(classes) ? classes : null;
  if (typeof classes === 'string') {
    classList = classes
      .trim()
      .split(/\s+/)
      .filter((k) => k);
  }
  return classList;
}

/**
 * Cancels any running enter animations on a given element to prevent them from interfering
 * with leave animations.
 */
export function cancelAnimationsIfRunning(element: HTMLElement, renderer: Renderer): void {
  if (!areAnimationSupported) return;
  const elementData = enterClassMap.get(element);
  if (
    elementData &&
    elementData.classList.length > 0 &&
    elementHasClassList(element, elementData.classList)
  ) {
    for (const klass of elementData.classList) {
      renderer.removeClass(element as unknown as RElement, klass);

Review comment (suggestion):
I think in this case, we should probably create a type for this instead. It gets difficult to read otherwise.

Code after:
    leavingNodes.set(tNode, [el]);
  }
}

/**
 * Retrieves the list of specified enter animations from the lView
 */
export function getLViewEnterAnimations(lView: LView): Map<number, EnterNodeAnimations> {
  const animationData = (lView[ANIMATIONS] ??= {});
  return (animationData.enter ??= new Map());
}

/**
 * Retrieves the list of specified leave animations from the lView
 */
export function getLViewLeaveAnimations(lView: LView): Map<number, LeaveNodeAnimations> {
  const animationData = (lView[ANIMATIONS] ??= {});
  return (animationData.leave ??= new Map());
}

/**
 * Gets the list of classes from a passed in value
 */
export function getClassListFromValue(value: string | AnimationClassBindingFn): string[] | null {
  const classes = typeof value === 'function' ? value() : value;
  let classList: string[] | null = Array.isArray(classes) ? classes : null;
  if (typeof classes === 'string') {
    classList = classes
      .trim()
      .split(/\s+/)
      .filter((k) => k);
  }
  return classList;
}

/**
 * Cancels any running enter animations on a given element to prevent them from interfering
 * with leave animations.
 */
export function cancelAnimationsIfRunning(element: HTMLElement, renderer: Renderer): void {
  if (!areAnimationSupported) return;
  const elementData = enterClassMap.get(element);
  if (
    elementData &&
    elementData.classList.length > 0 &&
    elementHasClassList(element, elementData.classList)
  ) {
    for (const klass of elementData.classList) {
      renderer.removeClass(element as unknown as RElement, klass);
    }
  }