const cacheStringFunction =<Textends(str:string)=>string>(fn:T):T=>{const cache: Record<string,string>= Object.create(null)return((str:string)=>{const hit = cache[str]return hit ||(cache[str]=fn(str))})asany}
hasChanged 是否有变化
// compare whether a value has changed, accounting for NaN.exportconst hasChanged =(value:any, oldValue:any):boolean=>!Object.is(value, oldValue)
invokeArrayFns 执行数组里的函数
exportconstinvokeArrayFns=(fns:Function[], arg?:any)=>{for(let i =0; i < fns.length; i++){
fns[i](arg)}}
def 定义对象属性
exportconstdef=(obj: object, key:string|symbol, value:any)=>{
Object.defineProperty(obj, key,{
configurable:true,
enumerable:false,
value
})}
toNumber 转数字
exportconst toNumber =(val:any):any=>{const n =parseFloat(val)returnisNaN(n)? val : n
}// 其实 isNaN 本意是判断是不是 NaN 值,但是不准确的。ES6有Number.isNaN去判断
Number.isNaN('a')// false
Number.isNaN(NaN);// true
getGlobalThis 全局对象
// 全局属性 globalThis 包含全局的 this 值,类似于全局对象let _globalThis:anyexportconst getGlobalThis =():any=>{return(
_globalThis ||(_globalThis =typeof globalThis !=='undefined'? globalThis
:typeof self !=='undefined'? self
:typeof window !=='undefined'? window
:typeof global !=='undefined'? global
:{}))}