/*
 * @Author: 赵伟
 * @Date: 2024-10-12 14:27:29
 * @Description: 函数式编程
 */

/**
 * Safely invokes a function with a given value, returning the result of the
 * function or the provided value if it is `undefined` or `null`.
 *
 * @template T - The type of the input value.
 * @template M - The type of the output value.
 * @param {function} fn - The function to be invoked with the input value.
 * @returns {function} A function that takes a value, invokes `fn` with it if
 * it's not `undefined` or `null`, and returns the result or the original value.
 */
export function safeInvoke<T, M>(
  fn: (value: T) => M | undefined | null,
): (value: T | undefined | null) => M | undefined | null {
  return (arg: T | undefined | null) => {
    if (arg === undefined || arg === null) {
      return arg as undefined | null;
    }
    return fn(arg);
  };
}
