@jinming6/ming-tool
Version:
Front-end tool library
2 lines (1 loc) • 5.07 kB
JavaScript
;var e=require("./chunks/vendor-C558v3h3.js");function t(e){console.warn(`[@jinming6/ming-tool warn] ${e}`)}const r="--",n=",";function o(e,t=r){return null==e||""===e?t:e}const a={label:"label",value:"value"};var i,s;function l(e,r){try{const t=new Blob([e],{type:"application/octet-stream"}),n=URL.createObjectURL(t),o=document.createElement("a");return o.href=n,o.download=r,o.target="_blank",o.click(),URL.revokeObjectURL(n),!0}catch(e){return t(`下载文件流失败 ${e.message}`),!1}}function c(e,r){try{const t=document.createElement("a");return t.href=e,t.setAttribute("download",r),t.target="_blank",t.style.display="none",document.body.appendChild(t),t.click(),document.body.removeChild(t),!0}catch(e){return t(`下载文件失败 ${e.message}`),!1}}!function(e){e.ArrayBuffer="arrayBuffer",e.URL="url"}(i||(i={})),function(e){e.RGB="rgb",e.HEX="hex"}(s||(s={}));const u={startDate:"startDate",endDate:"endDate"};function d(t){return e.isString(t)||e.isNumber(t)}exports.DefaultFieldsName=a,exports.DefaultReplaceStr=r,exports.DefaultSplitStr=n,exports.Option=class{constructor(t){this.dataSource=[],this.fieldsName=a;const{dataSource:r,fieldsName:n}=t;this.dataSource=e.isArray(r)?e.cloneDeep(r):[],this.fieldsName=e.isPlainObject(n)?n:a}get options(){return this.dataSource}get labelMap(){const e={};return this.dataSource.forEach((t=>{if(this.fieldsName.value in t&&this.fieldsName.label in t){const r=t[this.fieldsName.value],n=t[this.fieldsName.label];e[r]=n}})),e}getLabel(n){e.isPlainObject(n)||t("getLabel的参数必须是一个对象");const{key:a,allowReplaceEmpty:i=!1,replaceStr:s=r}=n,l=this.labelMap[a];return i?o(l,s):l}update(r){e.isPlainObject(r)||t("update的参数必须是一个对象");const{dataSource:n,fieldsName:o}=r;e.isArray(n)&&(this.dataSource=e.cloneDeep(n)),e.isPlainObject(o)&&(this.fieldsName=o)}},exports.combineDateRange=function(r){const n=[];if(!e.isPlainObject(r))return t("options必须是一个对象"),n;const{obj:o,inStartField:a=u.startDate,inEndField:i=u.endDate}=r;if(!e.isPlainObject(o))return t("obj必须是一个对象"),n;const s=o[a],l=o[i];return d(s)&&d(l)?(n.push(s),n.push(l),n):n},exports.convert2Webp=function(e,t=.8){return new Promise(((r,n)=>{if(!e.type.startsWith("image/"))return void n(new Error("不是图片类型"));if("image/webp"===e.type)return void r(e);const o=window.URL.createObjectURL(e),a=new window.Image;a.src=o,a.onload=()=>{const e=document.createElement("canvas");e.width=a.width,e.height=a.height;const i=e.getContext("2d");if(null===i)return n(new Error("无法获取canvas上下文")),void window.URL.revokeObjectURL(o);i.drawImage(a,0,0,a.width,a.height),e.toBlob((e=>{if(null==e)return n(new Error("无法获取blob对象")),void window.URL.revokeObjectURL(o);r(e),window.URL.revokeObjectURL(o)}),"image/webp",t)},a.onerror=e=>{n(e),window.URL.revokeObjectURL(o)}}))},exports.downloadArrayBuffer=l,exports.downloadFile=function(r){const{inputType:n=i.URL,filename:o,url:a,arrayBuffer:s}=r;return e.isString(o)?n===i.ArrayBuffer?e.isArrayBuffer(s)?l(s,o):(t("arrayBuffer 不能为空"),!1):e.isString(a)?c(a,o):(t("url 不能为空"),!1):(t("filename 不能为空"),!1)},exports.downloadFileV2=function(r){const{type:n=i.URL,filename:o,dataSource:a}=r;return e.isString(o)?n===i.ArrayBuffer?e.isArrayBuffer(a)?l(a,o):(t("dataSource 不能为空"),!1):e.isString(a)?c(a,o):(t("dataSource 不能为空"),!1):(t("filename 不能为空"),!1)},exports.downloadUrl=c,exports.fmtAddressStr=function(r){e.isPlainObject(r)||t("options必须是一个对象");const{addressStr:o,needSplit:a=!0,splitFlag:i=n,needJoin:s=!0,joinFlag:l=n,extraStrArr:c=[]}=r;if(!e.isString(o))return t("addressStr必须是一个字符串"),o;let u=[];return a&&(u=o.split(i).filter((e=>""!==e))),e.isArray(c)?u.push(...c):t("extraStrArr必须是一个数组"),s?u.join(l):u},exports.getFilenameFromDisposition=function(t,r=!0,n=decodeURIComponent){if(!e.isString(t))return null;const o=t.match(/filename\*=[^;]+/);if(null!=o){const e=o[0].split("'");if(3===e.length)return r?n(e[2]):e[2]}const a=t.match(/filename="([^"]+)"/);if(null!=a)return r?n(a[1]):a[1];const i=t.match(/filename=([^;]+)/);return null!=i?r?n(i[1]):i[1]:null},exports.getFilenameFromUrl=function(r){if(!e.isString(r)||""===r)return t("url为空"),"";const n=r.split("/");return n[n.length-1]},exports.getRandomColor=e=>{const{type:t,lightColor:r}=e,n=null!=r&&r?256:201,o=()=>Math.floor(Math.random()*n);if(t===s.RGB)return`rgb(${o()}, ${o()}, ${o()})`;const a=()=>o().toString(16);return`#${a()}${a()}${a()}`},exports.getRandomHex=()=>{const e=()=>Math.floor(256*Math.random()).toString(16);return`#${e()}${e()}${e()}`},exports.getRandomRgb=()=>{const e=()=>Math.floor(256*Math.random());return`rgb(${e()}, ${e()}, ${e()})`},exports.replaceEmpty=o,exports.splitDateRange=function(r){if(!e.isPlainObject(r))return t("options必须是一个对象"),{};const{dateRange:n,outStartField:o=u.startDate,outEndField:a=u.endDate,defaultValue:i=null}=r,s={[o]:i,[a]:i};return e.isArray(n)&&2===n.length?(s[o]=n[0],s[a]=n[1],s):s};