UNPKG

@minionorg/share

Version:
2 lines (1 loc) 3.75 kB
(function(f,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(f=typeof globalThis<"u"?globalThis:f||self,a(f.index={}))})(this,function(f){"use strict";var E=Object.defineProperty;var T=(f,a,d)=>a in f?E(f,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):f[a]=d;var h=(f,a,d)=>(T(f,typeof a!="symbol"?a+"":a,d),d);function a(e){return e.charAt(0).toUpperCase()+e.slice(1)}function d(e){if(!e)throw new SyntaxError("type is must");return function(t){return Object.prototype.toString.call(t)===`[object ${a(e)}]`}}function l(e){if(!e)throw new SyntaxError("type is must");return function(t){return Object.prototype.toString.call(t)===`[object ${a(e)}]`}}class g{constructor(){}static notNull(t,r){if(t===null)throw new SyntaxError(r||"The value must not be empty")}static notEmpty(t,r){if(t.length===0)throw new SyntaxError(r||"The String must not be empty")}static errMsg(t){return function(r){return`${t}: ${r}`}}static isString(t){if(t===null)return null;if(l("string")(t))return t;throw new TypeError("The parameter type is string")}static isValidDateFormat(t){return/(^(y{4}|y{2})[.\u002f-](m{1,2})[.\u002f-](d{1,2})$)|(^(m{1,2})[.\u002f-](d{1,2})[.\u002f-]((y{4}|y{2})$))|(^(d{1,2})[.\u002f-](m{1,2})[.\u002f-]((y{4}|y{2})$))/gi.test(t)}static isTimeFormat(t){return/(^(h{1,2}):(m{1,2}):(s{1,2})$)|(^(h{1,2}):(m{1,2})$)/gi.test(t)}}h(g,"DEFAULT_NOT_NAN_EX_MESSAGE","\u9A8C\u8BC1\u7684\u503C\u4E0D\u662F\u6570\u5B57"),d("Function"),d("Object");function y(e){return/(^(y{4}|y{2})[.\u002f-](m{1,2})[.\u002f-](d{1,2})$)|(^(m{1,2})[.\u002f-](d{1,2})[.\u002f-]((y{4}|y{2})$))|(^(d{1,2})[.\u002f-](m{1,2})[.\u002f-]((y{4}|y{2})$))/gi.test(e)}function D(e,t){const r=[],i=Math.min(e.length,t.length);for(let n=0;n<i;n++)r.push([e[n],t[n]]);return r}function m(e,t){if(t={format:"YYYY/MM/DD",delimiters:["/","-"],strictMode:!1,...t},l("String")(e)&&y(t.format)){const r=t.delimiters.find(o=>t.format.indexOf(o)!==-1),i=t.strictMode?r:t.delimiters.find(o=>e.indexOf(o)!==-1),n=D(e.split(i),t.format.toLowerCase().split(r)),s={};for(const[o,c]of n){if(o.length!==c.length)return!1;s[c.charAt(0)]=o}return new Date(`${s.m}/${s.d}/${s.y}`).getDate()===+s.d}return t.strictMode?!1:l("Date")(e)}function S(e,t){const r=[],i=Math.min(e.length,t.length);for(let n=0;n<i;n++)r.push([e[n],t[n]]);return r}function w(e,t,r){const i=t.delimiters.find(c=>t.format.indexOf(c)!==-1),n=t.strictMode?i:t.delimiters.find(c=>e.indexOf(c)!==-1),s=S(e.split(n),t.format.toLowerCase().split(i)),o={};for(const[c,u]of s){if(c.length!==u.length)throw new SyntaxError(`${t.format} format is error`);o[u.charAt(0)]=c}return r.format.replace(/(y{4}|y{2})/gi,o.y).replace(/(m{1,2})/gi,o.m).replace(/(d{1,2})/gi,o.d)}function M(e,t){const r=e.getFullYear();return t.format.replace(/(y{4}|y{2})/gi,r+"").replace(/(m{1,2})/gi,n=>n.length===1?String(e.getMonth()+1):String(e.getMonth()+1).padStart(2,"0")).replace(/(d{1,2})/gi,n=>n.length===1?String(e.getDate()):String(e.getDate()).padStart(2,"0"))}function $(e){if(!g.isValidDateFormat(e.format))throw new SyntaxError(`${e.format} is not valid date format`);return function(t,r){if(r={format:"YYYY/MM/DD",delimiters:["/","-"],strictMode:!1,...r},!m(t,r))throw new SyntaxError(`${t} is not valid date`);if(l("String")(t))return w(t,r,e);if(l("Date")(t))return M(t,e)}}function x(e,t,r){const i=r||{start:void 0,finish:void 0};if(!m(e,i.start)||!m(t,i.finish))throw new SyntaxError("Invalid date values");const n=$({format:"YYYY/MM/DD"}),s=24*60*60*1e3,o=new Date(n(e,i.start)),c=new Date(n(t,i.finish)),u=Math.abs(c.getTime()-o.getTime());return Math.ceil(u/s)}f.default=x,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});