Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @tomato-js/function

Index

Functions

after

  • after(origin: Function, target: Function): (Anonymous function)
  • AOP增强之向后切片

    脚本举例

      import { after } from '@tomato-js/function'
      let origin = (str) => console.log(str);
      const target = ({args,value}) => console.log('after');
      origin = after(origin, target);
      origin('current');//current after

    Parameters

    • origin: Function

      原始函数

    • target: Function

      注入的切片函数

    Returns (Anonymous function)

    新函数

afterReturn

  • afterReturn(origin: Function, target: Function): (Anonymous function)
  • AOP增强之向后切片并透传返回值

    脚本举例

      import { afterReturn } from '@tomato-js/function'
      let origin = (str) => str;
      const target = (str) => console.log(str);
      origin = afterReturn(origin, target);
      origin('current');//current

    Parameters

    • origin: Function

      原始函数

    • target: Function

      注入的切片函数

    Returns (Anonymous function)

    新函数

aop

  • aop(origin: Function, target: Function, aopType: AopType): (Anonymous function)
  • AOP增强底层函数

    脚本举例

      import { aop } from '@tomato-js/function'
      let origin = (str) => console.log(str);
      const target = ({args,value}) => console.log('around');
      origin = aop(origin, target,'around');
      origin('current');//around current around

    Parameters

    • origin: Function

      原始函数

    • target: Function

      注入的切片函数

    • aopType: AopType

      切片方案,支持after/before/around/afterReturn

    Returns (Anonymous function)

    新函数

around

  • around(origin: Function, target: Function): (Anonymous function)
  • AOP增强之前后切片

    脚本举例

      import { around } from '@tomato-js/function'
      let origin = (str) => console.log(str);
      const target = ({args,value}) => console.log('around');
      origin = around(origin, target);
      origin('current');//around current around

    Parameters

    • origin: Function

      原始函数

    • target: Function

      注入的切片函数

    Returns (Anonymous function)

    新函数

before

  • before(origin: Function, target: Function): (Anonymous function)
  • AOP增强之向前切片

    脚本举例

      import { before } from '@tomato-js/function'
      let origin = (str) => console.log(str);
      const target = ({args,value}) => console.log('before');
      origin = before(origin, target);
      origin('current');//before current

    Parameters

    • origin: Function

      原始函数

    • target: Function

      注入的切片函数

    Returns (Anonymous function)

    新函数

both

  • both(...fns: Array<boolean>): Function
  • both(...fns: Array<Function>): Function
  • both增强函数,相当于函数&&

    新增于v0.0.12

    脚本举例

      import { both } from '@tomato-js/function'
      function isOver18(){};
      function isCitizen(){};
      const isEligibleToVote = both(isOver18,isCitizen);
      isEligibleToVote();

    Parameters

    • Rest ...fns: Array<boolean>

      多个条件函数

    Returns Function

    新函数

  • Parameters

    • Rest ...fns: Array<Function>

    Returns Function

compose

  • compose(...args: any[]): (Anonymous function)
  • compose增强函数,相当于f(g(x)),从右到左

    新增于v0.0.12

    脚本举例

      import { compose } from '@tomato-js/function'
      const step1 = (x: number) => (x ? 1 : 2);
      const step2 = (x: number) => (x === 1 ? 3 : 4);
      const step3 = (x: number) => (x === 3 ? 5 : 6);
      const getResult = compose(step3, step2, step1);
      const result = getResult(1);//5

    Parameters

    • Rest ...args: any[]

      多个函数

    Returns (Anonymous function)

    新函数

curry

  • curry(fn: Function, ...args: any[]): any
  • curry增强函数

    新增于v0.0.12

    脚本举例

      import { curry } from '@tomato-js/function'
      function compute(a, b, c, d) {
         return (a + b * c) / d;
      } // f(12, 3, 6, 2) == 15
      const f = curry(compute);
      const g = f(12);
      const result = g(3, 6, 2);//15

    Parameters

    • fn: Function

      需要增强的函数

    • Rest ...args: any[]

      参数列表

    Returns any

    新函数

debounce

  • debounce<F>(func: F, wait?: number, options?: Options): (this: ThisParameterType<F>, ...args: Parameters<F>) => void
  • 函数防抖

    新增于v0.0.17

    脚本举例

      import { debounce } from '@tomato-js/function'
      debounce(() => console.log('debounce'), 1000, { isImmediate: true });

    Type parameters

    Parameters

    • func: F

      调用函数

    • Default value wait: number = 50

      延迟执行毫秒数

    • Default value options: Options = {isImmediate: false}

      是否立即执行,true 表立即执行,false 表非立即执行

    Returns (this: ThisParameterType<F>, ...args: Parameters<F>) => void

    新函数

      • (this: ThisParameterType<F>, ...args: Parameters<F>): void
      • Parameters

        • this: ThisParameterType<F>
        • Rest ...args: Parameters<F>

        Returns void

either

  • either(...fns: Array<boolean>): Function
  • either(...fns: Array<Function>): Function
  • either增强函数,相当于函数||

    新增于v0.0.12

    脚本举例

      import { either } from '@tomato-js/function'
      function isOver18(){};
      function isCitizen(){};
      const isEligibleToVote = either(isOver18,isCitizen);
      isEligibleToVote()

    Parameters

    • Rest ...fns: Array<boolean>

      多个条件函数

    Returns Function

    新函数

  • Parameters

    • Rest ...fns: Array<Function>

    Returns Function

flip

  • flip(func: Function): (Anonymous function)
  • flip增强函数参数反转

    新增于v0.0.12

    脚本举例

      import { flip } from '@tomato-js/function'
      const f = function(a: string, b: string, c: string) {
        return a + " " + b + " " + c;
      };
      const g = flip(f);
      const result = g("a", "b", "c");//c b a

    Parameters

    • func: Function

      需要增强的函数

    Returns (Anonymous function)

    新函数

inverse

  • inverse(func: Function): (Anonymous function)
  • 反转函数逻辑,返回新函数。之前返回true的情况,新函数会返回false

    脚本举例

      import { inverse } from '@tomato-js/function'
      const isTrue = ()=>true;
      const isFalse = inverse(isTrue);
      isTrue()//true
      isFalse()//false

    Parameters

    • func: Function

      需要逻辑反转的函数

    Returns (Anonymous function)

    新函数

once

  • once<T>(func: () => T): (Anonymous function)
  • 控制函数执行一次

    新增于v0.0.12

    脚本举例

      import { once } from '@tomato-js/function'
      const isTrue = ()=>conosle.log('true');
      const onceExec = once(isTrue);
      onceExec()//true
      onceExec()

    Type parameters

    • T

    Parameters

    • func: () => T

      只需执行一次的函数

        • (): T
        • Returns T

    Returns (Anonymous function)

    新函数

partial

  • partial(func: Function, args: any[]): (Anonymous function)
  • partial增强函数偏函数,固定前置参数返回新函数

    新增于v0.0.12

    脚本举例

      import { partial } from '@tomato-js/function'
      function compute(a: number, b: number, c: number, d: number) {
          return (a + b * c) / d;
      } // f(12, 3, 6, 2) == 15
      const f = partial(compute,[12,3]);
      const result = f(6,2);//15

    Parameters

    • func: Function

      需要增强的函数

    • args: any[]

      固定前置参数数组

    Returns (Anonymous function)

    新函数

partialRight

  • partialRight(func: Function, args: any[]): (Anonymous function)
  • partialRight增强函数偏函数,固定后置参数返回新函数

    新增于v0.0.12

    脚本举例

      import { partialRight } from '@tomato-js/function'
      function compute(a: number, b: number, c: number, d: number) {
          return (a + b * c) / d;
      } // f(12, 3, 6, 2) == 15
      const f = partialRight(compute,[6,2]);
      const result = f(12,3);//15

    Parameters

    • func: Function

      需要增强的函数

    • args: any[]

      固定前置参数数组

    Returns (Anonymous function)

    新函数

pipe

  • pipe(...args: any[]): (Anonymous function)
  • pipe增强函数,相当于f(g(x)),从左到右

    新增于v0.0.12

    脚本举例

      import { pipe } from '@tomato-js/function'
      const step1 = (x: number) => (x ? 1 : 2);
      const step2 = (x: number) => (x === 1 ? 3 : 4);
      const step3 = (x: number) => (x === 3 ? 5 : 6);
      const getResult = pipe(step1, step2, step3);
      const result = getResult(1);//5

    Parameters

    • Rest ...args: any[]

      多个函数

    Returns (Anonymous function)

    新函数

poll

  • poll<fnValueType>(__namedParameters: { fn: (...args: unknown[]) => fnValueType | PromiseLike<fnValueType>; interval: number; maxAttempts: number; validate: (value: fnValueType) => boolean }): { cancel: cancel; poller: Promise<unknown> }
  • 轮询方法,支持同步和异步

    新增于v0.0.16

    脚本举例

      import { poll } from '@tomato-js/function'
      const { poller, cancel } = poll({
        fn: getUser,
        validate: v => v.result === true,
        interval: 1000,
        maxAttempts: 20
      });
      //手动取消轮询
      setTimeout(() => cancel(), 7000);
      //满足validate后的fn后续回调
      poller.then(data => {
        console.log('poller done')
        console.log(data);
      });

    Type parameters

    • fnValueType

    Parameters

    • __namedParameters: { fn: (...args: unknown[]) => fnValueType | PromiseLike<fnValueType>; interval: number; maxAttempts: number; validate: (value: fnValueType) => boolean }
      • fn: (...args: unknown[]) => fnValueType | PromiseLike<fnValueType>

        轮询函数

          • (...args: unknown[]): fnValueType | PromiseLike<fnValueType>
          • Parameters

            • Rest ...args: unknown[]

            Returns fnValueType | PromiseLike<fnValueType>

      • interval: number

        间隔时间,默认1000毫秒

      • maxAttempts: number

        最大尝试次数,默认10次

      • validate: (value: fnValueType) => boolean

        验证函数,true则返回resolve

          • (value: fnValueType): boolean
          • Parameters

            • value: fnValueType

            Returns boolean

    Returns { cancel: cancel; poller: Promise<unknown> }

    轮询对象,提供轮询Promise和cancel取消轮询方法

    • cancel: cancel
    • poller: Promise<unknown>

takeTime

  • takeTime(callback: Function): any
  • 输出函数耗时

    脚本举例

      import { takeTime } from '@tomato-js/function'
      takeTime(() => Math.pow(2, 10));

    Parameters

    • callback: Function

      需要统计的函数

    Returns any

    耗时

throttle

  • throttle<F>(func: F, wait?: number, options?: Options): (this: ThisParameterType<F>, ...args: Parameters<F>) => void
  • 函数节流

    新增于v0.0.17

    脚本举例

      import { throttle } from '@tomato-js/function'
      throttle(() => console.log('throttle'), 1000, { isImmediate: true });

    Type parameters

    Parameters

    • func: F

      调用函数

    • Default value wait: number = 50

      延迟执行毫秒数

    • Default value options: Options = {isImmediate: false}

    Returns (this: ThisParameterType<F>, ...args: Parameters<F>) => void

    新函数

      • (this: ThisParameterType<F>, ...args: Parameters<F>): void
      • Parameters

        • this: ThisParameterType<F>
        • Rest ...args: Parameters<F>

        Returns void

times

  • times(n: number, fn: Function, context?: undefined): void
  • 执行多次

    脚本举例

      import { times } from '@tomato-js/function'
      times(5,() => Math.pow(2, 10));

    Parameters

    • n: number

      执行次数

    • fn: Function

      需要执行的函数

    • Default value context: undefined = undefined

      作用域指向

    Returns void

    耗时

Generated using TypeDoc