Skip to content

函数工具 ​

从 @axewo/fin-core/utils 或根入口导入。

函数参数与行为
debounce(fn, wait = 300, options?)防抖;支持 leading、trailing,默认尾部执行
throttle(fn, wait = 200, options?)节流;支持 leading、trailing,默认首尾均执行
memoize(fn)单参数记忆化,按值或对象引用缓存结果,不自动限制容量
once(fn)仅首次调用执行,后续返回首次结果
noop(...args)无操作
identity(value)原样返回输入
pipe(...fns)从左至右串联同一输入输出类型的函数
ts
import { debounce, pipe, once } from '@axewo/fin-core/utils'

const normalizeQuery = pipe<string>(
  value => value.trim(),
  value => value.toLowerCase(),
)
const onSearch = debounce((query: string) => console.log(normalizeQuery(query)), 300)
onSearch(' ORDER ')
const initialize = once(() => ({ initialized: true }))
initialize() === initialize() // true

debounce、throttle 使用计时器;它们不是无副作用的数学纯函数。当前公开签名不承诺 cancel、flush 等扩展方法。异步流程、错误重试和缓存淘汰策略由调用方管理,当前没有 retry、delay、compose、curry、before、after、clearMemoizeCache 导出。