You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

functional.ts 864 B

1234567891011121314151617181920212223242526
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-10-12 14:27:29
  4. * @Description: 函数式编程
  5. */
  6. /**
  7. * Safely invokes a function with a given value, returning the result of the
  8. * function or the provided value if it is `undefined` or `null`.
  9. *
  10. * @template T - The type of the input value.
  11. * @template M - The type of the output value.
  12. * @param {function} fn - The function to be invoked with the input value.
  13. * @returns {function} A function that takes a value, invokes `fn` with it if
  14. * it's not `undefined` or `null`, and returns the result or the original value.
  15. */
  16. export function safeInvoke<T, M>(
  17. fn: (value: T) => M | undefined | null,
  18. ): (value: T | undefined | null) => M | undefined | null {
  19. return (arg: T | undefined | null) => {
  20. if (arg === undefined || arg === null) {
  21. return arg as undefined | null;
  22. }
  23. return fn(arg);
  24. };
  25. }