@jecloud/utils
Version:
JECloud 工具库
1 lines • 2.8 kB
JavaScript
import{toDefaultValue as t}from"../system";import{dateFormat as e}from"./date";import{isArray as r,isString as n,isDate as o,isFunction as i,forEach as u,toString as l,get as c,isPlainObject as a,isEmpty as p,isObject as f}from"./lodash";import{execScript4Return as m}from"./sandbox";export function isNumeric(t){return!Number.isNaN(parseFloat(t))&&Number.isFinite(parseFloat(t))}export var isPromise=function(t){return f(t)&&i(t.then)&&i(t.catch)};export function isNotEmpty(t,e){return!p(t,e)}export function toBoolean(t){return"1"==t||1==t}export function uuid(t){void 0===t&&(t=19);for(var e="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),r=[],n=e.length,o=0;o<t;o++)r[o]=e[0|Math.random()*n];return r.join("")}export function toValue(t,e,r){return p(t,r)?e:t}export function encode(t){if(null==t)return"null";if(n(t))return JSON.stringify(t);if(o(t))return e(t,"YYYY-MM-DD HH:mm:ss");if(i(t))return t.toString();if(r(t)){var l=["[",""];return u(t,(function(t){l.push(encode(t),",")})),l[l.length-1]="]",l.join("")}if(a(t)){var c=["{",""];return u(t,(function(t,e){c.push(e,":",encode(t),",")})),c[c.length-1]="}",c.join("")}return JSON.stringify(t)}export function decode(t){if(p(t))return null;var e;try{e=n(t)?m(t):t}catch(e){console.error("decode解析出错:",e,"\n解析内容:",t)}return e}export function toTemplate(e,r,n){var o,i={};return(null!==(o=e.match(/@.*?@/g))&&void 0!==o?o:[]).forEach((function(e){i[e]=t(e)})),l(e).replace((null==n?void 0:n.tmplRE)||/\{(.*?)\}/g,(function(t,e){var n,o;return null!==(o=null!==(n=c(r,e.trim()))&&void 0!==n?n:i[e])&&void 0!==o?o:""}))}export function timer(t,e){for(var r=[],n=2;n<arguments.length;n++)r[n-2]=arguments[n];var o=null,i={cancel:function(){clearInterval(o)},exec:function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];try{!1===t.apply(this,null!=e?e:r)&&clearInterval(o)}catch(t){clearInterval(o)}}};return o=setInterval((function(){i.exec.apply(i,r)}),e),i}export function removeStyleSheet(t){var e,r=document.getElementById(t);null===(e=null==r?void 0:r.parentElement)||void 0===e||e.removeChild(r)}export function createStyleSheet(t,e){removeStyleSheet(e);var r=document,n=r.getElementsByTagName("head")[0],o=r.createElement("style");o.setAttribute("type","text/css"),e&&o.setAttribute("id",e);try{o.appendChild(r.createTextNode(t))}catch(e){o.textContent=t}n.appendChild(o)}export function stripHtmlTags(t){return p(t)?t:String(t).replace(/<\/?[^>]+>/gi,"")}export function isWindow(t){return"undefined"!=typeof window&&"[object Window]"===Object.prototype.toString.call(t)}export function isFormData(t){return"[object FormData]"===Object.prototype.toString.call(t)}export function trimArray(t){if(r(t)){var e=[];return t.forEach((function(t){isNotEmpty(t)&&e.push(t)})),e}return t}