Skip to content

引入

ts
import { parseFunction, cached } from 't-comm';

// 不支持 tree-shaking 的项目
import { parseFunction, cached} from 't-comm/lib/base/function/index';

// 只支持 ESM 的项目
import { parseFunction, cached} from 't-comm/es/base/function/index';

parseFunction(func)

描述

将字符串转为函数

参数

参数名类型描述
funcstring

字符串

返回: function

字符串对应的函数

示例

typescript
parseFunction('()=>console.log(1)')

// ()=>console.log(1)

cached(fn)

描述

记忆函数:缓存函数的运算结果

参数

参数名类型描述
fnfunction

输入函数

返回: any

函数计算结果

示例

typescript
function test(a) {
  return a + 2
}

const cachedTest = cached(test)

cachedTest(1)

// => 3

cachedTest(1)

// => 3