// chrome sort function which built in use different algorithm // when the count greater than 10; In this status, the result isn't// stableconststableSort=([...arr],[...keys])=>{if(keys.length<=0){returnarr;}constkey=keys.shift();constresult=arr.sort((a,b)=>((a[key]-b[key])||(a.$index-b.$index))).map((item,$index)=>({ ...item, $index }));returnstableSort(result,keys);}// examplevarinput=[{height: 100,weight: 80},{height: 90,weight: 90},{height: 70,weight: 95},{height: 100,weight: 100},{height: 80,weight: 110},{height: 110,weight: 115},{height: 100,weight: 120},{height: 70,weight: 125},{height: 70,weight: 130},{height: 100,weight: 135},{height: 75,weight: 140},{height: 70,weight: 140}];stableSort(input,['weight','height'];
// get img's url in <img src="">exportconstgetImgSrc=(str)=>{// [^xyz] 一个反向字符集。也就是说, 它匹配任何没有包含在方括号中的字符constregex=/<img [^>]*src=['"]([^'"]+)[^>]*>/gi;constresult=[];letcur;while((cur=regex.exec(str))!=null){result.push(cur[1]);}returnresult;}// is base64 format?exportconstisDataURL=(str)=>{if(str===null){returnfalse}constregex=/^\s*data:([a-z]+\/[a-z]+(;[a-z-]+=[a-z-]+)?)?(;base64)?,[a-z0-9!$&',()*+;=\-._~:@/?%\s]*\s*$/ireturn!!str.match(regex)}// convert base64url to blob which can be appended in formexportconstconvertBase64UrlToBlob=(urlData)=>{varbytes=window.atob(urlData.split(',')[1]);//去掉url的头,并转换为byte // base64 由于 2^{6}=64,所以每6个比特为一个单元//处理异常,将ascii码小于0的转换为大于0 varab=newArrayBuffer(bytes.length);varia=newUint8Array(ab);for(vari=0;i<bytes.length;i++){ia[i]=bytes.charCodeAt(i);}returnnewBlob([ab],{type : 'image/png'});}exportconstpost=(resources,form)=>{constoData=newFormData(form);if(isDataURL(resources.pic)){// use append to solve browser compatibility, if you use set will error in safari which version is 10.1.2oData.append('picture',utils.convertBase64UrlToBlob(pic_base64));// post to server}}