You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
环境要求:You will need Node.js version 16+, and PNPM version 7+.
# 克隆官方项目
git clone https://github.com/vuejs/core.git
cd core
pnpm i # install the dependencies of the project
yarn build shared # 打包生成 packages/shared/dist 目录下的 shared 源码
exportconstobjectToString=Object.prototype.toString;exportconsttoTypeString=(value: unknown): string=>objectToString.call(value);exportconsttoRawType=(value: unknown): string=>{// extract "RawType" from strings like "[object RawType]"returntoTypeString(value).slice(8,-1);};exportconstisArray=Array.isArray;exportconstisMap=(val: unknown): val is Map<any,any>=>toTypeString(val)==="[object Map]";exportconstisSet=(val: unknown): val is Set<any>=>toTypeString(val)==="[object Set]";exportconstisDate=(val: unknown): val is Date=>toTypeString(val)==="[object Date]";exportconstisRegExp=(val: unknown): val is RegExp=>toTypeString(val)==="[object RegExp]";exportconstisFunction=(val: unknown): val is Function=>typeofval==="function";exportconstisString=(val: unknown): val is string=>typeofval==="string";exportconstisSymbol=(val: unknown): val is symbol=>typeofval==="symbol";exportconstisObject=(val: unknown): val is Record<any,any>=>val!==null&&typeofval==="object";exportconstisPromise=<T=any>(val: unknown): val is Promise<T>=>{returnisObject(val)&&isFunction(val.then)&&isFunction(val.catch);};exportconstisPlainObject=(val: unknown): val is object=>toTypeString(val)==="[object Object]";
// 对象合并exportconstextend=Object.assign;// 数组删除某项exportconstremove=<T>(arr: T[],el: T)=>{consti=arr.indexOf(el);if(i>-1){arr.splice(i,1);}};// 检测是否属性是否拥有consthasOwnProperty=Object.prototype.hasOwnProperty;exportconsthasOwn=(val: object,key: string|symbol): key is keyoftypeofval=>hasOwnProperty.call(val,key);// 全局对象let_globalThis: any;exportconstgetGlobalThis=(): any=>{return(_globalThis||(_globalThis=typeofglobalThis!=="undefined"
? globalThis
: typeofself!=="undefined"
? self
: typeofwindow!=="undefined"
? window
: typeofglobal!=="undefined"
? global
: {}));};// 劫持对象属性exportconstdef=(obj: object,key: string|symbol,value: any)=>{Object.defineProperty(obj,key,{configurable: true,enumerable: false,
value
});};// 执行数组里的函数exportconstinvokeArrayFns=(fns: Function[],arg?: any)=>{for(leti=0;i<fns.length;i++){fns[i](arg);}};// compare whether a value has changed, accounting for NaN.exportconsthasChanged=(value: any,oldValue: any): boolean=>!Object.is(value,oldValue)
很有用的方法
makeMap 创建 map 检查 key 是否存在
/** * Make a map and return a function for checking if a key * is in that map. * IMPORTANT: all calls of this function must be prefixed with * \/\*#\_\_PURE\_\_\*\/ * So that rollup can tree-shake them if necessary. */exportfunctionmakeMap(str: string,expectsLowerCase?: boolean): (key: string)=>boolean{constmap: Record<string,boolean>=Object.create(null);constlist: Array<string> = str.split(",");
for (let i = 0; i <list.length;i++){map[list[i]]=true;}returnexpectsLowerCase ? val=> !!map[val.toLowerCase()] : val =>!!map[val];}// eg: isHTMLTag('html') => trueconstHTML_TAGS="html,body,base,head,link,meta,style,title,address,article,aside,footer,"+"header,h1,h2,h3,h4,h5,h6,nav,section,div,dd,dl,dt,figcaption,"+"figure,picture,hr,img,li,main,ol,p,pre,ul,a,b,abbr,bdi,bdo,br,cite,code,"+"data,dfn,em,i,kbd,mark,q,rp,rt,ruby,s,samp,small,span,strong,sub,sup,"+"time,u,var,wbr,area,audio,map,track,video,embed,object,param,source,"+"canvas,script,noscript,del,ins,caption,col,colgroup,table,thead,tbody,td,"+"th,tr,button,datalist,fieldset,form,input,label,legend,meter,optgroup,"+"option,output,progress,select,textarea,details,dialog,menu,"+"summary,template,blockquote,iframe,tfoot";constisHTMLTag=makeMap(HTML_TAGS);
松散相等
import{isArray,isDate,isObject,isSymbol}from"./";functionlooseCompareArrays(a: any[],b: any[]){if(a.length!==b.length)returnfalse;letequal=true;for(leti=0;equal&&i<a.length;i++){equal=looseEqual(a[i],b[i]);}returnequal;}// 检查两个值是否松散相等 (不需要引用地址相同,属性和值相同即可)exportfunctionlooseEqual(a: any,b: any): boolean{if(a===b)returntrue;letaValidType=isDate(a);letbValidType=isDate(b);// 时间比较时间戳if(aValidType||bValidType){returnaValidType&&bValidType ? a.getTime()===b.getTime() : false;}// 唯一的aValidType=isSymbol(a);bValidType=isSymbol(b);if(aValidType||bValidType){returna===b;}aValidType=isArray(a);bValidType=isArray(b);// 数组类型if(aValidType||bValidType){returnaValidType&&bValidType ? looseCompareArrays(a,b) : false;}aValidType=isObject(a);bValidType=isObject(b);// 对象类型if(aValidType||bValidType){/* istanbul ignore if: this if will probably never be called */if(!aValidType||!bValidType){returnfalse;}constaKeysCount=Object.keys(a).length;constbKeysCount=Object.keys(b).length;if(aKeysCount!==bKeysCount){returnfalse;}for(constkeyina){constaHasKey=a.hasOwnProperty(key);constbHasKey=b.hasOwnProperty(key);if((aHasKey&&!bHasKey)||(!aHasKey&&bHasKey)||!looseEqual(a[key],b[key])){returnfalse;}}}// 最后这个宽松,认可 String(1) === String('1')、String(true) === String('true')returnString(a)===String(b);}// 返回 val 在 arr 中的索引
export functionlooseIndexOf(arr: any[],val: any): number{returnarr.findIndex(item=>looseEqual(item,val));}
前言
源码阅读
查看贡献指南
环境要求:You will need Node.js version 16+, and PNPM version 7+.
使用
github.dev
在线阅读,https://github.dev/vuejs/core/tree/v3.2.46/packages/shared 。项目使用
下面介绍各个工具函数。
空类(默认值)
类型判断
字符串方法
简化方法
很有用的方法
makeMap 创建 map 检查 key 是否存在
松散相等
转义 HTML
toDisplayString 插值表达式转字符串
normalizeProp 转换 props
后记
基于 @vue/shared 创建自己的实用函数工具库,rainbow-shared !
参考链接
The text was updated successfully, but these errors were encountered: