UNPKG

@minionorg/share

Version:
2 lines (1 loc) 2.75 kB
(function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.index={}))})(this,function(o){"use strict";var g=Object.defineProperty;var S=(o,i,a)=>i in o?g(o,i,{enumerable:!0,configurable:!0,writable:!0,value:a}):o[i]=a;var h=(o,i,a)=>(S(o,typeof i!="symbol"?i+"":i,a),a);function i(r){return r.charAt(0).toUpperCase()+r.slice(1)}function a(r){if(!r)throw new SyntaxError("type is must");return function(t){return Object.prototype.toString.call(t)===`[object ${i(r)}]`}}function y(r){if(!r)throw new SyntaxError("type is must");return function(t){return Object.prototype.toString.call(t)===`[object ${i(r)}]`}}a("Function"),a("Object");class d{constructor(){}static notNull(t,n){if(t===null)throw new SyntaxError(n||"The value must not be empty")}static notEmpty(t,n){if(t.length===0)throw new SyntaxError(n||"The String must not be empty")}static errMsg(t){return function(n){return`${t}: ${n}`}}static isString(t){if(t===null)return null;if(y("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(d,"DEFAULT_NOT_NAN_EX_MESSAGE","\u9A8C\u8BC1\u7684\u503C\u4E0D\u662F\u6570\u5B57");const p={24:{default:/^([01]?\d|2[0-3]):([0-5]\d)$/,hasSecond:/^([01]?\d|2[0-3]):([0-5]\d):([0-5]\d)$/},12:{default:/^(0?\d|1[0-2]):([0-5]\d) (A|P)M$/,hasSecond:/^(0?\d|1[0-2]):([0-5]\d):([0-5]\d) (A|P)M$/}};function m(r,t,n){let e;for(const u in t)if(["12","24"].includes(u)){const s=m(r,t[u],u);s&&(e=s)}else if(t[u].test(r))return{mode:n,format:u,matches:r.match(t[u])};return e}function b(r){if(!d.isTimeFormat(r.format))throw new SyntaxError(`${r.format} is not valid format`);if(![24,12].includes(r.mode))throw new SyntaxError(`${r.mode} is not valid mode`);return function(t){const n=m(t,p,void 0);if(!n)throw new SyntaxError(`${t} is not valid time`);const e={h:"",m:"",s:"",b:""};if(n.mode==="12"){const[s,f,c,l]=[...n.matches].splice(1,4);e.h=s,e.m=f,e.s=c,e.b=l,r.mode===24&&l==="P"&&(e.h=+s+12+"")}else if(n.mode==="24"){const[s,f,c]=[...n.matches].splice(1,3);e.h=s,e.m=f,e.s=c}if(r.mode===12&&!e.b){const s=+e.h;s>12?(e.h=s-12+"",e.b="P"):e.b="A"}let u=r.format.replace(/(h{1,2})/gi,e.h).replace(/(m{1,2})/gi,e.m).replace(e.s?/(s{1,2})/gi:/:(s{1,2})/gi,e.s?e.s:"");return r.mode===12&&e.b&&(u+=` ${e.b}M`),u}}o.default=b,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});