@fluixi/utils 1.0.0-alpha.82 → 1.0.0-alpha.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/package.json +5 -5
|
@@ -1,5 +1,2316 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
`),bt=(e,t=80)=>{let r=e.split(" "),n=[],s="";for(let o of r){let i=s.length+o.length+1;s.length+o.length<=t?s+=(s?" ":"")+o:(s&&n.push(s),s=o)}return s&&n.push(s),n},Dt=e=>typeof e=="string",yt=(e,t)=>{if(e==null)return t!==void 0?t:"";let r=e;if(Array.isArray(r)){if(r.length===0)return t!==void 0?t:""}else if(typeof r=="object"&&Object.keys(r).length===0)return t!==void 0?t:"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return String(e)}catch{return t!==void 0?t:""}},wt=(e,t=!0)=>{if(!t||new Set([" "," "," ","_space_"]).has(e))return e;let n=/[.*+?^${}()|[\]\\]/g;return e.replace(n,"\\$&")};function L(e,t,r,n){if(t<0||t>e.length)throw new Error("Start index out of bounds");if(r<t||r>e.length)throw new Error("End index out of bounds");let s=e.substring(0,t),o=e.substring(r);return s+n+o}var St=(e,t,r="start",n="")=>{if(!e||!t||t.length===0||e.length<t.length)return e;if(r==="both"){let o=e;return o.startsWith(t)&&(o=L(o,0,t.length,n)),o.endsWith(t)&&(o=L(o,o.length-t.length,o.length,n)),o}return r==="start"&&e.startsWith(t)||r==="end"&&e.endsWith(t)?r==="start"?n+e.substring(t.length):e.substring(0,e.length-t.length)+n:e},xt=(e,t)=>{let{pattern:r,position:n,newValue:s,useRegex:o=!1,flags:i}={...t};if(!e)return e;s||(s="");let a,u=typeof n>"u"||n==="start";if(o&&r instanceof RegExp){a=r;let c=a.source;n&&(n==="start"?r=`^${c}`:n==="end"?r=`${c}$`:r=`^${c}|${c}$`,r=new RegExp(r,i))}else if(o)n&&(n==="start"?r=`^${r}`:n==="end"?r=`${r}$`:n==="both"&&(r=`^${r}|${r}$`)),a=new RegExp(r,i);else{let c=r;a=new RegExp(u?`^${c}`:n==="end"?`${c}$`:n==="both"?`^${c}|${c}$`:`${c}`,i||"g")}return e.replace(a,s)},_=e=>{if(!e.startsWith("[")||!e.endsWith("]"))return e;let t=e.slice(1,-1).trim();if(t==="")return[];try{let u=`[${t}]`,c=JSON.parse(u);if(Array.isArray(c))return c}catch{}let r=[],n="",s=!1,o="",i=!1,a=0;for(let u=0;u<t.length;u++){let c=t[u];if(i){n+=c,i=!1;continue}if(c==="\\"&&s){i=!0;continue}(c==='"'||c==="'")&&!s?(s=!0,o=c):c===o&&s?(s=!1,o=""):c==="["&&!s?(a++,n+=c):c==="]"&&!s?(a--,n+=c):c===","&&!s&&a===0?(r.push(n.trim()),n=""):n+=c}return n.trim()&&r.push(n.trim()),r.map(u=>{let c=u.trim();return c.startsWith('"')&&c.endsWith('"')||c.startsWith("'")&&c.endsWith("'")?c.slice(1,-1):c})},Mt=e=>{let t=e.trim();if(t.startsWith("[")&&t.endsWith("]")){let r=_(t);return Array.isArray(r)?r:t}if(t.startsWith("{")&&t.endsWith("}"))try{return JSON.parse(t)}catch{let r=H(t);if(r!==void 0)return r;let n=t.replace(/([{,]\s*)(\w+)(\s*:)/g,'$1"$2"$3').replace(/:\s*'([^']*)'/g,':"$1"');try{return JSON.parse(n)}catch{return t}}try{return JSON.parse(t)}catch{return t}},$t=e=>Array.isArray(e)&&e.every(t=>typeof t=="string"),z=(e,t={})=>{let{delimiters:r=["|"],valueKey:n,labelKey:s,defaultObject:o,trimItems:i=!0,filterEmpty:a=!0,returnObjects:u,parsePrimitives:c=!1,preserveNullUndefined:l=!1}=t,p=u??(n!==void 0||s!==void 0||o!==void 0),f=g=>{let h=o?{...o}:{},b=i?g.trim():g;if(n!==void 0&&(h[n]=c?m(b):b),n!==s||n===void 0&&s===void 0){let x;n===void 0&&s===void 0?x=u?"label":n:x=s,x!==void 0&&(h[x]=b)}return h},m=g=>{let h=g.trim();if(h==="")return l?null:"";if(h.toLowerCase()==="null")return l?null:"null";if(h.toLowerCase()==="undefined")return l?null:"undefined";if(h.toLowerCase()==="true")return!0;if(h.toLowerCase()==="false")return!1;if(/^-?\d+$/.test(h)){let b=parseInt(h,10);return isNaN(b)?i?h:g:b}if(/^-?\d+\.\d+$/.test(h)){let b=parseFloat(h);return isNaN(b)?i?h:g:b}return i?h:g},y=g=>{if(g==null)return l?g:null;if(typeof g=="string"){let b=i?g.trim():g;return g==="null"?null:g==="undefined"?l?void 0:"undefined":g===""?l?null:"":a&&b===""?l?b:null:p?f(b):c?m(b):b}if(typeof g=="number"||typeof g=="boolean")if(p){let b=o?{...o}:{};return n!==void 0&&(b[n]=g),s!==void 0&&s!==n&&(b[s]=String(g)),b}else return g;if(typeof g=="object"&&!Array.isArray(g))return p?o?{...o,...g}:g:n&&g[n]!==void 0?g[n]:g;if(Array.isArray(g)){let b=JSON.stringify(g);return p?f(b):b}let h=String(g);return p?f(h):h};if(Array.isArray(e)){let g=e.map(y);return l?g:g.filter(h=>h!==null)}if(typeof e!="string")return e==null?[]:y(String(e))?[y(String(e))]:[];let D=e.trim();if(D==="")return[];if(D.startsWith("[")&&D.endsWith("]")||D.startsWith("{")&&D.endsWith("}"))try{let g=JSON.parse(D);if(Array.isArray(g)){let b=g.map(y);return l?b:b.filter(x=>x!==null)}let h=y(g);return h?[h]:[]}catch{}let E=_(D);if(Array.isArray(E)){let g=E.map(h=>y(h));return l?g:g.filter(h=>h!==null)}for(let g of r)if(D.includes(g)){let b=(i?D:e).split(g).map(x=>y(x));return l?b:b.filter(x=>x!==null&&(!a||x!==""))}let T=y(D);return T?[T]:[]},Tt=(e,t={})=>z(e,{...t,returnObjects:!1}),Et=(e,t={})=>z(e,{...t,returnObjects:!0}),Ot=e=>e.every(t=>t==null||typeof t=="string"||typeof t=="number"||typeof t=="boolean"),It=e=>{let t=e.trim();if(t===""||t.toLowerCase()==="null")return null;if(t.toLowerCase()!=="undefined"){if(t.toLowerCase()==="true")return!0;if(t.toLowerCase()==="false")return!1;if(/^-?\d+$/.test(t)){let r=parseInt(t,10);return isNaN(r)?t:r}if(/^-?\d+\.\d+$/.test(t)){let r=parseFloat(t);return isNaN(r)?t:r}if(/^-?\d+(?:\.\d+)?[eE][+-]?\d+$/.test(t)){let r=parseFloat(t);return isNaN(r)?t:r}return t}};var kt=e=>e===!0||e===!1,ne=e=>{if(e===!0||e===!1||e===0||e===1)return!0;if(typeof e=="string"){let t=e.toLowerCase().trim();return t==="true"||t==="false"||t==="0"||t==="1"}return!1},k=(e,t="strict",r=!1)=>{if(e===null&&t==="strict")throw new Error(`Cannot convert ${e} to boolean with strict strategy`);if(typeof e=="boolean")return e;switch(t){case"strict":return se(e);case"truthy":return!!e;case"falsy":return oe(e);case"string":return ae(e);case"number":return ie(e);default:return!!e}},se=e=>{let t=typeof e=="string"?e.trim().toLowerCase():e;if(t===1||t==="1")return!0;if(t===0||t==="0")return!1;if(typeof t=="string"){let r=t.trim().toLowerCase();if(r==="true")return!0;if(r==="false")return!1;throw new Error(`Cannot convert string "${t}" to boolean with strict strategy`)}if(t===!0)return!0;if(t===!1)return!1;throw new Error(`Cannot convert ${typeof t} to boolean with strict strategy`)},oe=e=>{if(typeof e=="string"){let t=e.trim().toLowerCase();return!(t===""||t==="0"||t==="false"||t==="null"||t==="undefined"||t==="nan")}return typeof e=="number"?!(e===0||isNaN(e)):!!e},ae=e=>{if(typeof e!="string")throw new Error(`String strategy requires string input, got ${typeof e}`);let t=e.trim().toLowerCase();if(["true","yes","y","on","1","enabled","enable","active"].includes(t))return!0;if(["false","no","n","off","0","disabled","disable","inactive"].includes(t))return!1;if(!isNaN(Number(t)))return Number(t)!==0;throw new Error(`Cannot convert string "${e}" to boolean`)},ie=e=>{if(typeof e!="number")throw new Error(`Number strategy requires number input, got ${typeof e}`);return isNaN(e)||e===0?!1:e===1?!0:e!==0},Ct=(e,t)=>{try{return k(e,"strict")}catch{return t!==void 0?t:!1}},ue=e=>{if(!Array.isArray(e)||e.length===0)return!1;for(let t of e)if(!t)return!1;return!0},ce=e=>{if(!Array.isArray(e)||e.length===0)return!1;for(let t of e)if(t)return!0;return!1},le=e=>!ce(e),Yt=e=>!e,Nt=e=>{if(!Array.isArray(e))return!1;let t=0;for(let r of e)if(r&&(t++,t>1))return!1;return t===1},Ft=e=>!ue(e),Rt=e=>le(e),Pt=e=>{if(!Array.isArray(e)||e.length===0)return!0;let t=!!e[0];for(let r=1;r<e.length;r++)if(!!e[r]!==t)return!1;return!0},fe=e=>{if(!Array.isArray(e))return 0;let t=0;for(let r of e)r&&t++;return t},Ut=e=>Array.isArray(e)?e.length-fe(e):0,Ht=(e=!1)=>{let t=e;return()=>(t=!t,t)},ge=(e,t,r="===")=>{switch(r){case"==":return e==t;case"===":return e===t;case"!=":return e!=t;case"!==":return e!==t;case">":return e>t;case"<":return e<t;case">=":return e>=t;case"<=":return e<=t;default:throw new Error(`Unsupported operator: ${r}`)}},Lt=(e,t)=>{for(let r of t)if(!ge(e,r.value,r.operator))return!1;return!0},jt=(e,t)=>{switch(t){case"required":return e===!0;case"optional":return e===!0||e===!1;case"true":return e===!0;case"false":return e===!1;case"truthy":return!!e;case"falsy":return!e;default:return!1}},_t=(e=.5)=>{if(e<0||e>1)throw new Error("Probability must be between 0 and 1");return e===0?!1:Math.random()<=e},zt=(e,t)=>{let r,n;return(...s)=>(r&&clearTimeout(r),r=setTimeout(()=>{n=e(...s)},t),n)},Wt=e=>{let t=new WeakMap;return((...r)=>{let n=null,s="";for(let o=0;o<r.length;o++)if(r[o]!==null&&typeof r[o]=="object"){n=r[o];break}if(s=JSON.stringify(r),n){let o=t.get(n);if(o||(o=new Map,t.set(n,o)),o.has(s))return o.get(s);let i=e(...r);return o.set(s,i),i}else{let o=t.get(Object.prototype)||new Map;if(t.has(Object.prototype)||t.set(Object.prototype,o),o.has(s))return o.get(s);let i=e(...r);return o.set(s,i),i}})},Bt=e=>t=>{for(let r of e)if(!r(t))return!1;return!0},Zt=e=>Array.isArray(e)&&e.every(t=>t===!0||t===!1),Jt=e=>Array.isArray(e)&&e.every(t=>ne(t)),qt=(e,t)=>{let r=k(e),n=k(t);return r!==n};var Y=1e3,N=60,F=60,R=24,$=7;var q=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],v=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],K=["January","February","March","April","May","June","July","August","September","October","November","December"],V=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],Q={defaultTimezone:"UTC",defaultLocale:"en-US",weekStartsOn:0,businessDays:{businessDays:[1,2,3,4,5],holidays:[],holidayCheck:()=>!1,workStartHour:9,workEndHour:17}},S={...Q};function Kt(e){S={...S,...e,businessDays:{...S.businessDays,...e.businessDays||{}}}}function Vt(){S={...Q}}function d(e,t={}){let{format:r="ISO",customFormat:n="",timezone:s=S.defaultTimezone,strict:o=!0,locale:i=S.defaultLocale}=t;if(e instanceof Date&&w(e))return e;let a=null;if(typeof e=="number"){if(a=new Date(e),!w(a))throw new Error(`Invalid timestamp: ${e}`);return W(a,s)}if(typeof e!="string")throw new Error(`Invalid input type: ${typeof e}`);let u=e.trim();if(!u)throw new Error("Empty date string");try{switch(r){case"ISO":case"ISO_DATE":case"ISO_TIME":a=de(u,r);break;case"US":a=me(u);break;case"EU":a=pe(u);break;case"CUSTOM":a=he(u,n);break;case"RFC_2822":a=be(u);break;case"TIMESTAMP":a=De(u);break;default:a=ye(u,i,o)}if(!a||!w(a)){if(o)throw new Error(`Parsing resulted in invalid date from input: "${u}"`);if(a=new Date(u),!w(a))throw new Error(`Invalid date format: ${u}`)}if(s&&s!=="UTC"&&(a=te(a,s)),!a)throw new Error(`Invalid date format: ${u}`);return a}catch(c){if(o)throw new Error(`Failed to parse date "${u}" with format ${r}: ${c instanceof Error?c.message:String(c)}`);let l=new Date(u);if(w(l)){let p=W(l,s);if(!p)throw new Error(`Invalid date format: ${u}`);return p}throw new Error(`Invalid date format: ${u}`)}}function de(e,t){try{if(t==="ISO_DATE"){let n=e.split("-").map(a=>parseInt(a,10));if(n.length<3||n.some(isNaN))throw new Error("Invalid ISO date format");let[s,o,i]=n;return new Date(Date.UTC(s,o-1,i))}if(t==="ISO_TIME"){let s=new Date().toISOString().split("T")[0];return new Date(`${s}T${e}Z`)}let r=new Date(e);if(!w(r))throw new Error("Invalid ISO date");return r}catch{throw new Error(`Invalid ISO ${t} format: ${e}`)}}function me(e){try{let t=e.split("/");if(t.length!==3)throw new Error("Invalid US date format - expected MM/DD/YYYY");let r=parseInt(t[0],10)-1,n=parseInt(t[1],10),s=parseInt(t[2],10);if(isNaN(r)||isNaN(n)||isNaN(s))throw new Error("Invalid US date format - non-numeric parts");if(s<100){let i=new Date().getFullYear(),a=Math.floor(i/100)*100;s+=s<50?a:a-100}if(r<0||r>11)throw new Error(`Month must be between 1 and 12, got ${r+1}`);let o=new Date(s,r,n);if(!w(o))throw new Error("Invalid date");if(o.getFullYear()!==s||o.getMonth()!==r||o.getDate()!==n)throw new Error("Invalid date");return o}catch(t){throw new Error(`Invalid US date format: ${e} - ${t instanceof Error?t.message:String(t)}`)}}function pe(e){try{let t=e.split("/");if(t.length!==3)throw new Error("Invalid EU date format - expected DD/MM/YYYY");let r=parseInt(t[0],10),n=parseInt(t[1],10)-1,s=parseInt(t[2],10);if(isNaN(r)||isNaN(n)||isNaN(s))throw new Error("Invalid EU date format - non-numeric parts");if(s<100){let i=new Date().getFullYear(),a=Math.floor(i/100)*100;s+=s<50?a:a-100}if(n<0||n>11)throw new Error(`Month must be between 1 and 12, got ${n+1}`);let o=new Date(s,n,r);if(!w(o))throw new Error("Invalid date");if(o.getFullYear()!==s||o.getMonth()!==n||o.getDate()!==r)throw new Error("Invalid date");return o}catch(t){throw new Error(`Invalid EU date format: ${e} - ${t instanceof Error?t.message:String(t)}`)}}function he(e,t){try{if(!t)throw new Error("Custom format string is required");let r=t.replace(/YYYY/g,"(\\d{4})").replace(/YY/g,"(\\d{2})").replace(/MM/g,"(\\d{2})").replace(/M/g,"(\\d{1,2})").replace(/DD/g,"(\\d{2})").replace(/D/g,"(\\d{1,2})").replace(/HH/g,"(\\d{2})").replace(/H/g,"(\\d{1,2})").replace(/hh/g,"(\\d{2})").replace(/h/g,"(\\d{1,2})").replace(/mm/g,"(\\d{2})").replace(/m/g,"(\\d{1,2})").replace(/ss/g,"(\\d{2})").replace(/s/g,"(\\d{1,2})"),n=new RegExp(`^${r}$`),s=e.match(n);if(!s)throw new Error(`Input does not match custom format: ${t}`);let o=0,i=0,a=1,u=0,c=0,l=0,p=1,f=t.match(/YYYY|YY|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s/g)||[];for(let y of f){let D=parseInt(s[p],10);if(isNaN(D))throw new Error(`Invalid numeric value for token ${y}`);switch(y){case"YYYY":o=D;break;case"YY":if(o=D,o<100){let E=new Date().getFullYear(),T=Math.floor(E/100)*100;o+=o<50?T:T-100}break;case"MM":case"M":i=D-1;break;case"DD":case"D":a=D;break;case"HH":case"H":u=D;break;case"hh":case"h":u=D;break;case"mm":case"m":c=D;break;case"ss":case"s":l=D;break}p++}if(o<1e3||o>9999)throw new Error(`Year must be between 1000 and 9999: ${o}`);if(i<0||i>11)throw new Error(`Month must be between 1 and 12: ${i+1}`);let m=new Date(o,i,a,u,c,l);if(!w(m))throw new Error("Invalid date");if(m.getFullYear()!==o||m.getMonth()!==i||m.getDate()!==a)throw new Error("Invalid date");return m}catch(r){throw new Error(`Failed to parse with custom format "${t}": ${r instanceof Error?r.message:String(r)}`)}}function be(e){try{let t=new Date(e);if(!w(t))throw new Error("Invalid RFC 2822 date");return t}catch{throw new Error(`Invalid RFC 2822 date format: ${e}`)}}function De(e){try{if(!/^\d{8}(\d{6})?$/.test(e))throw new Error("Invalid timestamp format - expected YYYYMMDD or YYYYMMDDHHMMSS");let t=parseInt(e.substring(0,4),10),r=parseInt(e.substring(4,6),10)-1,n=parseInt(e.substring(6,8),10),s=0,o=0,i=0;if(e.length===14&&(s=parseInt(e.substring(8,10),10),o=parseInt(e.substring(10,12),10),i=parseInt(e.substring(12,14),10)),r<0||r>11)throw new Error(`Month must be between 1 and 12: ${r+1}`);let a=new Date(t,r,n,s,o,i);if(!w(a))throw new Error("Invalid timestamp");return a}catch(t){throw new Error(`Invalid timestamp format: ${e} - ${t instanceof Error?t.message:String(t)}`)}}function ye(e,t,r){try{let n=new Date(e);if(w(n))return n;let s=new Intl.DateTimeFormat(t,{year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric"}),o=new Date(e);if(w(o))return o;if(r)throw new Error(`Could not parse date with locale ${t}`);return new Date(NaN)}catch(n){if(r)throw new Error(`Failed to parse locale date: ${n instanceof Error?n.message:String(n)}`);return new Date(NaN)}}function W(e,t){return e}function w(e){if(!e||typeof e!="object")return!1;let t=e;if(typeof t.getTime!="function"||typeof t.getFullYear!="function")return!1;let r=t.getTime();return!isNaN(r)&&typeof r=="number"&&isFinite(r)}function B(e,t={}){let r=d(e),{format:n="ISO",customFormat:s="",timezone:o=S.defaultTimezone,locale:i=S.defaultLocale,includeTime:a=!0,includeSeconds:u=!1,includeMilliseconds:c=!1}=t,l=r;switch(o&&o!=="UTC"&&(l=te(r,o)),n){case"ISO":return l.toISOString();case"ISO_DATE":return l.toISOString().split("T")[0];case"ISO_TIME":{let p=l.toISOString().split("T")[1];return c?p:p.split(".")[0]}case"US":return Ee(l);case"EU":return Oe(l);case"FULL":return Ie(l,i);case"LONG":return Ae(l,i);case"MEDIUM":return Z(l,i);case"SHORT":return ke(l,i);case"TIME":return J(l,i,u);case"TIME_24":return Ce(l,u);case"DATETIME":return`${Z(l,i)}, ${J(l,i,u)}`;case"TIMESTAMP":return Ye(l);case"RFC_2822":return Ne(l);case"CUSTOM":return Fe(l,s);default:return l.toISOString()}}function O(){return new Date}function P(){return C(new Date)}function Qt(){return G(P(),1)}function Gt(){return Se(P(),1)}function we(e,t,r,n=0,s=0,o=0,i=0){return Re(e,t,r,n,s,o,i),new Date(e,t,r,n,s,o,i)}function I(e,t,r="day"){let n=d(e),s=new Date(n);switch(r){case"millisecond":s.setMilliseconds(s.getMilliseconds()+t);break;case"second":s.setSeconds(s.getSeconds()+t);break;case"minute":s.setMinutes(s.getMinutes()+t);break;case"hour":s.setHours(s.getHours()+t);break;case"day":s.setDate(s.getDate()+t);break;case"week":s.setDate(s.getDate()+t*$);break;case"month":s.setMonth(s.getMonth()+t);break;case"quarter":s.setMonth(s.getMonth()+t*3);break;case"year":s.setFullYear(s.getFullYear()+t);break;default:throw new Error(`Unsupported date unit: ${r}`)}return s}function U(e,t,r="day"){return I(e,-t,r)}function G(e,t){return I(e,t,"day")}function Se(e,t){return U(e,t,"day")}function Xt(e,t){return I(e,t,"month")}function er(e,t){return U(e,t,"month")}function tr(e,t){return I(e,t,"year")}function rr(e,t){return U(e,t,"year")}function M(e,t){let r=d(e),n=new Date(r);switch(t){case"year":n.setMonth(0,1),n.setHours(0,0,0,0);break;case"month":n.setDate(1),n.setHours(0,0,0,0);break;case"day":n.setHours(0,0,0,0);break;case"hour":n.setMinutes(0,0,0);break;case"minute":n.setSeconds(0,0);break;case"second":n.setMilliseconds(0);break;case"week":{let o=(n.getDay()-S.weekStartsOn+$)%$;n.setDate(n.getDate()-o),n.setHours(0,0,0,0);break}case"quarter":{let s=Math.floor(n.getMonth()/3);n.setMonth(s*3,1),n.setHours(0,0,0,0);break}default:throw new Error(`Unsupported unit for startOf: ${t}`)}return n}function A(e,t){let r=M(e,t),n=new Date(r);switch(t){case"year":n.setFullYear(n.getFullYear()+1);break;case"quarter":n.setMonth(n.getMonth()+3);break;case"month":n.setMonth(n.getMonth()+1);break;case"week":n.setDate(n.getDate()+$);break;case"day":n.setDate(n.getDate()+1);break;case"hour":n.setHours(n.getHours()+1);break;case"minute":n.setMinutes(n.getMinutes()+1);break;case"second":n.setSeconds(n.getSeconds()+1);break;default:throw new Error(`Unsupported unit for endOf: ${t}`)}return n.setMilliseconds(n.getMilliseconds()-1),n}function C(e){return M(e,"day")}function nr(e){return A(e,"day")}function sr(e){return M(e,"month")}function or(e){return A(e,"month")}function ar(e){return M(e,"year")}function ir(e){return A(e,"year")}function ur(e){return M(e,"week")}function cr(e){return A(e,"week")}function lr(e,t,r,n=!0){let s=d(e),o=d(t),i=d(r);return n?s>=o&&s<=i:s>o&&s<i}function fr(e,t){return d(e)<d(t)}function gr(e,t){return d(e)>d(t)}function X(e,t,r){let n=d(e),s=d(t);if(!r)return n.getTime()===s.getTime();let o=M(n,r),i=M(s,r);return o.getTime()===i.getTime()}function dr(e){return X(e,P(),"day")}function xe(e){return d(e)<O()}function Me(e){return d(e)>O()}function $e(e,t){let r=d(e),n=d(t),s=Math.abs(n.getTime()-r.getTime()),o=Math.floor(s/Y),i=Math.floor(o/N),a=Math.floor(i/F),u=Math.floor(a/R);return{years:Math.floor(u/365),months:Math.floor(u/30),weeks:Math.floor(u/$),days:u,hours:a,minutes:i,seconds:o,milliseconds:s,totalDays:u,totalHours:a,totalMinutes:i,totalSeconds:o,totalMilliseconds:s}}function mr(e,t=O(),r=!1){let n=$e(e,t),o=d(e)<d(t)?"ago":"in";return n.years>0?r?`${n.years} year${n.years===1?"":"s"} ${o}`:`${n.years}y ${o}`:n.months>0?r?`${n.months} month${n.months===1?"":"s"} ${o}`:`${n.months}mo ${o}`:n.weeks>0?r?`${n.weeks} week${n.weeks===1?"":"s"} ${o}`:`${n.weeks}w ${o}`:n.days>0?r?`${n.days} day${n.days===1?"":"s"} ${o}`:`${n.days}d ${o}`:n.hours>0?r?`${n.hours} hour${n.hours===1?"":"s"} ${o}`:`${n.hours}h ${o}`:n.minutes>0?r?`${n.minutes} minute${n.minutes===1?"":"s"} ${o}`:`${n.minutes}m ${o}`:r?`${n.seconds} second${n.seconds===1?"":"s"} ${o}`:`${n.seconds}s ${o}`}function ee(e){let r=d(e).getDay();return r===0||r===6}function pr(e){return!ee(e)}function Te(e){let t=d(e);return ee(t)||!S.businessDays.businessDays.includes(t.getDay())?!1:!(S.businessDays.holidays?.some(n=>X(n,t,"day"))||S.businessDays.holidayCheck?.(t))}function hr(e,t){let r=d(e),n=0,s=t>0?1:-1;for(;n<Math.abs(t);)r=G(r,s),Te(r)&&n++;return r}function br(e,t,r=!1){let n=C(d(e)),s=C(d(t)),o=Math.abs(s.getTime()-n.getTime()),i=Math.floor(o/(Y*N*F*R));return r?i+1:i}function Dr(e,t){let r=d(e),n=d(t),s=n.getFullYear()-r.getFullYear(),o=n.getMonth()-r.getMonth();return s*12+o}function yr(e,t){let r=d(e);return d(t).getFullYear()-r.getFullYear()}function wr(e,t=O()){let r=d(e),n=d(t),s=n.getFullYear()-r.getFullYear(),o=n.getMonth()-r.getMonth();return(o<0||o===0&&n.getDate()<r.getDate())&&s--,Math.max(0,s)}function Sr(e){let t=d(e),r=new Date(t.getFullYear(),0,1),n=(t.getTime()-r.getTime())/(Y*N*F*R);return Math.ceil((n+r.getDay()+1)/$)}function xr(e){let t=d(e).getMonth();return Math.floor(t/3)+1}function Mr(e,t="full"){let n=d(e).getDay();return t==="full"?q[n]:v[n]}function $r(e,t="full"){let n=d(e).getMonth();return t==="full"?K[n]:V[n]}function Tr(e){let t=d(e);return new Date(t.getFullYear(),t.getMonth()+1,0).getDate()}function Er(e){return e%4===0&&e%100!==0||e%400===0}function Or(e,t={}){let r=[];try{let n=d(e);if(t.required&&!n&&r.push("Date is required"),t.min||t.minDate){let s=d(t.min||t.minDate);n<s&&r.push(`Date must be on or after ${B(s,{format:"MEDIUM"})}`)}if(t.max||t.maxDate){let s=d(t.max||t.maxDate);n>s&&r.push(`Date must be on or before ${B(s,{format:"MEDIUM"})}`)}t.allowPast===!1&&xe(n)&&r.push("Date must be in the future"),t.allowFuture===!1&&Me(n)&&r.push("Date must be in the past")}catch(n){r.push(n instanceof Error?n.message:"Invalid date")}return{isValid:r.length===0,errors:r}}function Ir(e){let t=d(e),r=Intl.DateTimeFormat().resolvedOptions().timeZone,n=-t.getTimezoneOffset(),s=Math.floor(Math.abs(n)/60),o=Math.abs(n)%60,i=n>=0?"+":"-";return{abbreviation:Pe(t),name:r,offset:n,offsetString:`UTC${i}${String(s).padStart(2,"0")}:${String(o).padStart(2,"0")}`,isDST:Ue(t)}}function te(e,t){let r=d(e);if(t==="UTC")return new Date(r.getTime()+r.getTimezoneOffset()*6e4);let s=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric"}).formatToParts(r),o=i=>s.find(a=>a.type===i)?.value;return we(parseInt(o("year")),parseInt(o("month"))-1,parseInt(o("day")),parseInt(o("hour")),parseInt(o("minute")),parseInt(o("second")))}function Ee(e){let t=(e.getMonth()+1).toString().padStart(2,"0"),r=e.getDate().toString().padStart(2,"0"),n=e.getFullYear();return`${t}/${r}/${n}`}function Oe(e){let t=e.getDate().toString().padStart(2,"0"),r=(e.getMonth()+1).toString().padStart(2,"0"),n=e.getFullYear();return`${t}/${r}/${n}`}function Ie(e,t){return new Intl.DateTimeFormat(t,{weekday:"long",year:"numeric",month:"long",day:"numeric"}).format(e)}function Ae(e,t){return new Intl.DateTimeFormat(t,{year:"numeric",month:"long",day:"numeric"}).format(e)}function Z(e,t){return new Intl.DateTimeFormat(t,{year:"numeric",month:"short",day:"numeric"}).format(e)}function ke(e,t){return new Intl.DateTimeFormat(t,{year:"2-digit",month:"numeric",day:"numeric"}).format(e)}function J(e,t,r){return new Intl.DateTimeFormat(t,{hour:"numeric",minute:"2-digit",second:r?"2-digit":void 0,hour12:!0}).format(e)}function Ce(e,t){let r=e.getHours().toString().padStart(2,"0"),n=e.getMinutes().toString().padStart(2,"0"),s=t?`:${e.getSeconds().toString().padStart(2,"0")}`:"";return`${r}:${n}${s}`}function Ye(e){let t=e.getFullYear(),r=(e.getMonth()+1).toString().padStart(2,"0"),n=e.getDate().toString().padStart(2,"0"),s=e.getHours().toString().padStart(2,"0"),o=e.getMinutes().toString().padStart(2,"0"),i=e.getSeconds().toString().padStart(2,"0");return`${t}${r}${n}${s}${o}${i}`}function Ne(e){return e.toUTCString()}function Fe(e,t){let r={YYYY:e.getFullYear().toString(),YY:e.getFullYear().toString().slice(-2),MMMM:K[e.getMonth()],MMM:V[e.getMonth()],MM:(e.getMonth()+1).toString().padStart(2,"0"),M:(e.getMonth()+1).toString(),DDDD:q[e.getDay()],DDD:v[e.getDay()],DD:e.getDate().toString().padStart(2,"0"),D:e.getDate().toString(),HH:e.getHours().toString().padStart(2,"0"),H:e.getHours().toString(),hh:(e.getHours()%12||12).toString().padStart(2,"0"),h:(e.getHours()%12||12).toString(),mm:e.getMinutes().toString().padStart(2,"0"),m:e.getMinutes().toString(),ss:e.getSeconds().toString().padStart(2,"0"),s:e.getSeconds().toString(),SSS:e.getMilliseconds().toString().padStart(3,"0"),A:e.getHours()<12?"AM":"PM",a:e.getHours()<12?"am":"pm"};return t.replace(/YYYY|YY|MMMM|MMM|MM|M|DDDD|DDD|DD|D|HH|H|hh|h|mm|m|ss|s|SSS|A|a/g,n=>r[n]||n)}function Re(e,t,r,n=0,s=0,o=0,i=0){if(e<1e3||e>9999)throw new Error(`Year must be between 1000 and 9999: ${e}`);if(t<0||t>11)throw new Error(`Month must be between 0 and 11: ${t}`);let a=new Date(e,t+1,0).getDate();if(r<1||r>a)throw new Error(`Day must be between 1 and ${a} for month ${t}: ${r}`);if(n<0||n>23)throw new Error(`Hour must be between 0 and 23: ${n}`);if(s<0||s>59)throw new Error(`Minute must be between 0 and 59: ${s}`);if(o<0||o>59)throw new Error(`Second must be between 0 and 59: ${o}`);if(i<0||i>999)throw new Error(`Millisecond must be between 0 and 999: ${i}`)}function Pe(e){let n=new Intl.DateTimeFormat("en-US",{timeZoneName:"short"}).formatToParts(e).find(s=>s.type==="timeZoneName");return n?n.value:"UTC"}function Ue(e){let t=new Date(e.getFullYear(),0,1),r=new Date(e.getFullYear(),6,1),n=Math.max(t.getTimezoneOffset(),r.getTimezoneOffset());return e.getTimezoneOffset()<n}export{I as add,hr as addBusinessDays,G as addDays,Xt as addMonths,tr as addYears,ue as all,ce as any,qe as capitalize,Lt as checkConditions,ge as compare,qt as compareBoolean,Kt as configureDateUtils,te as convertTimezone,Ut as countFalsy,lt as countOccurrences,fe as countTruthy,we as createDate,Bt as createPipeline,Ht as createToggle,br as daysBetween,zt as debounceBoolean,$e as difference,A as endOf,nr as endOfDay,or as endOfMonth,cr as endOfWeek,ir as endOfYear,Ve as endsWith,tt as escapeHtml,j as escapeRegExp,wt as escapeString,ut as extractDomain,B as formatDate,wr as getAge,Mr as getDayName,Tr as getDaysInMonth,pt as getInitials,$r as getMonthName,xr as getQuarter,Ir as getTimezoneInfo,Sr as getWeekNumber,mr as humanizeDifference,Qe as includes,Ge as includesIgnoreCase,gr as isAfter,fr as isBefore,lr as isBetween,kt as isBoolean,Zt as isBooleanArray,ne as isBooleanLike,Jt as isBooleanLikeArray,Te as isBusinessDay,w as isDate,ot as isEmpty,Me as isFuture,Er as isLeapYear,gt as isPalindrome,xe as isPast,Ot as isPrimitiveArray,X as isSame,Dt as isString,$t as isStringArray,dr as isToday,at as isValidEmail,it as isValidUrl,pr as isWeekday,ee as isWeekend,mt as mask,Wt as memoizeBoolean,Dr as monthsBetween,Ft as nand,le as none,Rt as nor,ht as normalizeLineEndings,Yt as not,O as now,st as pad,_ as parseArrayLiteral,d as parseDate,Mt as parseLiteral,It as parsePrimitiveValue,_t as randomBoolean,ct as randomString,ft as removeWhitespace,Xe as replaceAll,et as replaceAllIgnoreCase,St as replaceString,xt as replaceStringPattern,Vt as resetDateConfig,re as reverse,Ct as safeToBoolean,dt as slugify,M as startOf,C as startOfDay,sr as startOfMonth,ur as startOfWeek,ar as startOfYear,Ke as startsWith,z as stringToObjectArray,Et as stringToObjectArrayOnly,Tt as stringToPrimitiveArray,U as subtract,Se as subtractDays,er as subtractMonths,rr as subtractYears,k as toBoolean,We as toCamelCase,Ze as toKebabCase,Be as toPascalCase,Je as toSnakeCase,yt as toString,ve as toTitleCase,P as today,Qt as tomorrow,je as trim,ze as trimEnd,_e as trimStart,nt as truncate,rt as unescapeHtml,jt as validateBooleanConstraint,Or as validateDate,bt as wordWrap,Pt as xnor,Nt as xor,yr as yearsBetween,Gt as yesterday};
|
|
1
|
+
/*! @fluixi/utils v1.0.0-alpha.84 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/lib/primitive/string/parser.ts
|
|
4
|
+
function parseJsObjectLiteral(str, options = {}) {
|
|
5
|
+
const {
|
|
6
|
+
allowComments = true,
|
|
7
|
+
allowUnquotedKeys = true,
|
|
8
|
+
allowSingleQuotes = true,
|
|
9
|
+
allowTrailingCommas = true,
|
|
10
|
+
allowUndefined = false
|
|
11
|
+
} = options;
|
|
12
|
+
let fixed = str.trim();
|
|
13
|
+
if (allowComments) {
|
|
14
|
+
fixed = fixed.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
15
|
+
const lines = fixed.split("\n");
|
|
16
|
+
const cleanedLines = lines.map((line) => {
|
|
17
|
+
let inString = false;
|
|
18
|
+
let stringChar = "";
|
|
19
|
+
let escapeNext = false;
|
|
20
|
+
for (let i = 0; i < line.length; i++) {
|
|
21
|
+
const char = line[i];
|
|
22
|
+
if (escapeNext) {
|
|
23
|
+
escapeNext = false;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (char === "\\") {
|
|
27
|
+
escapeNext = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (!inString && (char === '"' || char === "'")) {
|
|
31
|
+
inString = true;
|
|
32
|
+
stringChar = char;
|
|
33
|
+
} else if (inString && char === stringChar) {
|
|
34
|
+
inString = false;
|
|
35
|
+
stringChar = "";
|
|
36
|
+
} else if (!inString && char === "/" && line[i + 1] === "/") {
|
|
37
|
+
return line.substring(0, i);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return line;
|
|
41
|
+
});
|
|
42
|
+
fixed = cleanedLines.join("\n");
|
|
43
|
+
}
|
|
44
|
+
if (allowUnquotedKeys) {
|
|
45
|
+
fixed = fixed.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)(\s*:)/g, '$1"$2"$3');
|
|
46
|
+
}
|
|
47
|
+
if (allowSingleQuotes) {
|
|
48
|
+
let result = "";
|
|
49
|
+
let inSingleQuote = false;
|
|
50
|
+
let inDoubleQuote = false;
|
|
51
|
+
let escapeNext = false;
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (i < fixed.length) {
|
|
54
|
+
const char = fixed[i];
|
|
55
|
+
if (escapeNext) {
|
|
56
|
+
result += char;
|
|
57
|
+
escapeNext = false;
|
|
58
|
+
i++;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (char === "\\") {
|
|
62
|
+
escapeNext = true;
|
|
63
|
+
result += char;
|
|
64
|
+
i++;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (char === "'" && !inDoubleQuote) {
|
|
68
|
+
if (inSingleQuote) {
|
|
69
|
+
result += '"';
|
|
70
|
+
inSingleQuote = false;
|
|
71
|
+
} else {
|
|
72
|
+
result += '"';
|
|
73
|
+
inSingleQuote = true;
|
|
74
|
+
}
|
|
75
|
+
} else if (char === '"' && !inSingleQuote) {
|
|
76
|
+
if (inDoubleQuote) {
|
|
77
|
+
inDoubleQuote = false;
|
|
78
|
+
} else {
|
|
79
|
+
inDoubleQuote = true;
|
|
80
|
+
}
|
|
81
|
+
result += char;
|
|
82
|
+
} else {
|
|
83
|
+
result += char;
|
|
84
|
+
}
|
|
85
|
+
i++;
|
|
86
|
+
}
|
|
87
|
+
fixed = result;
|
|
88
|
+
}
|
|
89
|
+
if (allowTrailingCommas) {
|
|
90
|
+
let result = "";
|
|
91
|
+
let inString = false;
|
|
92
|
+
let stringChar = "";
|
|
93
|
+
let escapeNext = false;
|
|
94
|
+
let i = 0;
|
|
95
|
+
while (i < fixed.length) {
|
|
96
|
+
const char = fixed[i];
|
|
97
|
+
if (escapeNext) {
|
|
98
|
+
result += char;
|
|
99
|
+
escapeNext = false;
|
|
100
|
+
i++;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (char === "\\") {
|
|
104
|
+
escapeNext = true;
|
|
105
|
+
result += char;
|
|
106
|
+
i++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if ((char === '"' || char === "'") && !inString) {
|
|
110
|
+
inString = true;
|
|
111
|
+
stringChar = char;
|
|
112
|
+
result += char;
|
|
113
|
+
} else if (inString && char === stringChar) {
|
|
114
|
+
inString = false;
|
|
115
|
+
stringChar = "";
|
|
116
|
+
result += char;
|
|
117
|
+
} else if (!inString && char === ",") {
|
|
118
|
+
let j = i + 1;
|
|
119
|
+
while (j < fixed.length && /\s/.test(fixed[j])) {
|
|
120
|
+
j++;
|
|
121
|
+
}
|
|
122
|
+
if (j < fixed.length && (fixed[j] === "]" || fixed[j] === "}")) {
|
|
123
|
+
i++;
|
|
124
|
+
continue;
|
|
125
|
+
} else {
|
|
126
|
+
result += char;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
result += char;
|
|
130
|
+
}
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
fixed = result;
|
|
134
|
+
}
|
|
135
|
+
if (allowUndefined) {
|
|
136
|
+
let result = "";
|
|
137
|
+
let inString = false;
|
|
138
|
+
let stringChar = "";
|
|
139
|
+
let escapeNext = false;
|
|
140
|
+
let i = 0;
|
|
141
|
+
while (i < fixed.length) {
|
|
142
|
+
if (escapeNext) {
|
|
143
|
+
result += fixed[i];
|
|
144
|
+
escapeNext = false;
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (fixed[i] === "\\") {
|
|
149
|
+
escapeNext = true;
|
|
150
|
+
result += fixed[i];
|
|
151
|
+
i++;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if ((fixed[i] === '"' || fixed[i] === "'") && !inString) {
|
|
155
|
+
inString = true;
|
|
156
|
+
stringChar = fixed[i];
|
|
157
|
+
result += fixed[i];
|
|
158
|
+
} else if (inString && fixed[i] === stringChar) {
|
|
159
|
+
inString = false;
|
|
160
|
+
stringChar = "";
|
|
161
|
+
result += fixed[i];
|
|
162
|
+
} else if (!inString && fixed.substr(i, 9) === "undefined") {
|
|
163
|
+
const before = i > 0 ? fixed[i - 1] : "";
|
|
164
|
+
const after = i + 8 < fixed.length ? fixed[i + 9] : "";
|
|
165
|
+
const isWordChar = /[a-zA-Z0-9_$]/.test(before) || /[a-zA-Z0-9_$]/.test(after);
|
|
166
|
+
if (!isWordChar) {
|
|
167
|
+
result += "null";
|
|
168
|
+
i += 9;
|
|
169
|
+
continue;
|
|
170
|
+
} else {
|
|
171
|
+
result += fixed[i];
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
result += fixed[i];
|
|
175
|
+
}
|
|
176
|
+
i++;
|
|
177
|
+
}
|
|
178
|
+
fixed = result;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
return JSON.parse(fixed);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
return void 0;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/lib/primitive/string/string.ts
|
|
188
|
+
var trim = (str) => {
|
|
189
|
+
return str.trim();
|
|
190
|
+
};
|
|
191
|
+
var trimStart = (str) => {
|
|
192
|
+
return str.trimStart();
|
|
193
|
+
};
|
|
194
|
+
var trimEnd = (str) => {
|
|
195
|
+
return str.trimEnd();
|
|
196
|
+
};
|
|
197
|
+
var toCamelCase = (str) => {
|
|
198
|
+
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase()).replace(/^[A-Z]/, (chr) => chr.toLowerCase());
|
|
199
|
+
};
|
|
200
|
+
var toPascalCase = (str) => {
|
|
201
|
+
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase()).replace(/^[a-z]/, (chr) => chr.toUpperCase());
|
|
202
|
+
};
|
|
203
|
+
var toKebabCase = (str) => {
|
|
204
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
|
205
|
+
};
|
|
206
|
+
var toSnakeCase = (str) => {
|
|
207
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase();
|
|
208
|
+
};
|
|
209
|
+
var capitalize = (str) => {
|
|
210
|
+
if (!str) return str;
|
|
211
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
212
|
+
};
|
|
213
|
+
var toTitleCase = (str) => {
|
|
214
|
+
return str.replace(
|
|
215
|
+
/\b\w+/g,
|
|
216
|
+
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
var startsWith = (str, search, position = 0) => {
|
|
220
|
+
return str.startsWith(search, position);
|
|
221
|
+
};
|
|
222
|
+
var endsWith = (str, search, length) => {
|
|
223
|
+
return str.endsWith(search, length);
|
|
224
|
+
};
|
|
225
|
+
var includes = (str, search) => {
|
|
226
|
+
return str.includes(search);
|
|
227
|
+
};
|
|
228
|
+
var includesIgnoreCase = (str, search) => {
|
|
229
|
+
return str.toLowerCase().includes(search.toLowerCase());
|
|
230
|
+
};
|
|
231
|
+
var replaceAll = (str, search, replacement) => {
|
|
232
|
+
if (typeof str.replaceAll === "function") {
|
|
233
|
+
return str.replaceAll(search, replacement);
|
|
234
|
+
}
|
|
235
|
+
return str.replace(new RegExp(escapeRegExp(search), "g"), replacement);
|
|
236
|
+
};
|
|
237
|
+
var replaceAllIgnoreCase = (str, search, replacement) => {
|
|
238
|
+
return str.replace(new RegExp(escapeRegExp(search), "gi"), replacement);
|
|
239
|
+
};
|
|
240
|
+
var escapeRegExp = (str) => {
|
|
241
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
242
|
+
};
|
|
243
|
+
var escapeHtml = (str) => {
|
|
244
|
+
const htmlEntities = {
|
|
245
|
+
"&": "&",
|
|
246
|
+
"<": "<",
|
|
247
|
+
">": ">",
|
|
248
|
+
'"': """,
|
|
249
|
+
"'": "'",
|
|
250
|
+
"/": "/"
|
|
251
|
+
};
|
|
252
|
+
return str.replace(/[&<>"'/]/g, (char) => htmlEntities[char] || char);
|
|
253
|
+
};
|
|
254
|
+
var unescapeHtml = (str) => {
|
|
255
|
+
const htmlEntities = {
|
|
256
|
+
"&": "&",
|
|
257
|
+
"<": "<",
|
|
258
|
+
">": ">",
|
|
259
|
+
""": '"',
|
|
260
|
+
"'": "'",
|
|
261
|
+
"/": "/"
|
|
262
|
+
};
|
|
263
|
+
return str.replace(
|
|
264
|
+
/&(?:amp|lt|gt|quot|#39|#x2F);/g,
|
|
265
|
+
(entity) => htmlEntities[entity] || entity
|
|
266
|
+
);
|
|
267
|
+
};
|
|
268
|
+
var truncate = (str, length, ellipsis = "...") => {
|
|
269
|
+
if (str.length <= length) return str;
|
|
270
|
+
return str.slice(0, length - ellipsis.length) + ellipsis;
|
|
271
|
+
};
|
|
272
|
+
var pad = (str, length, padChar = " ", side = "start") => {
|
|
273
|
+
const paddingNeeded = length - str.length;
|
|
274
|
+
if (paddingNeeded <= 0) return str;
|
|
275
|
+
const padStr = padChar.repeat(paddingNeeded);
|
|
276
|
+
switch (side) {
|
|
277
|
+
case "start":
|
|
278
|
+
return padStr + str;
|
|
279
|
+
case "end":
|
|
280
|
+
return str + padStr;
|
|
281
|
+
case "both":
|
|
282
|
+
const leftPad = Math.floor(paddingNeeded / 2);
|
|
283
|
+
const rightPad = paddingNeeded - leftPad;
|
|
284
|
+
return padChar.repeat(leftPad) + str + padChar.repeat(rightPad);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var isEmpty = (str) => {
|
|
288
|
+
return str.trim().length === 0;
|
|
289
|
+
};
|
|
290
|
+
var isValidEmail = (email) => {
|
|
291
|
+
const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
292
|
+
return emailRegex.test(email.trim());
|
|
293
|
+
};
|
|
294
|
+
var isValidUrl = (url) => {
|
|
295
|
+
try {
|
|
296
|
+
new URL(url);
|
|
297
|
+
return true;
|
|
298
|
+
} catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
var extractDomain = (url) => {
|
|
303
|
+
try {
|
|
304
|
+
const urlObj = new URL(url);
|
|
305
|
+
return urlObj.hostname.replace(/^www\./, "");
|
|
306
|
+
} catch {
|
|
307
|
+
return "";
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
var randomString = (length = 10, charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") => {
|
|
311
|
+
let result = "";
|
|
312
|
+
const charsetLength = charset.length;
|
|
313
|
+
for (let i = 0; i < length; i++) {
|
|
314
|
+
result += charset.charAt(Math.floor(Math.random() * charsetLength));
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
};
|
|
318
|
+
var countOccurrences = (str, search, caseSensitive = true) => {
|
|
319
|
+
if (!search) return 0;
|
|
320
|
+
const source = caseSensitive ? str : str.toLowerCase();
|
|
321
|
+
const target = caseSensitive ? search : search.toLowerCase();
|
|
322
|
+
let count = 0;
|
|
323
|
+
let position = 0;
|
|
324
|
+
while ((position = source.indexOf(target, position)) !== -1) {
|
|
325
|
+
count++;
|
|
326
|
+
position += target.length;
|
|
327
|
+
}
|
|
328
|
+
return count;
|
|
329
|
+
};
|
|
330
|
+
var removeWhitespace = (str) => {
|
|
331
|
+
return str.replace(/\s+/g, "");
|
|
332
|
+
};
|
|
333
|
+
var reverse = (str) => {
|
|
334
|
+
return str.split("").reverse().join("");
|
|
335
|
+
};
|
|
336
|
+
var isPalindrome = (str, caseSensitive = false) => {
|
|
337
|
+
const normalized = caseSensitive ? str : str.toLowerCase();
|
|
338
|
+
const cleaned = normalized.replace(/[^a-z0-9]/gi, "");
|
|
339
|
+
return cleaned === reverse(cleaned);
|
|
340
|
+
};
|
|
341
|
+
var slugify = (str) => {
|
|
342
|
+
return str.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
343
|
+
};
|
|
344
|
+
var mask = (str, visibleStart = 0, visibleEnd = 0, maskChar = "*") => {
|
|
345
|
+
if (visibleStart + visibleEnd >= str.length) {
|
|
346
|
+
return str;
|
|
347
|
+
}
|
|
348
|
+
const start = str.slice(0, visibleStart);
|
|
349
|
+
const end = visibleEnd > 0 ? str.slice(-visibleEnd) : "";
|
|
350
|
+
const middle = maskChar.repeat(str.length - visibleStart - visibleEnd);
|
|
351
|
+
return start + middle + end;
|
|
352
|
+
};
|
|
353
|
+
var getInitials = (name, maxInitials = 2) => {
|
|
354
|
+
return name.split(/\s+/).slice(0, maxInitials).map((word) => word.charAt(0).toUpperCase()).join("");
|
|
355
|
+
};
|
|
356
|
+
var normalizeLineEndings = (str) => {
|
|
357
|
+
return str.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
358
|
+
};
|
|
359
|
+
var wordWrap = (text, lineLength = 80) => {
|
|
360
|
+
const words = text.split(" ");
|
|
361
|
+
const lines = [];
|
|
362
|
+
let currentLine = "";
|
|
363
|
+
for (const word of words) {
|
|
364
|
+
const len = currentLine.length + word.length + 1;
|
|
365
|
+
if (currentLine.length + word.length <= lineLength) {
|
|
366
|
+
currentLine += (currentLine ? " " : "") + word;
|
|
367
|
+
} else {
|
|
368
|
+
if (currentLine) {
|
|
369
|
+
lines.push(currentLine);
|
|
370
|
+
}
|
|
371
|
+
currentLine = word;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (currentLine) {
|
|
375
|
+
lines.push(currentLine);
|
|
376
|
+
}
|
|
377
|
+
return lines;
|
|
378
|
+
};
|
|
379
|
+
var isString = (value) => {
|
|
380
|
+
return typeof value === "string";
|
|
381
|
+
};
|
|
382
|
+
var toString = (value, fallback) => {
|
|
383
|
+
if (value == null) {
|
|
384
|
+
return fallback !== void 0 ? fallback : "";
|
|
385
|
+
}
|
|
386
|
+
const v = value;
|
|
387
|
+
if (Array.isArray(v)) {
|
|
388
|
+
if (v.length === 0) {
|
|
389
|
+
return fallback !== void 0 ? fallback : "";
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
if (typeof v === "object" && Object.keys(v).length === 0) {
|
|
393
|
+
return fallback !== void 0 ? fallback : "";
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (typeof value === "string") {
|
|
397
|
+
return value;
|
|
398
|
+
}
|
|
399
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
400
|
+
return String(value);
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
return String(value);
|
|
404
|
+
} catch {
|
|
405
|
+
return fallback !== void 0 ? fallback : "";
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
var escapeString = (str, escape = true) => {
|
|
409
|
+
if (!escape) {
|
|
410
|
+
return str;
|
|
411
|
+
}
|
|
412
|
+
const exclude = /* @__PURE__ */ new Set([" ", " ", " ", "_space_"]);
|
|
413
|
+
if (exclude.has(str)) {
|
|
414
|
+
return str;
|
|
415
|
+
}
|
|
416
|
+
const regexSpecialChars = /[.*+?^${}()|[\]\\]/g;
|
|
417
|
+
return str.replace(regexSpecialChars, "\\$&");
|
|
418
|
+
};
|
|
419
|
+
function replaceStringByIndex(original, startIndex, endIndex, replacement) {
|
|
420
|
+
if (startIndex < 0 || startIndex > original.length) {
|
|
421
|
+
throw new Error("Start index out of bounds");
|
|
422
|
+
}
|
|
423
|
+
if (endIndex < startIndex || endIndex > original.length) {
|
|
424
|
+
throw new Error("End index out of bounds");
|
|
425
|
+
}
|
|
426
|
+
const before = original.substring(0, startIndex);
|
|
427
|
+
const after = original.substring(endIndex);
|
|
428
|
+
return before + replacement + after;
|
|
429
|
+
}
|
|
430
|
+
var replaceString = (originalString, valueToReplace, position = "start", newValue = "") => {
|
|
431
|
+
if (!originalString || !valueToReplace || valueToReplace.length === 0 || originalString.length < valueToReplace.length) {
|
|
432
|
+
return originalString;
|
|
433
|
+
}
|
|
434
|
+
if (position === "both") {
|
|
435
|
+
let result = originalString;
|
|
436
|
+
if (result.startsWith(valueToReplace)) {
|
|
437
|
+
result = replaceStringByIndex(result, 0, valueToReplace.length, newValue);
|
|
438
|
+
}
|
|
439
|
+
if (result.endsWith(valueToReplace)) {
|
|
440
|
+
result = replaceStringByIndex(
|
|
441
|
+
result,
|
|
442
|
+
result.length - valueToReplace.length,
|
|
443
|
+
result.length,
|
|
444
|
+
newValue
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
return result;
|
|
448
|
+
}
|
|
449
|
+
const shouldReplace = position === "start" && originalString.startsWith(valueToReplace) || position === "end" && originalString.endsWith(valueToReplace);
|
|
450
|
+
if (!shouldReplace) {
|
|
451
|
+
return originalString;
|
|
452
|
+
}
|
|
453
|
+
if (position === "start") {
|
|
454
|
+
return newValue + originalString.substring(valueToReplace.length);
|
|
455
|
+
} else {
|
|
456
|
+
return originalString.substring(
|
|
457
|
+
0,
|
|
458
|
+
originalString.length - valueToReplace.length
|
|
459
|
+
) + newValue;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
var replaceStringPattern = (originalString, options) => {
|
|
463
|
+
let { pattern, position, newValue, useRegex = false, flags } = { ...options };
|
|
464
|
+
if (!originalString) {
|
|
465
|
+
return originalString;
|
|
466
|
+
}
|
|
467
|
+
if (!newValue) {
|
|
468
|
+
newValue = "";
|
|
469
|
+
}
|
|
470
|
+
let regex;
|
|
471
|
+
const isStart = typeof position === "undefined" || position === "start";
|
|
472
|
+
if (useRegex && pattern instanceof RegExp) {
|
|
473
|
+
regex = pattern;
|
|
474
|
+
const regex_string = regex.source;
|
|
475
|
+
if (position) {
|
|
476
|
+
if (position === "start") {
|
|
477
|
+
pattern = `^${regex_string}`;
|
|
478
|
+
} else if (position === "end") {
|
|
479
|
+
pattern = `${regex_string}$`;
|
|
480
|
+
} else {
|
|
481
|
+
pattern = `^${regex_string}|${regex_string}$`;
|
|
482
|
+
}
|
|
483
|
+
pattern = new RegExp(pattern, flags);
|
|
484
|
+
}
|
|
485
|
+
} else if (useRegex) {
|
|
486
|
+
if (position) {
|
|
487
|
+
if (position === "start") {
|
|
488
|
+
pattern = `^${pattern}`;
|
|
489
|
+
} else if (position === "end") {
|
|
490
|
+
pattern = `${pattern}$`;
|
|
491
|
+
} else if (position === "both") {
|
|
492
|
+
pattern = `^${pattern}|${pattern}$`;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
regex = new RegExp(pattern, flags);
|
|
496
|
+
} else {
|
|
497
|
+
const escapedPattern = pattern;
|
|
498
|
+
regex = new RegExp(
|
|
499
|
+
isStart ? `^${escapedPattern}` : position === "end" ? `${escapedPattern}$` : position === "both" ? `^${escapedPattern}|${escapedPattern}$` : `${escapedPattern}`,
|
|
500
|
+
flags || "g"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
return originalString.replace(regex, newValue);
|
|
504
|
+
};
|
|
505
|
+
var parseArrayLiteral = (str) => {
|
|
506
|
+
if (!str.startsWith("[") || !str.endsWith("]")) {
|
|
507
|
+
return str;
|
|
508
|
+
}
|
|
509
|
+
const innerStr = str.slice(1, -1).trim();
|
|
510
|
+
if (innerStr === "") {
|
|
511
|
+
return [];
|
|
512
|
+
}
|
|
513
|
+
try {
|
|
514
|
+
const jsonStr = `[${innerStr}]`;
|
|
515
|
+
const parsed = JSON.parse(jsonStr);
|
|
516
|
+
if (Array.isArray(parsed)) {
|
|
517
|
+
return parsed;
|
|
518
|
+
}
|
|
519
|
+
} catch {
|
|
520
|
+
}
|
|
521
|
+
const items = [];
|
|
522
|
+
let current = "";
|
|
523
|
+
let inQuotes = false;
|
|
524
|
+
let quoteChar = "";
|
|
525
|
+
let escaped = false;
|
|
526
|
+
let depth = 0;
|
|
527
|
+
for (let i = 0; i < innerStr.length; i++) {
|
|
528
|
+
const char = innerStr[i];
|
|
529
|
+
if (escaped) {
|
|
530
|
+
current += char;
|
|
531
|
+
escaped = false;
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (char === "\\" && inQuotes) {
|
|
535
|
+
escaped = true;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
539
|
+
inQuotes = true;
|
|
540
|
+
quoteChar = char;
|
|
541
|
+
} else if (char === quoteChar && inQuotes) {
|
|
542
|
+
inQuotes = false;
|
|
543
|
+
quoteChar = "";
|
|
544
|
+
} else if (char === "[" && !inQuotes) {
|
|
545
|
+
depth++;
|
|
546
|
+
current += char;
|
|
547
|
+
} else if (char === "]" && !inQuotes) {
|
|
548
|
+
depth--;
|
|
549
|
+
current += char;
|
|
550
|
+
} else if (char === "," && !inQuotes && depth === 0) {
|
|
551
|
+
items.push(current.trim());
|
|
552
|
+
current = "";
|
|
553
|
+
} else {
|
|
554
|
+
current += char;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (current.trim()) {
|
|
558
|
+
items.push(current.trim());
|
|
559
|
+
}
|
|
560
|
+
return items.map((item) => {
|
|
561
|
+
const trimmed = item.trim();
|
|
562
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
563
|
+
return trimmed.slice(1, -1);
|
|
564
|
+
}
|
|
565
|
+
return trimmed;
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
var parseLiteral = (str) => {
|
|
569
|
+
const trimmed = str.trim();
|
|
570
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
571
|
+
const result = parseArrayLiteral(trimmed);
|
|
572
|
+
return Array.isArray(result) ? result : trimmed;
|
|
573
|
+
}
|
|
574
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
575
|
+
try {
|
|
576
|
+
return JSON.parse(trimmed);
|
|
577
|
+
} catch {
|
|
578
|
+
const p = parseJsObjectLiteral(trimmed);
|
|
579
|
+
if (p !== void 0) {
|
|
580
|
+
return p;
|
|
581
|
+
}
|
|
582
|
+
const fixed = trimmed.replace(/([{,]\s*)(\w+)(\s*:)/g, '$1"$2"$3').replace(/:\s*'([^']*)'/g, ':"$1"');
|
|
583
|
+
try {
|
|
584
|
+
return JSON.parse(fixed);
|
|
585
|
+
} catch {
|
|
586
|
+
return trimmed;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
try {
|
|
591
|
+
return JSON.parse(trimmed);
|
|
592
|
+
} catch {
|
|
593
|
+
return trimmed;
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
var isStringArray = (value) => {
|
|
597
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
598
|
+
};
|
|
599
|
+
var stringToObjectArray = (input, options = {}) => {
|
|
600
|
+
const {
|
|
601
|
+
delimiters = ["|"],
|
|
602
|
+
valueKey,
|
|
603
|
+
labelKey,
|
|
604
|
+
defaultObject,
|
|
605
|
+
trimItems = true,
|
|
606
|
+
filterEmpty = true,
|
|
607
|
+
returnObjects,
|
|
608
|
+
parsePrimitives = false,
|
|
609
|
+
preserveNullUndefined = false
|
|
610
|
+
} = options;
|
|
611
|
+
const shouldReturnObjects = returnObjects ?? (valueKey !== void 0 || labelKey !== void 0 || defaultObject !== void 0);
|
|
612
|
+
const createObject = (item) => {
|
|
613
|
+
const base = defaultObject ? { ...defaultObject } : {};
|
|
614
|
+
const trimmed = trimItems ? item.trim() : item;
|
|
615
|
+
if (valueKey !== void 0) {
|
|
616
|
+
base[valueKey] = parsePrimitives ? parsePrimitiveValue2(trimmed) : trimmed;
|
|
617
|
+
}
|
|
618
|
+
if (valueKey !== labelKey || valueKey === void 0 && labelKey === void 0) {
|
|
619
|
+
let label;
|
|
620
|
+
if (valueKey === void 0 && labelKey === void 0) {
|
|
621
|
+
label = returnObjects ? "label" : valueKey;
|
|
622
|
+
} else {
|
|
623
|
+
label = labelKey;
|
|
624
|
+
}
|
|
625
|
+
if (label !== void 0) {
|
|
626
|
+
base[label] = trimmed;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return base;
|
|
630
|
+
};
|
|
631
|
+
const parsePrimitiveValue2 = (str2) => {
|
|
632
|
+
const trimmed = str2.trim();
|
|
633
|
+
if (trimmed === "") {
|
|
634
|
+
return preserveNullUndefined ? null : "";
|
|
635
|
+
}
|
|
636
|
+
if (trimmed.toLowerCase() === "null") {
|
|
637
|
+
return preserveNullUndefined ? null : "null";
|
|
638
|
+
}
|
|
639
|
+
if (trimmed.toLowerCase() === "undefined") {
|
|
640
|
+
return preserveNullUndefined ? null : "undefined";
|
|
641
|
+
}
|
|
642
|
+
if (trimmed.toLowerCase() === "true") return true;
|
|
643
|
+
if (trimmed.toLowerCase() === "false") return false;
|
|
644
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
645
|
+
const num = parseInt(trimmed, 10);
|
|
646
|
+
return isNaN(num) ? trimItems ? trimmed : str2 : num;
|
|
647
|
+
}
|
|
648
|
+
if (/^-?\d+\.\d+$/.test(trimmed)) {
|
|
649
|
+
const num = parseFloat(trimmed);
|
|
650
|
+
return isNaN(num) ? trimItems ? trimmed : str2 : num;
|
|
651
|
+
}
|
|
652
|
+
if (!trimItems) return str2;
|
|
653
|
+
return trimmed;
|
|
654
|
+
};
|
|
655
|
+
const processItem = (item) => {
|
|
656
|
+
if (item == null) {
|
|
657
|
+
return preserveNullUndefined ? item : null;
|
|
658
|
+
}
|
|
659
|
+
if (typeof item === "string") {
|
|
660
|
+
const processed2 = trimItems ? item.trim() : item;
|
|
661
|
+
if (item === "null")
|
|
662
|
+
return preserveNullUndefined ? null : null;
|
|
663
|
+
if (item === "undefined")
|
|
664
|
+
return preserveNullUndefined ? void 0 : "undefined";
|
|
665
|
+
if (item === "")
|
|
666
|
+
return preserveNullUndefined ? null : "";
|
|
667
|
+
if (filterEmpty && processed2 === "") {
|
|
668
|
+
return preserveNullUndefined ? processed2 : null;
|
|
669
|
+
}
|
|
670
|
+
if (shouldReturnObjects) {
|
|
671
|
+
return createObject(processed2);
|
|
672
|
+
} else {
|
|
673
|
+
return parsePrimitives ? parsePrimitiveValue2(processed2) : processed2;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
if (typeof item === "number" || typeof item === "boolean") {
|
|
677
|
+
if (shouldReturnObjects) {
|
|
678
|
+
const base = defaultObject ? { ...defaultObject } : {};
|
|
679
|
+
if (valueKey !== void 0) base[valueKey] = item;
|
|
680
|
+
if (labelKey !== void 0 && labelKey !== valueKey)
|
|
681
|
+
base[labelKey] = String(item);
|
|
682
|
+
return base;
|
|
683
|
+
} else {
|
|
684
|
+
return item;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (typeof item === "object" && !Array.isArray(item)) {
|
|
688
|
+
if (shouldReturnObjects) {
|
|
689
|
+
return defaultObject ? { ...defaultObject, ...item } : item;
|
|
690
|
+
} else {
|
|
691
|
+
if (valueKey && item[valueKey] !== void 0) {
|
|
692
|
+
return item[valueKey];
|
|
693
|
+
}
|
|
694
|
+
return item;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
if (Array.isArray(item)) {
|
|
698
|
+
const str3 = JSON.stringify(item);
|
|
699
|
+
if (shouldReturnObjects) {
|
|
700
|
+
return createObject(str3);
|
|
701
|
+
} else {
|
|
702
|
+
return str3;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const str2 = String(item);
|
|
706
|
+
if (shouldReturnObjects) {
|
|
707
|
+
return createObject(str2);
|
|
708
|
+
} else {
|
|
709
|
+
return str2;
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
if (Array.isArray(input)) {
|
|
713
|
+
const result = input.map(processItem);
|
|
714
|
+
if (preserveNullUndefined) {
|
|
715
|
+
return result;
|
|
716
|
+
}
|
|
717
|
+
return result.filter((item) => item !== null);
|
|
718
|
+
}
|
|
719
|
+
if (typeof input !== "string") {
|
|
720
|
+
if (input == null) return [];
|
|
721
|
+
return processItem(String(input)) ? [processItem(String(input))] : [];
|
|
722
|
+
}
|
|
723
|
+
const str = input.trim();
|
|
724
|
+
if (str === "") {
|
|
725
|
+
return [];
|
|
726
|
+
}
|
|
727
|
+
if (str.startsWith("[") && str.endsWith("]") || str.startsWith("{") && str.endsWith("}")) {
|
|
728
|
+
try {
|
|
729
|
+
const parsed = JSON.parse(str);
|
|
730
|
+
if (Array.isArray(parsed)) {
|
|
731
|
+
const result = parsed.map(processItem);
|
|
732
|
+
if (preserveNullUndefined) {
|
|
733
|
+
return result;
|
|
734
|
+
}
|
|
735
|
+
return result.filter((item) => item !== null);
|
|
736
|
+
}
|
|
737
|
+
const processed2 = processItem(parsed);
|
|
738
|
+
return processed2 ? [processed2] : [];
|
|
739
|
+
} catch {
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
const literalResult = parseArrayLiteral(str);
|
|
743
|
+
if (Array.isArray(literalResult)) {
|
|
744
|
+
const result = literalResult.map((item) => {
|
|
745
|
+
const processed2 = processItem(item);
|
|
746
|
+
return processed2;
|
|
747
|
+
});
|
|
748
|
+
if (preserveNullUndefined) {
|
|
749
|
+
return result;
|
|
750
|
+
}
|
|
751
|
+
return result.filter((item) => item !== null);
|
|
752
|
+
}
|
|
753
|
+
for (const delimiter of delimiters) {
|
|
754
|
+
if (str.includes(delimiter)) {
|
|
755
|
+
const parts = (trimItems ? str : input).split(delimiter);
|
|
756
|
+
const result = parts.map((part) => processItem(part));
|
|
757
|
+
if (preserveNullUndefined) {
|
|
758
|
+
return result;
|
|
759
|
+
}
|
|
760
|
+
return result.filter(
|
|
761
|
+
(item) => item !== null && (!filterEmpty || item !== "")
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
const processed = processItem(str);
|
|
766
|
+
return processed ? [processed] : [];
|
|
767
|
+
};
|
|
768
|
+
var stringToPrimitiveArray = (input, options = {}) => {
|
|
769
|
+
return stringToObjectArray(input, {
|
|
770
|
+
...options,
|
|
771
|
+
returnObjects: false
|
|
772
|
+
});
|
|
773
|
+
};
|
|
774
|
+
var stringToObjectArrayOnly = (input, options = {}) => {
|
|
775
|
+
return stringToObjectArray(input, {
|
|
776
|
+
...options,
|
|
777
|
+
returnObjects: true
|
|
778
|
+
});
|
|
779
|
+
};
|
|
780
|
+
var isPrimitiveArray = (arr) => {
|
|
781
|
+
return arr.every(
|
|
782
|
+
(item) => item == null || typeof item === "string" || typeof item === "number" || typeof item === "boolean"
|
|
783
|
+
);
|
|
784
|
+
};
|
|
785
|
+
var parsePrimitiveValue = (str) => {
|
|
786
|
+
const trimmed = str.trim();
|
|
787
|
+
if (trimmed === "") return null;
|
|
788
|
+
if (trimmed.toLowerCase() === "null") return null;
|
|
789
|
+
if (trimmed.toLowerCase() === "undefined") return void 0;
|
|
790
|
+
if (trimmed.toLowerCase() === "true") return true;
|
|
791
|
+
if (trimmed.toLowerCase() === "false") return false;
|
|
792
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
793
|
+
const num = parseInt(trimmed, 10);
|
|
794
|
+
return isNaN(num) ? trimmed : num;
|
|
795
|
+
}
|
|
796
|
+
if (/^-?\d+\.\d+$/.test(trimmed)) {
|
|
797
|
+
const num = parseFloat(trimmed);
|
|
798
|
+
return isNaN(num) ? trimmed : num;
|
|
799
|
+
}
|
|
800
|
+
if (/^-?\d+(?:\.\d+)?[eE][+-]?\d+$/.test(trimmed)) {
|
|
801
|
+
const num = parseFloat(trimmed);
|
|
802
|
+
return isNaN(num) ? trimmed : num;
|
|
803
|
+
}
|
|
804
|
+
return trimmed;
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
// src/lib/primitive/boolean/boolean.ts
|
|
808
|
+
var isBoolean = (value) => {
|
|
809
|
+
return value === true || value === false;
|
|
810
|
+
};
|
|
811
|
+
var isBooleanLike = (value) => {
|
|
812
|
+
if (value === true || value === false) return true;
|
|
813
|
+
if (value === 0 || value === 1) return true;
|
|
814
|
+
if (typeof value === "string") {
|
|
815
|
+
const lower = value.toLowerCase().trim();
|
|
816
|
+
return lower === "true" || lower === "false" || lower === "0" || lower === "1";
|
|
817
|
+
}
|
|
818
|
+
return false;
|
|
819
|
+
};
|
|
820
|
+
var toBoolean = (value, strategy = "strict", debug = false) => {
|
|
821
|
+
if (value === null) {
|
|
822
|
+
if (strategy === "strict") {
|
|
823
|
+
throw new Error(
|
|
824
|
+
`Cannot convert ${value} to boolean with strict strategy`
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
if (typeof value === "boolean") {
|
|
829
|
+
return value;
|
|
830
|
+
}
|
|
831
|
+
switch (strategy) {
|
|
832
|
+
case "strict":
|
|
833
|
+
return convertStrict(value);
|
|
834
|
+
case "truthy":
|
|
835
|
+
return Boolean(value);
|
|
836
|
+
case "falsy":
|
|
837
|
+
return convertFalsy(value);
|
|
838
|
+
// return !convertFalsy(value);
|
|
839
|
+
case "string":
|
|
840
|
+
return convertFromString(value);
|
|
841
|
+
case "number":
|
|
842
|
+
return convertFromNumber(value);
|
|
843
|
+
default:
|
|
844
|
+
return Boolean(value);
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
var convertStrict = (_value) => {
|
|
848
|
+
const value = typeof _value === "string" ? _value.trim().toLowerCase() : _value;
|
|
849
|
+
if (value === 1 || value === "1") return true;
|
|
850
|
+
if (value === 0 || value === "0") return false;
|
|
851
|
+
if (typeof value === "string") {
|
|
852
|
+
const trimmed = value.trim().toLowerCase();
|
|
853
|
+
if (trimmed === "true") return true;
|
|
854
|
+
if (trimmed === "false") return false;
|
|
855
|
+
throw new Error(
|
|
856
|
+
`Cannot convert string "${value}" to boolean with strict strategy`
|
|
857
|
+
);
|
|
858
|
+
}
|
|
859
|
+
if (value === true) return true;
|
|
860
|
+
if (value === false) return false;
|
|
861
|
+
throw new Error(
|
|
862
|
+
`Cannot convert ${typeof value} to boolean with strict strategy`
|
|
863
|
+
);
|
|
864
|
+
};
|
|
865
|
+
var convertFalsy = (value) => {
|
|
866
|
+
if (typeof value === "string") {
|
|
867
|
+
const trimmed = value.trim().toLowerCase();
|
|
868
|
+
if (trimmed === "" || trimmed === "0" || trimmed === "false" || trimmed === "null" || trimmed === "undefined" || trimmed === "nan") {
|
|
869
|
+
return false;
|
|
870
|
+
}
|
|
871
|
+
return true;
|
|
872
|
+
}
|
|
873
|
+
if (typeof value === "number") {
|
|
874
|
+
if (value === 0 || isNaN(value)) return false;
|
|
875
|
+
return true;
|
|
876
|
+
}
|
|
877
|
+
return Boolean(value);
|
|
878
|
+
};
|
|
879
|
+
var convertFromString = (value) => {
|
|
880
|
+
if (typeof value !== "string") {
|
|
881
|
+
throw new Error(
|
|
882
|
+
`String strategy requires string input, got ${typeof value}`
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
const trimmed = value.trim().toLowerCase();
|
|
886
|
+
if (["true", "yes", "y", "on", "1", "enabled", "enable", "active"].includes(
|
|
887
|
+
trimmed
|
|
888
|
+
)) {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
if ([
|
|
892
|
+
"false",
|
|
893
|
+
"no",
|
|
894
|
+
"n",
|
|
895
|
+
"off",
|
|
896
|
+
"0",
|
|
897
|
+
"disabled",
|
|
898
|
+
"disable",
|
|
899
|
+
"inactive"
|
|
900
|
+
].includes(trimmed)) {
|
|
901
|
+
return false;
|
|
902
|
+
}
|
|
903
|
+
if (!isNaN(Number(trimmed))) {
|
|
904
|
+
return Number(trimmed) !== 0;
|
|
905
|
+
}
|
|
906
|
+
throw new Error(`Cannot convert string "${value}" to boolean`);
|
|
907
|
+
};
|
|
908
|
+
var convertFromNumber = (value) => {
|
|
909
|
+
if (typeof value !== "number") {
|
|
910
|
+
throw new Error(
|
|
911
|
+
`Number strategy requires number input, got ${typeof value}`
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
if (isNaN(value)) return false;
|
|
915
|
+
if (value === 0) return false;
|
|
916
|
+
if (value === 1) return true;
|
|
917
|
+
return value !== 0;
|
|
918
|
+
};
|
|
919
|
+
var safeToBoolean = (value, fallback) => {
|
|
920
|
+
try {
|
|
921
|
+
return toBoolean(value, "strict");
|
|
922
|
+
} catch {
|
|
923
|
+
return fallback !== void 0 ? fallback : false;
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
var all = (values) => {
|
|
927
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
928
|
+
return false;
|
|
929
|
+
}
|
|
930
|
+
for (const value of values) {
|
|
931
|
+
if (!Boolean(value)) {
|
|
932
|
+
return false;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
return true;
|
|
936
|
+
};
|
|
937
|
+
var any = (values) => {
|
|
938
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
939
|
+
return false;
|
|
940
|
+
}
|
|
941
|
+
for (const value of values) {
|
|
942
|
+
if (Boolean(value)) {
|
|
943
|
+
return true;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
return false;
|
|
947
|
+
};
|
|
948
|
+
var none = (values) => {
|
|
949
|
+
return !any(values);
|
|
950
|
+
};
|
|
951
|
+
var not = (value) => {
|
|
952
|
+
return !Boolean(value);
|
|
953
|
+
};
|
|
954
|
+
var xor = (values) => {
|
|
955
|
+
if (!Array.isArray(values)) {
|
|
956
|
+
return false;
|
|
957
|
+
}
|
|
958
|
+
let truthyCount = 0;
|
|
959
|
+
for (const value of values) {
|
|
960
|
+
if (Boolean(value)) {
|
|
961
|
+
truthyCount++;
|
|
962
|
+
if (truthyCount > 1) {
|
|
963
|
+
return false;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
return truthyCount === 1;
|
|
968
|
+
};
|
|
969
|
+
var nand = (values) => {
|
|
970
|
+
return !all(values);
|
|
971
|
+
};
|
|
972
|
+
var nor = (values) => {
|
|
973
|
+
return none(values);
|
|
974
|
+
};
|
|
975
|
+
var xnor = (values) => {
|
|
976
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
977
|
+
return true;
|
|
978
|
+
}
|
|
979
|
+
const firstTruthy = Boolean(values[0]);
|
|
980
|
+
for (let i = 1; i < values.length; i++) {
|
|
981
|
+
if (Boolean(values[i]) !== firstTruthy) {
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
return true;
|
|
986
|
+
};
|
|
987
|
+
var countTruthy = (values) => {
|
|
988
|
+
if (!Array.isArray(values)) {
|
|
989
|
+
return 0;
|
|
990
|
+
}
|
|
991
|
+
let count = 0;
|
|
992
|
+
for (const value of values) {
|
|
993
|
+
if (Boolean(value)) {
|
|
994
|
+
count++;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return count;
|
|
998
|
+
};
|
|
999
|
+
var countFalsy = (values) => {
|
|
1000
|
+
if (!Array.isArray(values)) {
|
|
1001
|
+
return 0;
|
|
1002
|
+
}
|
|
1003
|
+
return values.length - countTruthy(values);
|
|
1004
|
+
};
|
|
1005
|
+
var createToggle = (initialState = false) => {
|
|
1006
|
+
let state = initialState;
|
|
1007
|
+
return () => {
|
|
1008
|
+
state = !state;
|
|
1009
|
+
return state;
|
|
1010
|
+
};
|
|
1011
|
+
};
|
|
1012
|
+
var compare = (a, b, operator = "===") => {
|
|
1013
|
+
switch (operator) {
|
|
1014
|
+
case "==":
|
|
1015
|
+
return a == b;
|
|
1016
|
+
case "===":
|
|
1017
|
+
return a === b;
|
|
1018
|
+
case "!=":
|
|
1019
|
+
return a != b;
|
|
1020
|
+
case "!==":
|
|
1021
|
+
return a !== b;
|
|
1022
|
+
case ">":
|
|
1023
|
+
return a > b;
|
|
1024
|
+
case "<":
|
|
1025
|
+
return a < b;
|
|
1026
|
+
case ">=":
|
|
1027
|
+
return a >= b;
|
|
1028
|
+
case "<=":
|
|
1029
|
+
return a <= b;
|
|
1030
|
+
default:
|
|
1031
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
var checkConditions = (value, checks) => {
|
|
1035
|
+
for (const check of checks) {
|
|
1036
|
+
if (!compare(value, check.value, check.operator)) {
|
|
1037
|
+
return false;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
return true;
|
|
1041
|
+
};
|
|
1042
|
+
var validateBooleanConstraint = (value, constraint) => {
|
|
1043
|
+
switch (constraint) {
|
|
1044
|
+
case "required":
|
|
1045
|
+
return value === true;
|
|
1046
|
+
case "optional":
|
|
1047
|
+
return value === true || value === false;
|
|
1048
|
+
case "true":
|
|
1049
|
+
return value === true;
|
|
1050
|
+
case "false":
|
|
1051
|
+
return value === false;
|
|
1052
|
+
case "truthy":
|
|
1053
|
+
return Boolean(value);
|
|
1054
|
+
case "falsy":
|
|
1055
|
+
return !Boolean(value);
|
|
1056
|
+
default:
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
var randomBoolean = (probability = 0.5) => {
|
|
1061
|
+
if (probability < 0 || probability > 1) {
|
|
1062
|
+
throw new Error("Probability must be between 0 and 1");
|
|
1063
|
+
}
|
|
1064
|
+
if (probability === 0) return false;
|
|
1065
|
+
return Math.random() <= probability;
|
|
1066
|
+
};
|
|
1067
|
+
var debounceBoolean = (fn, delay) => {
|
|
1068
|
+
let timeoutId;
|
|
1069
|
+
let lastResult;
|
|
1070
|
+
return (...args) => {
|
|
1071
|
+
if (timeoutId) {
|
|
1072
|
+
clearTimeout(timeoutId);
|
|
1073
|
+
}
|
|
1074
|
+
timeoutId = setTimeout(() => {
|
|
1075
|
+
lastResult = fn(...args);
|
|
1076
|
+
}, delay);
|
|
1077
|
+
return lastResult;
|
|
1078
|
+
};
|
|
1079
|
+
};
|
|
1080
|
+
var memoizeBoolean = (fn) => {
|
|
1081
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
1082
|
+
return ((...args) => {
|
|
1083
|
+
let keyObj = null;
|
|
1084
|
+
let keyString = "";
|
|
1085
|
+
for (let i = 0; i < args.length; i++) {
|
|
1086
|
+
if (args[i] !== null && typeof args[i] === "object") {
|
|
1087
|
+
keyObj = args[i];
|
|
1088
|
+
break;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
keyString = JSON.stringify(args);
|
|
1092
|
+
if (keyObj) {
|
|
1093
|
+
let objCache = cache.get(keyObj);
|
|
1094
|
+
if (!objCache) {
|
|
1095
|
+
objCache = /* @__PURE__ */ new Map();
|
|
1096
|
+
cache.set(keyObj, objCache);
|
|
1097
|
+
}
|
|
1098
|
+
if (objCache.has(keyString)) {
|
|
1099
|
+
return objCache.get(keyString);
|
|
1100
|
+
}
|
|
1101
|
+
const result = fn(...args);
|
|
1102
|
+
objCache.set(keyString, result);
|
|
1103
|
+
return result;
|
|
1104
|
+
} else {
|
|
1105
|
+
const stringCache = cache.get(Object.prototype) || /* @__PURE__ */ new Map();
|
|
1106
|
+
if (!cache.has(Object.prototype)) {
|
|
1107
|
+
cache.set(Object.prototype, stringCache);
|
|
1108
|
+
}
|
|
1109
|
+
if (stringCache.has(keyString)) {
|
|
1110
|
+
return stringCache.get(keyString);
|
|
1111
|
+
}
|
|
1112
|
+
const result = fn(...args);
|
|
1113
|
+
stringCache.set(keyString, result);
|
|
1114
|
+
return result;
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
};
|
|
1118
|
+
var createPipeline = (functions) => {
|
|
1119
|
+
return (value) => {
|
|
1120
|
+
for (const fn of functions) {
|
|
1121
|
+
if (!fn(value)) {
|
|
1122
|
+
return false;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
return true;
|
|
1126
|
+
};
|
|
1127
|
+
};
|
|
1128
|
+
var isBooleanArray = (value) => {
|
|
1129
|
+
return Array.isArray(value) && value.every((item) => item === true || item === false);
|
|
1130
|
+
};
|
|
1131
|
+
var isBooleanLikeArray = (value) => {
|
|
1132
|
+
return Array.isArray(value) && value.every((item) => isBooleanLike(item));
|
|
1133
|
+
};
|
|
1134
|
+
var compareBoolean = (value, newValue) => {
|
|
1135
|
+
const o = toBoolean(value);
|
|
1136
|
+
const n = toBoolean(newValue);
|
|
1137
|
+
return o !== n;
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
// src/lib/primitive/date/date.ts
|
|
1141
|
+
var MILLISECONDS_IN_SECOND = 1e3;
|
|
1142
|
+
var SECONDS_IN_MINUTE = 60;
|
|
1143
|
+
var MINUTES_IN_HOUR = 60;
|
|
1144
|
+
var HOURS_IN_DAY = 24;
|
|
1145
|
+
var DAYS_IN_WEEK = 7;
|
|
1146
|
+
var WEEKDAY_NAMES = [
|
|
1147
|
+
"Sunday",
|
|
1148
|
+
"Monday",
|
|
1149
|
+
"Tuesday",
|
|
1150
|
+
"Wednesday",
|
|
1151
|
+
"Thursday",
|
|
1152
|
+
"Friday",
|
|
1153
|
+
"Saturday"
|
|
1154
|
+
];
|
|
1155
|
+
var WEEKDAY_NAMES_SHORT = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
1156
|
+
var MONTH_NAMES = [
|
|
1157
|
+
"January",
|
|
1158
|
+
"February",
|
|
1159
|
+
"March",
|
|
1160
|
+
"April",
|
|
1161
|
+
"May",
|
|
1162
|
+
"June",
|
|
1163
|
+
"July",
|
|
1164
|
+
"August",
|
|
1165
|
+
"September",
|
|
1166
|
+
"October",
|
|
1167
|
+
"November",
|
|
1168
|
+
"December"
|
|
1169
|
+
];
|
|
1170
|
+
var MONTH_NAMES_SHORT = [
|
|
1171
|
+
"Jan",
|
|
1172
|
+
"Feb",
|
|
1173
|
+
"Mar",
|
|
1174
|
+
"Apr",
|
|
1175
|
+
"May",
|
|
1176
|
+
"Jun",
|
|
1177
|
+
"Jul",
|
|
1178
|
+
"Aug",
|
|
1179
|
+
"Sep",
|
|
1180
|
+
"Oct",
|
|
1181
|
+
"Nov",
|
|
1182
|
+
"Dec"
|
|
1183
|
+
];
|
|
1184
|
+
var DEFAULT_CONFIG = {
|
|
1185
|
+
defaultTimezone: "UTC",
|
|
1186
|
+
defaultLocale: "en-US",
|
|
1187
|
+
weekStartsOn: 0,
|
|
1188
|
+
// Sunday
|
|
1189
|
+
businessDays: {
|
|
1190
|
+
businessDays: [1, 2, 3, 4, 5],
|
|
1191
|
+
// Monday to Friday
|
|
1192
|
+
holidays: [],
|
|
1193
|
+
holidayCheck: () => false,
|
|
1194
|
+
workStartHour: 9,
|
|
1195
|
+
workEndHour: 17
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
var config = { ...DEFAULT_CONFIG };
|
|
1199
|
+
function configureDateUtils(newConfig) {
|
|
1200
|
+
config = {
|
|
1201
|
+
...config,
|
|
1202
|
+
...newConfig,
|
|
1203
|
+
businessDays: {
|
|
1204
|
+
...config.businessDays,
|
|
1205
|
+
...newConfig.businessDays || {}
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
function resetDateConfig() {
|
|
1210
|
+
config = { ...DEFAULT_CONFIG };
|
|
1211
|
+
}
|
|
1212
|
+
function parseDate(input, options = {}) {
|
|
1213
|
+
const {
|
|
1214
|
+
format = "ISO",
|
|
1215
|
+
customFormat = "",
|
|
1216
|
+
timezone = config.defaultTimezone,
|
|
1217
|
+
strict = true,
|
|
1218
|
+
locale = config.defaultLocale
|
|
1219
|
+
} = options;
|
|
1220
|
+
if (input instanceof Date && isDate(input)) {
|
|
1221
|
+
return input;
|
|
1222
|
+
}
|
|
1223
|
+
let parsedDate = null;
|
|
1224
|
+
if (typeof input === "number") {
|
|
1225
|
+
parsedDate = new Date(input);
|
|
1226
|
+
if (!isDate(parsedDate)) {
|
|
1227
|
+
throw new Error(`Invalid timestamp: ${input}`);
|
|
1228
|
+
}
|
|
1229
|
+
return applyTimezone(parsedDate, timezone);
|
|
1230
|
+
}
|
|
1231
|
+
if (typeof input !== "string") {
|
|
1232
|
+
throw new Error(`Invalid input type: ${typeof input}`);
|
|
1233
|
+
}
|
|
1234
|
+
const trimmedInput = input.trim();
|
|
1235
|
+
if (!trimmedInput) {
|
|
1236
|
+
throw new Error("Empty date string");
|
|
1237
|
+
}
|
|
1238
|
+
try {
|
|
1239
|
+
switch (format) {
|
|
1240
|
+
case "ISO":
|
|
1241
|
+
case "ISO_DATE":
|
|
1242
|
+
case "ISO_TIME":
|
|
1243
|
+
parsedDate = parseISODate(trimmedInput, format);
|
|
1244
|
+
break;
|
|
1245
|
+
case "US":
|
|
1246
|
+
parsedDate = parseUSDate(trimmedInput);
|
|
1247
|
+
break;
|
|
1248
|
+
case "EU":
|
|
1249
|
+
parsedDate = parseEUDate(trimmedInput);
|
|
1250
|
+
break;
|
|
1251
|
+
case "CUSTOM":
|
|
1252
|
+
parsedDate = parseCustomDate(trimmedInput, customFormat);
|
|
1253
|
+
break;
|
|
1254
|
+
case "RFC_2822":
|
|
1255
|
+
parsedDate = parseRFC2822Date(trimmedInput);
|
|
1256
|
+
break;
|
|
1257
|
+
case "TIMESTAMP":
|
|
1258
|
+
parsedDate = parseTimestamp(trimmedInput);
|
|
1259
|
+
break;
|
|
1260
|
+
default:
|
|
1261
|
+
parsedDate = parseLocaleDate(trimmedInput, locale, strict);
|
|
1262
|
+
}
|
|
1263
|
+
if (!parsedDate || !isDate(parsedDate)) {
|
|
1264
|
+
if (strict) {
|
|
1265
|
+
throw new Error(
|
|
1266
|
+
`Parsing resulted in invalid date from input: "${trimmedInput}"`
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
parsedDate = new Date(trimmedInput);
|
|
1270
|
+
if (!isDate(parsedDate)) {
|
|
1271
|
+
throw new Error(`Invalid date format: ${trimmedInput}`);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
if (timezone && timezone !== "UTC") {
|
|
1275
|
+
parsedDate = convertTimezone(parsedDate, timezone);
|
|
1276
|
+
}
|
|
1277
|
+
if (!parsedDate) {
|
|
1278
|
+
throw new Error(`Invalid date format: ${trimmedInput}`);
|
|
1279
|
+
}
|
|
1280
|
+
return parsedDate;
|
|
1281
|
+
} catch (error) {
|
|
1282
|
+
if (strict) {
|
|
1283
|
+
throw new Error(
|
|
1284
|
+
`Failed to parse date "${trimmedInput}" with format ${format}: ${error instanceof Error ? error.message : String(error)}`
|
|
1285
|
+
);
|
|
1286
|
+
}
|
|
1287
|
+
const fallbackDate = new Date(trimmedInput);
|
|
1288
|
+
if (isDate(fallbackDate)) {
|
|
1289
|
+
const t = applyTimezone(fallbackDate, timezone);
|
|
1290
|
+
if (!t) {
|
|
1291
|
+
throw new Error(`Invalid date format: ${trimmedInput}`);
|
|
1292
|
+
}
|
|
1293
|
+
return t;
|
|
1294
|
+
}
|
|
1295
|
+
throw new Error(`Invalid date format: ${trimmedInput}`);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
function parseISODate(input, format) {
|
|
1299
|
+
try {
|
|
1300
|
+
if (format === "ISO_DATE") {
|
|
1301
|
+
const parts = input.split("-").map((part) => parseInt(part, 10));
|
|
1302
|
+
if (parts.length < 3 || parts.some(isNaN)) {
|
|
1303
|
+
throw new Error("Invalid ISO date format");
|
|
1304
|
+
}
|
|
1305
|
+
const [year, month, day] = parts;
|
|
1306
|
+
return new Date(Date.UTC(year, month - 1, day));
|
|
1307
|
+
}
|
|
1308
|
+
if (format === "ISO_TIME") {
|
|
1309
|
+
const today2 = /* @__PURE__ */ new Date();
|
|
1310
|
+
const datePart = today2.toISOString().split("T")[0];
|
|
1311
|
+
return /* @__PURE__ */ new Date(`${datePart}T${input}Z`);
|
|
1312
|
+
}
|
|
1313
|
+
const date = new Date(input);
|
|
1314
|
+
if (!isDate(date)) {
|
|
1315
|
+
throw new Error("Invalid ISO date");
|
|
1316
|
+
}
|
|
1317
|
+
return date;
|
|
1318
|
+
} catch {
|
|
1319
|
+
throw new Error(`Invalid ISO ${format} format: ${input}`);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
function parseUSDate(input) {
|
|
1323
|
+
try {
|
|
1324
|
+
const parts = input.split("/");
|
|
1325
|
+
if (parts.length !== 3) {
|
|
1326
|
+
throw new Error("Invalid US date format - expected MM/DD/YYYY");
|
|
1327
|
+
}
|
|
1328
|
+
let month = parseInt(parts[0], 10) - 1;
|
|
1329
|
+
let day = parseInt(parts[1], 10);
|
|
1330
|
+
let year = parseInt(parts[2], 10);
|
|
1331
|
+
if (isNaN(month) || isNaN(day) || isNaN(year)) {
|
|
1332
|
+
throw new Error("Invalid US date format - non-numeric parts");
|
|
1333
|
+
}
|
|
1334
|
+
if (year < 100) {
|
|
1335
|
+
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
|
|
1336
|
+
const century = Math.floor(currentYear / 100) * 100;
|
|
1337
|
+
year += year < 50 ? century : century - 100;
|
|
1338
|
+
}
|
|
1339
|
+
if (month < 0 || month > 11) {
|
|
1340
|
+
throw new Error(`Month must be between 1 and 12, got ${month + 1}`);
|
|
1341
|
+
}
|
|
1342
|
+
const date = new Date(year, month, day);
|
|
1343
|
+
if (!isDate(date)) {
|
|
1344
|
+
throw new Error("Invalid date");
|
|
1345
|
+
}
|
|
1346
|
+
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
|
|
1347
|
+
throw new Error("Invalid date");
|
|
1348
|
+
}
|
|
1349
|
+
return date;
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
throw new Error(
|
|
1352
|
+
`Invalid US date format: ${input} - ${error instanceof Error ? error.message : String(error)}`
|
|
1353
|
+
);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
function parseEUDate(input) {
|
|
1357
|
+
try {
|
|
1358
|
+
const parts = input.split("/");
|
|
1359
|
+
if (parts.length !== 3) {
|
|
1360
|
+
throw new Error("Invalid EU date format - expected DD/MM/YYYY");
|
|
1361
|
+
}
|
|
1362
|
+
let day = parseInt(parts[0], 10);
|
|
1363
|
+
let month = parseInt(parts[1], 10) - 1;
|
|
1364
|
+
let year = parseInt(parts[2], 10);
|
|
1365
|
+
if (isNaN(day) || isNaN(month) || isNaN(year)) {
|
|
1366
|
+
throw new Error("Invalid EU date format - non-numeric parts");
|
|
1367
|
+
}
|
|
1368
|
+
if (year < 100) {
|
|
1369
|
+
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
|
|
1370
|
+
const century = Math.floor(currentYear / 100) * 100;
|
|
1371
|
+
year += year < 50 ? century : century - 100;
|
|
1372
|
+
}
|
|
1373
|
+
if (month < 0 || month > 11) {
|
|
1374
|
+
throw new Error(`Month must be between 1 and 12, got ${month + 1}`);
|
|
1375
|
+
}
|
|
1376
|
+
const date = new Date(year, month, day);
|
|
1377
|
+
if (!isDate(date)) {
|
|
1378
|
+
throw new Error("Invalid date");
|
|
1379
|
+
}
|
|
1380
|
+
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
|
|
1381
|
+
throw new Error("Invalid date");
|
|
1382
|
+
}
|
|
1383
|
+
return date;
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
throw new Error(
|
|
1386
|
+
`Invalid EU date format: ${input} - ${error instanceof Error ? error.message : String(error)}`
|
|
1387
|
+
);
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
function parseCustomDate(input, format) {
|
|
1391
|
+
try {
|
|
1392
|
+
if (!format) {
|
|
1393
|
+
throw new Error("Custom format string is required");
|
|
1394
|
+
}
|
|
1395
|
+
const formatPattern = format.replace(/YYYY/g, "(\\d{4})").replace(/YY/g, "(\\d{2})").replace(/MM/g, "(\\d{2})").replace(/M/g, "(\\d{1,2})").replace(/DD/g, "(\\d{2})").replace(/D/g, "(\\d{1,2})").replace(/HH/g, "(\\d{2})").replace(/H/g, "(\\d{1,2})").replace(/hh/g, "(\\d{2})").replace(/h/g, "(\\d{1,2})").replace(/mm/g, "(\\d{2})").replace(/m/g, "(\\d{1,2})").replace(/ss/g, "(\\d{2})").replace(/s/g, "(\\d{1,2})");
|
|
1396
|
+
const regex = new RegExp(`^${formatPattern}$`);
|
|
1397
|
+
const match = input.match(regex);
|
|
1398
|
+
if (!match) {
|
|
1399
|
+
throw new Error(`Input does not match custom format: ${format}`);
|
|
1400
|
+
}
|
|
1401
|
+
let year = 0, month = 0, day = 1, hour = 0, minute = 0, second = 0;
|
|
1402
|
+
let matchIndex = 1;
|
|
1403
|
+
const formatTokens = format.match(/YYYY|YY|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s/g) || [];
|
|
1404
|
+
for (const token of formatTokens) {
|
|
1405
|
+
const value = parseInt(match[matchIndex], 10);
|
|
1406
|
+
if (isNaN(value)) {
|
|
1407
|
+
throw new Error(`Invalid numeric value for token ${token}`);
|
|
1408
|
+
}
|
|
1409
|
+
switch (token) {
|
|
1410
|
+
case "YYYY":
|
|
1411
|
+
year = value;
|
|
1412
|
+
break;
|
|
1413
|
+
case "YY":
|
|
1414
|
+
year = value;
|
|
1415
|
+
if (year < 100) {
|
|
1416
|
+
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
|
|
1417
|
+
const century = Math.floor(currentYear / 100) * 100;
|
|
1418
|
+
year += year < 50 ? century : century - 100;
|
|
1419
|
+
}
|
|
1420
|
+
break;
|
|
1421
|
+
case "MM":
|
|
1422
|
+
case "M":
|
|
1423
|
+
month = value - 1;
|
|
1424
|
+
break;
|
|
1425
|
+
case "DD":
|
|
1426
|
+
case "D":
|
|
1427
|
+
day = value;
|
|
1428
|
+
break;
|
|
1429
|
+
case "HH":
|
|
1430
|
+
case "H":
|
|
1431
|
+
hour = value;
|
|
1432
|
+
break;
|
|
1433
|
+
case "hh":
|
|
1434
|
+
case "h":
|
|
1435
|
+
hour = value;
|
|
1436
|
+
break;
|
|
1437
|
+
case "mm":
|
|
1438
|
+
case "m":
|
|
1439
|
+
minute = value;
|
|
1440
|
+
break;
|
|
1441
|
+
case "ss":
|
|
1442
|
+
case "s":
|
|
1443
|
+
second = value;
|
|
1444
|
+
break;
|
|
1445
|
+
}
|
|
1446
|
+
matchIndex++;
|
|
1447
|
+
}
|
|
1448
|
+
if (year < 1e3 || year > 9999) {
|
|
1449
|
+
throw new Error(`Year must be between 1000 and 9999: ${year}`);
|
|
1450
|
+
}
|
|
1451
|
+
if (month < 0 || month > 11) {
|
|
1452
|
+
throw new Error(`Month must be between 1 and 12: ${month + 1}`);
|
|
1453
|
+
}
|
|
1454
|
+
const date = new Date(year, month, day, hour, minute, second);
|
|
1455
|
+
if (!isDate(date)) {
|
|
1456
|
+
throw new Error("Invalid date");
|
|
1457
|
+
}
|
|
1458
|
+
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
|
|
1459
|
+
throw new Error("Invalid date");
|
|
1460
|
+
}
|
|
1461
|
+
return date;
|
|
1462
|
+
} catch (error) {
|
|
1463
|
+
throw new Error(
|
|
1464
|
+
`Failed to parse with custom format "${format}": ${error instanceof Error ? error.message : String(error)}`
|
|
1465
|
+
);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
function parseRFC2822Date(input) {
|
|
1469
|
+
try {
|
|
1470
|
+
const date = new Date(input);
|
|
1471
|
+
if (!isDate(date)) {
|
|
1472
|
+
throw new Error("Invalid RFC 2822 date");
|
|
1473
|
+
}
|
|
1474
|
+
return date;
|
|
1475
|
+
} catch {
|
|
1476
|
+
throw new Error(`Invalid RFC 2822 date format: ${input}`);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
function parseTimestamp(input) {
|
|
1480
|
+
try {
|
|
1481
|
+
if (!/^\d{8}(\d{6})?$/.test(input)) {
|
|
1482
|
+
throw new Error(
|
|
1483
|
+
"Invalid timestamp format - expected YYYYMMDD or YYYYMMDDHHMMSS"
|
|
1484
|
+
);
|
|
1485
|
+
}
|
|
1486
|
+
const year = parseInt(input.substring(0, 4), 10);
|
|
1487
|
+
const month = parseInt(input.substring(4, 6), 10) - 1;
|
|
1488
|
+
const day = parseInt(input.substring(6, 8), 10);
|
|
1489
|
+
let hour = 0, minute = 0, second = 0;
|
|
1490
|
+
if (input.length === 14) {
|
|
1491
|
+
hour = parseInt(input.substring(8, 10), 10);
|
|
1492
|
+
minute = parseInt(input.substring(10, 12), 10);
|
|
1493
|
+
second = parseInt(input.substring(12, 14), 10);
|
|
1494
|
+
}
|
|
1495
|
+
if (month < 0 || month > 11) {
|
|
1496
|
+
throw new Error(`Month must be between 1 and 12: ${month + 1}`);
|
|
1497
|
+
}
|
|
1498
|
+
const date = new Date(year, month, day, hour, minute, second);
|
|
1499
|
+
if (!isDate(date)) {
|
|
1500
|
+
throw new Error("Invalid timestamp");
|
|
1501
|
+
}
|
|
1502
|
+
return date;
|
|
1503
|
+
} catch (error) {
|
|
1504
|
+
throw new Error(
|
|
1505
|
+
`Invalid timestamp format: ${input} - ${error instanceof Error ? error.message : String(error)}`
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
function parseLocaleDate(input, locale, strict) {
|
|
1510
|
+
try {
|
|
1511
|
+
const date = new Date(input);
|
|
1512
|
+
if (isDate(date)) {
|
|
1513
|
+
return date;
|
|
1514
|
+
}
|
|
1515
|
+
const formatter = new Intl.DateTimeFormat(locale, {
|
|
1516
|
+
year: "numeric",
|
|
1517
|
+
month: "numeric",
|
|
1518
|
+
day: "numeric",
|
|
1519
|
+
hour: "numeric",
|
|
1520
|
+
minute: "numeric",
|
|
1521
|
+
second: "numeric"
|
|
1522
|
+
});
|
|
1523
|
+
const fallback = new Date(input);
|
|
1524
|
+
if (isDate(fallback)) {
|
|
1525
|
+
return fallback;
|
|
1526
|
+
}
|
|
1527
|
+
if (strict) {
|
|
1528
|
+
throw new Error(`Could not parse date with locale ${locale}`);
|
|
1529
|
+
}
|
|
1530
|
+
return /* @__PURE__ */ new Date(NaN);
|
|
1531
|
+
} catch (error) {
|
|
1532
|
+
if (strict) {
|
|
1533
|
+
throw new Error(
|
|
1534
|
+
`Failed to parse locale date: ${error instanceof Error ? error.message : String(error)}`
|
|
1535
|
+
);
|
|
1536
|
+
}
|
|
1537
|
+
return /* @__PURE__ */ new Date(NaN);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
function applyTimezone(date, timezone) {
|
|
1541
|
+
if (!timezone || timezone === "UTC") {
|
|
1542
|
+
return date;
|
|
1543
|
+
}
|
|
1544
|
+
return date;
|
|
1545
|
+
}
|
|
1546
|
+
function isDate(date) {
|
|
1547
|
+
if (!date || typeof date !== "object") {
|
|
1548
|
+
return false;
|
|
1549
|
+
}
|
|
1550
|
+
const d = date;
|
|
1551
|
+
if (typeof d.getTime !== "function" || typeof d.getFullYear !== "function") {
|
|
1552
|
+
return false;
|
|
1553
|
+
}
|
|
1554
|
+
const time = d.getTime();
|
|
1555
|
+
return !isNaN(time) && typeof time === "number" && isFinite(time);
|
|
1556
|
+
}
|
|
1557
|
+
function formatDate(date, options = {}) {
|
|
1558
|
+
const parsedDate = parseDate(date);
|
|
1559
|
+
const {
|
|
1560
|
+
format = "ISO",
|
|
1561
|
+
customFormat = "",
|
|
1562
|
+
timezone = config.defaultTimezone,
|
|
1563
|
+
locale = config.defaultLocale,
|
|
1564
|
+
includeTime = true,
|
|
1565
|
+
includeSeconds = false,
|
|
1566
|
+
includeMilliseconds = false
|
|
1567
|
+
} = options;
|
|
1568
|
+
let targetDate = parsedDate;
|
|
1569
|
+
if (timezone && timezone !== "UTC") {
|
|
1570
|
+
targetDate = convertTimezone(parsedDate, timezone);
|
|
1571
|
+
}
|
|
1572
|
+
switch (format) {
|
|
1573
|
+
case "ISO":
|
|
1574
|
+
return targetDate.toISOString();
|
|
1575
|
+
case "ISO_DATE":
|
|
1576
|
+
return targetDate.toISOString().split("T")[0];
|
|
1577
|
+
case "ISO_TIME": {
|
|
1578
|
+
const timePart = targetDate.toISOString().split("T")[1];
|
|
1579
|
+
return includeMilliseconds ? timePart : timePart.split(".")[0];
|
|
1580
|
+
}
|
|
1581
|
+
case "US":
|
|
1582
|
+
return formatUSDate(targetDate);
|
|
1583
|
+
case "EU":
|
|
1584
|
+
return formatEUDate(targetDate);
|
|
1585
|
+
case "FULL":
|
|
1586
|
+
return formatFullDate(targetDate, locale);
|
|
1587
|
+
case "LONG":
|
|
1588
|
+
return formatLongDate(targetDate, locale);
|
|
1589
|
+
case "MEDIUM":
|
|
1590
|
+
return formatMediumDate(targetDate, locale);
|
|
1591
|
+
case "SHORT":
|
|
1592
|
+
return formatShortDate(targetDate, locale);
|
|
1593
|
+
case "TIME":
|
|
1594
|
+
return formatTime(targetDate, locale, includeSeconds);
|
|
1595
|
+
case "TIME_24":
|
|
1596
|
+
return formatTime24(targetDate, includeSeconds);
|
|
1597
|
+
case "DATETIME":
|
|
1598
|
+
return `${formatMediumDate(targetDate, locale)}, ${formatTime(
|
|
1599
|
+
targetDate,
|
|
1600
|
+
locale,
|
|
1601
|
+
includeSeconds
|
|
1602
|
+
)}`;
|
|
1603
|
+
case "TIMESTAMP":
|
|
1604
|
+
return formatTimestamp(targetDate);
|
|
1605
|
+
case "RFC_2822":
|
|
1606
|
+
return formatRFC2822(targetDate);
|
|
1607
|
+
case "CUSTOM":
|
|
1608
|
+
return formatCustomDate(targetDate, customFormat);
|
|
1609
|
+
default:
|
|
1610
|
+
return targetDate.toISOString();
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
function now() {
|
|
1614
|
+
return /* @__PURE__ */ new Date();
|
|
1615
|
+
}
|
|
1616
|
+
function today() {
|
|
1617
|
+
return startOfDay(/* @__PURE__ */ new Date());
|
|
1618
|
+
}
|
|
1619
|
+
function tomorrow() {
|
|
1620
|
+
return addDays(today(), 1);
|
|
1621
|
+
}
|
|
1622
|
+
function yesterday() {
|
|
1623
|
+
return subtractDays(today(), 1);
|
|
1624
|
+
}
|
|
1625
|
+
function createDate(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0) {
|
|
1626
|
+
validateDateParts(year, month, day, hour, minute, second, millisecond);
|
|
1627
|
+
return new Date(year, month, day, hour, minute, second, millisecond);
|
|
1628
|
+
}
|
|
1629
|
+
function add(date, amount, unit = "day") {
|
|
1630
|
+
const parsedDate = parseDate(date);
|
|
1631
|
+
const result = new Date(parsedDate);
|
|
1632
|
+
switch (unit) {
|
|
1633
|
+
case "millisecond":
|
|
1634
|
+
result.setMilliseconds(result.getMilliseconds() + amount);
|
|
1635
|
+
break;
|
|
1636
|
+
case "second":
|
|
1637
|
+
result.setSeconds(result.getSeconds() + amount);
|
|
1638
|
+
break;
|
|
1639
|
+
case "minute":
|
|
1640
|
+
result.setMinutes(result.getMinutes() + amount);
|
|
1641
|
+
break;
|
|
1642
|
+
case "hour":
|
|
1643
|
+
result.setHours(result.getHours() + amount);
|
|
1644
|
+
break;
|
|
1645
|
+
case "day":
|
|
1646
|
+
result.setDate(result.getDate() + amount);
|
|
1647
|
+
break;
|
|
1648
|
+
case "week":
|
|
1649
|
+
result.setDate(result.getDate() + amount * DAYS_IN_WEEK);
|
|
1650
|
+
break;
|
|
1651
|
+
case "month":
|
|
1652
|
+
result.setMonth(result.getMonth() + amount);
|
|
1653
|
+
break;
|
|
1654
|
+
case "quarter":
|
|
1655
|
+
result.setMonth(result.getMonth() + amount * 3);
|
|
1656
|
+
break;
|
|
1657
|
+
case "year":
|
|
1658
|
+
result.setFullYear(result.getFullYear() + amount);
|
|
1659
|
+
break;
|
|
1660
|
+
default:
|
|
1661
|
+
throw new Error(`Unsupported date unit: ${unit}`);
|
|
1662
|
+
}
|
|
1663
|
+
return result;
|
|
1664
|
+
}
|
|
1665
|
+
function subtract(date, amount, unit = "day") {
|
|
1666
|
+
return add(date, -amount, unit);
|
|
1667
|
+
}
|
|
1668
|
+
function addDays(date, days) {
|
|
1669
|
+
return add(date, days, "day");
|
|
1670
|
+
}
|
|
1671
|
+
function subtractDays(date, days) {
|
|
1672
|
+
return subtract(date, days, "day");
|
|
1673
|
+
}
|
|
1674
|
+
function addMonths(date, months) {
|
|
1675
|
+
return add(date, months, "month");
|
|
1676
|
+
}
|
|
1677
|
+
function subtractMonths(date, months) {
|
|
1678
|
+
return subtract(date, months, "month");
|
|
1679
|
+
}
|
|
1680
|
+
function addYears(date, years) {
|
|
1681
|
+
return add(date, years, "year");
|
|
1682
|
+
}
|
|
1683
|
+
function subtractYears(date, years) {
|
|
1684
|
+
return subtract(date, years, "year");
|
|
1685
|
+
}
|
|
1686
|
+
function startOf(date, unit) {
|
|
1687
|
+
const parsedDate = parseDate(date);
|
|
1688
|
+
const result = new Date(parsedDate);
|
|
1689
|
+
switch (unit) {
|
|
1690
|
+
case "year":
|
|
1691
|
+
result.setMonth(0, 1);
|
|
1692
|
+
result.setHours(0, 0, 0, 0);
|
|
1693
|
+
break;
|
|
1694
|
+
case "month":
|
|
1695
|
+
result.setDate(1);
|
|
1696
|
+
result.setHours(0, 0, 0, 0);
|
|
1697
|
+
break;
|
|
1698
|
+
case "day":
|
|
1699
|
+
result.setHours(0, 0, 0, 0);
|
|
1700
|
+
break;
|
|
1701
|
+
case "hour":
|
|
1702
|
+
result.setMinutes(0, 0, 0);
|
|
1703
|
+
break;
|
|
1704
|
+
case "minute":
|
|
1705
|
+
result.setSeconds(0, 0);
|
|
1706
|
+
break;
|
|
1707
|
+
case "second":
|
|
1708
|
+
result.setMilliseconds(0);
|
|
1709
|
+
break;
|
|
1710
|
+
case "week": {
|
|
1711
|
+
const dayOfWeek = result.getDay();
|
|
1712
|
+
const diff = (dayOfWeek - config.weekStartsOn + DAYS_IN_WEEK) % DAYS_IN_WEEK;
|
|
1713
|
+
result.setDate(result.getDate() - diff);
|
|
1714
|
+
result.setHours(0, 0, 0, 0);
|
|
1715
|
+
break;
|
|
1716
|
+
}
|
|
1717
|
+
case "quarter": {
|
|
1718
|
+
const quarter = Math.floor(result.getMonth() / 3);
|
|
1719
|
+
result.setMonth(quarter * 3, 1);
|
|
1720
|
+
result.setHours(0, 0, 0, 0);
|
|
1721
|
+
break;
|
|
1722
|
+
}
|
|
1723
|
+
default:
|
|
1724
|
+
throw new Error(`Unsupported unit for startOf: ${unit}`);
|
|
1725
|
+
}
|
|
1726
|
+
return result;
|
|
1727
|
+
}
|
|
1728
|
+
function endOf(date, unit) {
|
|
1729
|
+
const start = startOf(date, unit);
|
|
1730
|
+
const result = new Date(start);
|
|
1731
|
+
switch (unit) {
|
|
1732
|
+
case "year":
|
|
1733
|
+
result.setFullYear(result.getFullYear() + 1);
|
|
1734
|
+
break;
|
|
1735
|
+
case "quarter":
|
|
1736
|
+
result.setMonth(result.getMonth() + 3);
|
|
1737
|
+
break;
|
|
1738
|
+
case "month":
|
|
1739
|
+
result.setMonth(result.getMonth() + 1);
|
|
1740
|
+
break;
|
|
1741
|
+
case "week":
|
|
1742
|
+
result.setDate(result.getDate() + DAYS_IN_WEEK);
|
|
1743
|
+
break;
|
|
1744
|
+
case "day":
|
|
1745
|
+
result.setDate(result.getDate() + 1);
|
|
1746
|
+
break;
|
|
1747
|
+
case "hour":
|
|
1748
|
+
result.setHours(result.getHours() + 1);
|
|
1749
|
+
break;
|
|
1750
|
+
case "minute":
|
|
1751
|
+
result.setMinutes(result.getMinutes() + 1);
|
|
1752
|
+
break;
|
|
1753
|
+
case "second":
|
|
1754
|
+
result.setSeconds(result.getSeconds() + 1);
|
|
1755
|
+
break;
|
|
1756
|
+
default:
|
|
1757
|
+
throw new Error(`Unsupported unit for endOf: ${unit}`);
|
|
1758
|
+
}
|
|
1759
|
+
result.setMilliseconds(result.getMilliseconds() - 1);
|
|
1760
|
+
return result;
|
|
1761
|
+
}
|
|
1762
|
+
function startOfDay(date) {
|
|
1763
|
+
return startOf(date, "day");
|
|
1764
|
+
}
|
|
1765
|
+
function endOfDay(date) {
|
|
1766
|
+
return endOf(date, "day");
|
|
1767
|
+
}
|
|
1768
|
+
function startOfMonth(date) {
|
|
1769
|
+
return startOf(date, "month");
|
|
1770
|
+
}
|
|
1771
|
+
function endOfMonth(date) {
|
|
1772
|
+
return endOf(date, "month");
|
|
1773
|
+
}
|
|
1774
|
+
function startOfYear(date) {
|
|
1775
|
+
return startOf(date, "year");
|
|
1776
|
+
}
|
|
1777
|
+
function endOfYear(date) {
|
|
1778
|
+
return endOf(date, "year");
|
|
1779
|
+
}
|
|
1780
|
+
function startOfWeek(date) {
|
|
1781
|
+
return startOf(date, "week");
|
|
1782
|
+
}
|
|
1783
|
+
function endOfWeek(date) {
|
|
1784
|
+
return endOf(date, "week");
|
|
1785
|
+
}
|
|
1786
|
+
function isBetween(date, start, end, inclusive = true) {
|
|
1787
|
+
const parsedDate = parseDate(date);
|
|
1788
|
+
const parsedStart = parseDate(start);
|
|
1789
|
+
const parsedEnd = parseDate(end);
|
|
1790
|
+
if (inclusive) {
|
|
1791
|
+
return parsedDate >= parsedStart && parsedDate <= parsedEnd;
|
|
1792
|
+
}
|
|
1793
|
+
return parsedDate > parsedStart && parsedDate < parsedEnd;
|
|
1794
|
+
}
|
|
1795
|
+
function isBefore(date, compareTo) {
|
|
1796
|
+
return parseDate(date) < parseDate(compareTo);
|
|
1797
|
+
}
|
|
1798
|
+
function isAfter(date, compareTo) {
|
|
1799
|
+
return parseDate(date) > parseDate(compareTo);
|
|
1800
|
+
}
|
|
1801
|
+
function isSame(date1, date2, unit) {
|
|
1802
|
+
const d1 = parseDate(date1);
|
|
1803
|
+
const d2 = parseDate(date2);
|
|
1804
|
+
if (!unit) {
|
|
1805
|
+
return d1.getTime() === d2.getTime();
|
|
1806
|
+
}
|
|
1807
|
+
const start1 = startOf(d1, unit);
|
|
1808
|
+
const start2 = startOf(d2, unit);
|
|
1809
|
+
return start1.getTime() === start2.getTime();
|
|
1810
|
+
}
|
|
1811
|
+
function isToday(date) {
|
|
1812
|
+
return isSame(date, today(), "day");
|
|
1813
|
+
}
|
|
1814
|
+
function isPast(date) {
|
|
1815
|
+
return parseDate(date) < now();
|
|
1816
|
+
}
|
|
1817
|
+
function isFuture(date) {
|
|
1818
|
+
return parseDate(date) > now();
|
|
1819
|
+
}
|
|
1820
|
+
function difference(date1, date2) {
|
|
1821
|
+
const d1 = parseDate(date1);
|
|
1822
|
+
const d2 = parseDate(date2);
|
|
1823
|
+
const diffMs = Math.abs(d2.getTime() - d1.getTime());
|
|
1824
|
+
const totalSeconds = Math.floor(diffMs / MILLISECONDS_IN_SECOND);
|
|
1825
|
+
const totalMinutes = Math.floor(totalSeconds / SECONDS_IN_MINUTE);
|
|
1826
|
+
const totalHours = Math.floor(totalMinutes / MINUTES_IN_HOUR);
|
|
1827
|
+
const totalDays = Math.floor(totalHours / HOURS_IN_DAY);
|
|
1828
|
+
return {
|
|
1829
|
+
years: Math.floor(totalDays / 365),
|
|
1830
|
+
months: Math.floor(totalDays / 30),
|
|
1831
|
+
weeks: Math.floor(totalDays / DAYS_IN_WEEK),
|
|
1832
|
+
days: totalDays,
|
|
1833
|
+
hours: totalHours,
|
|
1834
|
+
minutes: totalMinutes,
|
|
1835
|
+
seconds: totalSeconds,
|
|
1836
|
+
milliseconds: diffMs,
|
|
1837
|
+
totalDays,
|
|
1838
|
+
totalHours,
|
|
1839
|
+
totalMinutes,
|
|
1840
|
+
totalSeconds,
|
|
1841
|
+
totalMilliseconds: diffMs
|
|
1842
|
+
};
|
|
1843
|
+
}
|
|
1844
|
+
function humanizeDifference(date1, date2 = now(), detailed = false) {
|
|
1845
|
+
const diff = difference(date1, date2);
|
|
1846
|
+
const isPast2 = parseDate(date1) < parseDate(date2);
|
|
1847
|
+
const prefix = isPast2 ? "ago" : "in";
|
|
1848
|
+
if (diff.years > 0) {
|
|
1849
|
+
return detailed ? `${diff.years} year${diff.years === 1 ? "" : "s"} ${prefix}` : `${diff.years}y ${prefix}`;
|
|
1850
|
+
}
|
|
1851
|
+
if (diff.months > 0) {
|
|
1852
|
+
return detailed ? `${diff.months} month${diff.months === 1 ? "" : "s"} ${prefix}` : `${diff.months}mo ${prefix}`;
|
|
1853
|
+
}
|
|
1854
|
+
if (diff.weeks > 0) {
|
|
1855
|
+
return detailed ? `${diff.weeks} week${diff.weeks === 1 ? "" : "s"} ${prefix}` : `${diff.weeks}w ${prefix}`;
|
|
1856
|
+
}
|
|
1857
|
+
if (diff.days > 0) {
|
|
1858
|
+
return detailed ? `${diff.days} day${diff.days === 1 ? "" : "s"} ${prefix}` : `${diff.days}d ${prefix}`;
|
|
1859
|
+
}
|
|
1860
|
+
if (diff.hours > 0) {
|
|
1861
|
+
return detailed ? `${diff.hours} hour${diff.hours === 1 ? "" : "s"} ${prefix}` : `${diff.hours}h ${prefix}`;
|
|
1862
|
+
}
|
|
1863
|
+
if (diff.minutes > 0) {
|
|
1864
|
+
return detailed ? `${diff.minutes} minute${diff.minutes === 1 ? "" : "s"} ${prefix}` : `${diff.minutes}m ${prefix}`;
|
|
1865
|
+
}
|
|
1866
|
+
return detailed ? `${diff.seconds} second${diff.seconds === 1 ? "" : "s"} ${prefix}` : `${diff.seconds}s ${prefix}`;
|
|
1867
|
+
}
|
|
1868
|
+
function isWeekend(date) {
|
|
1869
|
+
const parsedDate = parseDate(date);
|
|
1870
|
+
const day = parsedDate.getDay();
|
|
1871
|
+
return day === 0 || day === 6;
|
|
1872
|
+
}
|
|
1873
|
+
function isWeekday(date) {
|
|
1874
|
+
return !isWeekend(date);
|
|
1875
|
+
}
|
|
1876
|
+
function isBusinessDay(date) {
|
|
1877
|
+
const parsedDate = parseDate(date);
|
|
1878
|
+
if (isWeekend(parsedDate)) {
|
|
1879
|
+
return false;
|
|
1880
|
+
}
|
|
1881
|
+
if (!config.businessDays.businessDays.includes(parsedDate.getDay())) {
|
|
1882
|
+
return false;
|
|
1883
|
+
}
|
|
1884
|
+
const isHoliday = config.businessDays.holidays?.some(
|
|
1885
|
+
(holiday) => isSame(holiday, parsedDate, "day")
|
|
1886
|
+
) || config.businessDays.holidayCheck?.(parsedDate);
|
|
1887
|
+
return !isHoliday;
|
|
1888
|
+
}
|
|
1889
|
+
function addBusinessDays(date, days) {
|
|
1890
|
+
let result = parseDate(date);
|
|
1891
|
+
let daysAdded = 0;
|
|
1892
|
+
const direction = days > 0 ? 1 : -1;
|
|
1893
|
+
while (daysAdded < Math.abs(days)) {
|
|
1894
|
+
result = addDays(result, direction);
|
|
1895
|
+
if (isBusinessDay(result)) {
|
|
1896
|
+
daysAdded++;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
return result;
|
|
1900
|
+
}
|
|
1901
|
+
function daysBetween(start, end, inclusive = false) {
|
|
1902
|
+
const startDate = startOfDay(parseDate(start));
|
|
1903
|
+
const endDate = startOfDay(parseDate(end));
|
|
1904
|
+
const diffMs = Math.abs(endDate.getTime() - startDate.getTime());
|
|
1905
|
+
const days = Math.floor(
|
|
1906
|
+
diffMs / (MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY)
|
|
1907
|
+
);
|
|
1908
|
+
return inclusive ? days + 1 : days;
|
|
1909
|
+
}
|
|
1910
|
+
function monthsBetween(start, end) {
|
|
1911
|
+
const startDate = parseDate(start);
|
|
1912
|
+
const endDate = parseDate(end);
|
|
1913
|
+
const yearsDiff = endDate.getFullYear() - startDate.getFullYear();
|
|
1914
|
+
const monthsDiff = endDate.getMonth() - startDate.getMonth();
|
|
1915
|
+
return yearsDiff * 12 + monthsDiff;
|
|
1916
|
+
}
|
|
1917
|
+
function yearsBetween(start, end) {
|
|
1918
|
+
const startDate = parseDate(start);
|
|
1919
|
+
const endDate = parseDate(end);
|
|
1920
|
+
return endDate.getFullYear() - startDate.getFullYear();
|
|
1921
|
+
}
|
|
1922
|
+
function getAge(birthDate, asOfDate = now()) {
|
|
1923
|
+
const birth = parseDate(birthDate);
|
|
1924
|
+
const asOf = parseDate(asOfDate);
|
|
1925
|
+
let age = asOf.getFullYear() - birth.getFullYear();
|
|
1926
|
+
const monthDiff = asOf.getMonth() - birth.getMonth();
|
|
1927
|
+
if (monthDiff < 0 || monthDiff === 0 && asOf.getDate() < birth.getDate()) {
|
|
1928
|
+
age--;
|
|
1929
|
+
}
|
|
1930
|
+
return Math.max(0, age);
|
|
1931
|
+
}
|
|
1932
|
+
function getWeekNumber(date) {
|
|
1933
|
+
const parsedDate = parseDate(date);
|
|
1934
|
+
const firstDayOfYear = new Date(parsedDate.getFullYear(), 0, 1);
|
|
1935
|
+
const pastDaysOfYear = (parsedDate.getTime() - firstDayOfYear.getTime()) / (MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY);
|
|
1936
|
+
return Math.ceil(
|
|
1937
|
+
(pastDaysOfYear + firstDayOfYear.getDay() + 1) / DAYS_IN_WEEK
|
|
1938
|
+
);
|
|
1939
|
+
}
|
|
1940
|
+
function getQuarter(date) {
|
|
1941
|
+
const month = parseDate(date).getMonth();
|
|
1942
|
+
return Math.floor(month / 3) + 1;
|
|
1943
|
+
}
|
|
1944
|
+
function getDayName(date, format = "full") {
|
|
1945
|
+
const parsedDate = parseDate(date);
|
|
1946
|
+
const dayIndex = parsedDate.getDay();
|
|
1947
|
+
return format === "full" ? WEEKDAY_NAMES[dayIndex] : WEEKDAY_NAMES_SHORT[dayIndex];
|
|
1948
|
+
}
|
|
1949
|
+
function getMonthName(date, format = "full") {
|
|
1950
|
+
const parsedDate = parseDate(date);
|
|
1951
|
+
const monthIndex = parsedDate.getMonth();
|
|
1952
|
+
return format === "full" ? MONTH_NAMES[monthIndex] : MONTH_NAMES_SHORT[monthIndex];
|
|
1953
|
+
}
|
|
1954
|
+
function getDaysInMonth(date) {
|
|
1955
|
+
const parsedDate = parseDate(date);
|
|
1956
|
+
return new Date(
|
|
1957
|
+
parsedDate.getFullYear(),
|
|
1958
|
+
parsedDate.getMonth() + 1,
|
|
1959
|
+
0
|
|
1960
|
+
).getDate();
|
|
1961
|
+
}
|
|
1962
|
+
function isLeapYear(year) {
|
|
1963
|
+
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
1964
|
+
}
|
|
1965
|
+
function validateDate(date, options = {}) {
|
|
1966
|
+
const errors = [];
|
|
1967
|
+
try {
|
|
1968
|
+
const parsedDate = parseDate(date);
|
|
1969
|
+
if (options.required && !parsedDate) {
|
|
1970
|
+
errors.push("Date is required");
|
|
1971
|
+
}
|
|
1972
|
+
if (options.min || options.minDate) {
|
|
1973
|
+
const minDate = parseDate(options.min || options.minDate);
|
|
1974
|
+
if (parsedDate < minDate) {
|
|
1975
|
+
errors.push(
|
|
1976
|
+
`Date must be on or after ${formatDate(minDate, {
|
|
1977
|
+
format: "MEDIUM"
|
|
1978
|
+
})}`
|
|
1979
|
+
);
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
if (options.max || options.maxDate) {
|
|
1983
|
+
const maxDate = parseDate(options.max || options.maxDate);
|
|
1984
|
+
if (parsedDate > maxDate) {
|
|
1985
|
+
errors.push(
|
|
1986
|
+
`Date must be on or before ${formatDate(maxDate, {
|
|
1987
|
+
format: "MEDIUM"
|
|
1988
|
+
})}`
|
|
1989
|
+
);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
if (options.allowPast === false && isPast(parsedDate)) {
|
|
1993
|
+
errors.push("Date must be in the future");
|
|
1994
|
+
}
|
|
1995
|
+
if (options.allowFuture === false && isFuture(parsedDate)) {
|
|
1996
|
+
errors.push("Date must be in the past");
|
|
1997
|
+
}
|
|
1998
|
+
} catch (error) {
|
|
1999
|
+
errors.push(error instanceof Error ? error.message : "Invalid date");
|
|
2000
|
+
}
|
|
2001
|
+
return {
|
|
2002
|
+
isValid: errors.length === 0,
|
|
2003
|
+
errors
|
|
2004
|
+
};
|
|
2005
|
+
}
|
|
2006
|
+
function getTimezoneInfo(date) {
|
|
2007
|
+
const parsedDate = parseDate(date);
|
|
2008
|
+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
2009
|
+
const offset = -parsedDate.getTimezoneOffset();
|
|
2010
|
+
const hours = Math.floor(Math.abs(offset) / 60);
|
|
2011
|
+
const minutes = Math.abs(offset) % 60;
|
|
2012
|
+
const sign = offset >= 0 ? "+" : "-";
|
|
2013
|
+
return {
|
|
2014
|
+
abbreviation: getTimezoneAbbreviation(parsedDate),
|
|
2015
|
+
name: timezone,
|
|
2016
|
+
offset,
|
|
2017
|
+
offsetString: `UTC${sign}${String(hours).padStart(2, "0")}:${String(
|
|
2018
|
+
minutes
|
|
2019
|
+
).padStart(2, "0")}`,
|
|
2020
|
+
isDST: isDST(parsedDate)
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
function convertTimezone(date, targetTimezone) {
|
|
2024
|
+
const parsedDate = parseDate(date);
|
|
2025
|
+
if (targetTimezone === "UTC") {
|
|
2026
|
+
return new Date(
|
|
2027
|
+
parsedDate.getTime() + parsedDate.getTimezoneOffset() * 6e4
|
|
2028
|
+
);
|
|
2029
|
+
}
|
|
2030
|
+
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
2031
|
+
timeZone: targetTimezone,
|
|
2032
|
+
year: "numeric",
|
|
2033
|
+
month: "numeric",
|
|
2034
|
+
day: "numeric",
|
|
2035
|
+
hour: "numeric",
|
|
2036
|
+
minute: "numeric",
|
|
2037
|
+
second: "numeric"
|
|
2038
|
+
});
|
|
2039
|
+
const parts = formatter.formatToParts(parsedDate);
|
|
2040
|
+
const getPart = (type) => parts.find((p) => p.type === type)?.value;
|
|
2041
|
+
return createDate(
|
|
2042
|
+
parseInt(getPart("year")),
|
|
2043
|
+
parseInt(getPart("month")) - 1,
|
|
2044
|
+
parseInt(getPart("day")),
|
|
2045
|
+
parseInt(getPart("hour")),
|
|
2046
|
+
parseInt(getPart("minute")),
|
|
2047
|
+
parseInt(getPart("second"))
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
2050
|
+
function formatUSDate(date) {
|
|
2051
|
+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
2052
|
+
const day = date.getDate().toString().padStart(2, "0");
|
|
2053
|
+
const year = date.getFullYear();
|
|
2054
|
+
return `${month}/${day}/${year}`;
|
|
2055
|
+
}
|
|
2056
|
+
function formatEUDate(date) {
|
|
2057
|
+
const day = date.getDate().toString().padStart(2, "0");
|
|
2058
|
+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
2059
|
+
const year = date.getFullYear();
|
|
2060
|
+
return `${day}/${month}/${year}`;
|
|
2061
|
+
}
|
|
2062
|
+
function formatFullDate(date, locale) {
|
|
2063
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2064
|
+
weekday: "long",
|
|
2065
|
+
year: "numeric",
|
|
2066
|
+
month: "long",
|
|
2067
|
+
day: "numeric"
|
|
2068
|
+
}).format(date);
|
|
2069
|
+
}
|
|
2070
|
+
function formatLongDate(date, locale) {
|
|
2071
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2072
|
+
year: "numeric",
|
|
2073
|
+
month: "long",
|
|
2074
|
+
day: "numeric"
|
|
2075
|
+
}).format(date);
|
|
2076
|
+
}
|
|
2077
|
+
function formatMediumDate(date, locale) {
|
|
2078
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2079
|
+
year: "numeric",
|
|
2080
|
+
month: "short",
|
|
2081
|
+
day: "numeric"
|
|
2082
|
+
}).format(date);
|
|
2083
|
+
}
|
|
2084
|
+
function formatShortDate(date, locale) {
|
|
2085
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2086
|
+
year: "2-digit",
|
|
2087
|
+
month: "numeric",
|
|
2088
|
+
day: "numeric"
|
|
2089
|
+
}).format(date);
|
|
2090
|
+
}
|
|
2091
|
+
function formatTime(date, locale, includeSeconds) {
|
|
2092
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2093
|
+
hour: "numeric",
|
|
2094
|
+
minute: "2-digit",
|
|
2095
|
+
second: includeSeconds ? "2-digit" : void 0,
|
|
2096
|
+
hour12: true
|
|
2097
|
+
}).format(date);
|
|
2098
|
+
}
|
|
2099
|
+
function formatTime24(date, includeSeconds) {
|
|
2100
|
+
const hours = date.getHours().toString().padStart(2, "0");
|
|
2101
|
+
const minutes = date.getMinutes().toString().padStart(2, "0");
|
|
2102
|
+
const seconds = includeSeconds ? `:${date.getSeconds().toString().padStart(2, "0")}` : "";
|
|
2103
|
+
return `${hours}:${minutes}${seconds}`;
|
|
2104
|
+
}
|
|
2105
|
+
function formatTimestamp(date) {
|
|
2106
|
+
const year = date.getFullYear();
|
|
2107
|
+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
2108
|
+
const day = date.getDate().toString().padStart(2, "0");
|
|
2109
|
+
const hours = date.getHours().toString().padStart(2, "0");
|
|
2110
|
+
const minutes = date.getMinutes().toString().padStart(2, "0");
|
|
2111
|
+
const seconds = date.getSeconds().toString().padStart(2, "0");
|
|
2112
|
+
return `${year}${month}${day}${hours}${minutes}${seconds}`;
|
|
2113
|
+
}
|
|
2114
|
+
function formatRFC2822(date) {
|
|
2115
|
+
return date.toUTCString();
|
|
2116
|
+
}
|
|
2117
|
+
function formatCustomDate(date, format) {
|
|
2118
|
+
const tokens = {
|
|
2119
|
+
YYYY: date.getFullYear().toString(),
|
|
2120
|
+
YY: date.getFullYear().toString().slice(-2),
|
|
2121
|
+
MMMM: MONTH_NAMES[date.getMonth()],
|
|
2122
|
+
MMM: MONTH_NAMES_SHORT[date.getMonth()],
|
|
2123
|
+
MM: (date.getMonth() + 1).toString().padStart(2, "0"),
|
|
2124
|
+
M: (date.getMonth() + 1).toString(),
|
|
2125
|
+
DDDD: WEEKDAY_NAMES[date.getDay()],
|
|
2126
|
+
DDD: WEEKDAY_NAMES_SHORT[date.getDay()],
|
|
2127
|
+
DD: date.getDate().toString().padStart(2, "0"),
|
|
2128
|
+
D: date.getDate().toString(),
|
|
2129
|
+
HH: date.getHours().toString().padStart(2, "0"),
|
|
2130
|
+
H: date.getHours().toString(),
|
|
2131
|
+
hh: (date.getHours() % 12 || 12).toString().padStart(2, "0"),
|
|
2132
|
+
h: (date.getHours() % 12 || 12).toString(),
|
|
2133
|
+
mm: date.getMinutes().toString().padStart(2, "0"),
|
|
2134
|
+
m: date.getMinutes().toString(),
|
|
2135
|
+
ss: date.getSeconds().toString().padStart(2, "0"),
|
|
2136
|
+
s: date.getSeconds().toString(),
|
|
2137
|
+
SSS: date.getMilliseconds().toString().padStart(3, "0"),
|
|
2138
|
+
A: date.getHours() < 12 ? "AM" : "PM",
|
|
2139
|
+
a: date.getHours() < 12 ? "am" : "pm"
|
|
2140
|
+
};
|
|
2141
|
+
return format.replace(
|
|
2142
|
+
/YYYY|YY|MMMM|MMM|MM|M|DDDD|DDD|DD|D|HH|H|hh|h|mm|m|ss|s|SSS|A|a/g,
|
|
2143
|
+
(match) => tokens[match] || match
|
|
2144
|
+
);
|
|
2145
|
+
}
|
|
2146
|
+
function validateDateParts(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0) {
|
|
2147
|
+
if (year < 1e3 || year > 9999) {
|
|
2148
|
+
throw new Error(`Year must be between 1000 and 9999: ${year}`);
|
|
2149
|
+
}
|
|
2150
|
+
if (month < 0 || month > 11) {
|
|
2151
|
+
throw new Error(`Month must be between 0 and 11: ${month}`);
|
|
2152
|
+
}
|
|
2153
|
+
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
|
2154
|
+
if (day < 1 || day > daysInMonth) {
|
|
2155
|
+
throw new Error(
|
|
2156
|
+
`Day must be between 1 and ${daysInMonth} for month ${month}: ${day}`
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
if (hour < 0 || hour > 23) {
|
|
2160
|
+
throw new Error(`Hour must be between 0 and 23: ${hour}`);
|
|
2161
|
+
}
|
|
2162
|
+
if (minute < 0 || minute > 59) {
|
|
2163
|
+
throw new Error(`Minute must be between 0 and 59: ${minute}`);
|
|
2164
|
+
}
|
|
2165
|
+
if (second < 0 || second > 59) {
|
|
2166
|
+
throw new Error(`Second must be between 0 and 59: ${second}`);
|
|
2167
|
+
}
|
|
2168
|
+
if (millisecond < 0 || millisecond > 999) {
|
|
2169
|
+
throw new Error(`Millisecond must be between 0 and 999: ${millisecond}`);
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
function getTimezoneAbbreviation(date) {
|
|
2173
|
+
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
2174
|
+
timeZoneName: "short"
|
|
2175
|
+
});
|
|
2176
|
+
const parts = formatter.formatToParts(date);
|
|
2177
|
+
const timezonePart = parts.find((part) => part.type === "timeZoneName");
|
|
2178
|
+
return timezonePart ? timezonePart.value : "UTC";
|
|
2179
|
+
}
|
|
2180
|
+
function isDST(date) {
|
|
2181
|
+
const jan = new Date(date.getFullYear(), 0, 1);
|
|
2182
|
+
const jul = new Date(date.getFullYear(), 6, 1);
|
|
2183
|
+
const stdTimezoneOffset = Math.max(
|
|
2184
|
+
jan.getTimezoneOffset(),
|
|
2185
|
+
jul.getTimezoneOffset()
|
|
2186
|
+
);
|
|
2187
|
+
return date.getTimezoneOffset() < stdTimezoneOffset;
|
|
2188
|
+
}
|
|
2189
|
+
export {
|
|
2190
|
+
add,
|
|
2191
|
+
addBusinessDays,
|
|
2192
|
+
addDays,
|
|
2193
|
+
addMonths,
|
|
2194
|
+
addYears,
|
|
2195
|
+
all,
|
|
2196
|
+
any,
|
|
2197
|
+
capitalize,
|
|
2198
|
+
checkConditions,
|
|
2199
|
+
compare,
|
|
2200
|
+
compareBoolean,
|
|
2201
|
+
configureDateUtils,
|
|
2202
|
+
convertTimezone,
|
|
2203
|
+
countFalsy,
|
|
2204
|
+
countOccurrences,
|
|
2205
|
+
countTruthy,
|
|
2206
|
+
createDate,
|
|
2207
|
+
createPipeline,
|
|
2208
|
+
createToggle,
|
|
2209
|
+
daysBetween,
|
|
2210
|
+
debounceBoolean,
|
|
2211
|
+
difference,
|
|
2212
|
+
endOf,
|
|
2213
|
+
endOfDay,
|
|
2214
|
+
endOfMonth,
|
|
2215
|
+
endOfWeek,
|
|
2216
|
+
endOfYear,
|
|
2217
|
+
endsWith,
|
|
2218
|
+
escapeHtml,
|
|
2219
|
+
escapeRegExp,
|
|
2220
|
+
escapeString,
|
|
2221
|
+
extractDomain,
|
|
2222
|
+
formatDate,
|
|
2223
|
+
getAge,
|
|
2224
|
+
getDayName,
|
|
2225
|
+
getDaysInMonth,
|
|
2226
|
+
getInitials,
|
|
2227
|
+
getMonthName,
|
|
2228
|
+
getQuarter,
|
|
2229
|
+
getTimezoneInfo,
|
|
2230
|
+
getWeekNumber,
|
|
2231
|
+
humanizeDifference,
|
|
2232
|
+
includes,
|
|
2233
|
+
includesIgnoreCase,
|
|
2234
|
+
isAfter,
|
|
2235
|
+
isBefore,
|
|
2236
|
+
isBetween,
|
|
2237
|
+
isBoolean,
|
|
2238
|
+
isBooleanArray,
|
|
2239
|
+
isBooleanLike,
|
|
2240
|
+
isBooleanLikeArray,
|
|
2241
|
+
isBusinessDay,
|
|
2242
|
+
isDate,
|
|
2243
|
+
isEmpty,
|
|
2244
|
+
isFuture,
|
|
2245
|
+
isLeapYear,
|
|
2246
|
+
isPalindrome,
|
|
2247
|
+
isPast,
|
|
2248
|
+
isPrimitiveArray,
|
|
2249
|
+
isSame,
|
|
2250
|
+
isString,
|
|
2251
|
+
isStringArray,
|
|
2252
|
+
isToday,
|
|
2253
|
+
isValidEmail,
|
|
2254
|
+
isValidUrl,
|
|
2255
|
+
isWeekday,
|
|
2256
|
+
isWeekend,
|
|
2257
|
+
mask,
|
|
2258
|
+
memoizeBoolean,
|
|
2259
|
+
monthsBetween,
|
|
2260
|
+
nand,
|
|
2261
|
+
none,
|
|
2262
|
+
nor,
|
|
2263
|
+
normalizeLineEndings,
|
|
2264
|
+
not,
|
|
2265
|
+
now,
|
|
2266
|
+
pad,
|
|
2267
|
+
parseArrayLiteral,
|
|
2268
|
+
parseDate,
|
|
2269
|
+
parseLiteral,
|
|
2270
|
+
parsePrimitiveValue,
|
|
2271
|
+
randomBoolean,
|
|
2272
|
+
randomString,
|
|
2273
|
+
removeWhitespace,
|
|
2274
|
+
replaceAll,
|
|
2275
|
+
replaceAllIgnoreCase,
|
|
2276
|
+
replaceString,
|
|
2277
|
+
replaceStringPattern,
|
|
2278
|
+
resetDateConfig,
|
|
2279
|
+
reverse,
|
|
2280
|
+
safeToBoolean,
|
|
2281
|
+
slugify,
|
|
2282
|
+
startOf,
|
|
2283
|
+
startOfDay,
|
|
2284
|
+
startOfMonth,
|
|
2285
|
+
startOfWeek,
|
|
2286
|
+
startOfYear,
|
|
2287
|
+
startsWith,
|
|
2288
|
+
stringToObjectArray,
|
|
2289
|
+
stringToObjectArrayOnly,
|
|
2290
|
+
stringToPrimitiveArray,
|
|
2291
|
+
subtract,
|
|
2292
|
+
subtractDays,
|
|
2293
|
+
subtractMonths,
|
|
2294
|
+
subtractYears,
|
|
2295
|
+
toBoolean,
|
|
2296
|
+
toCamelCase,
|
|
2297
|
+
toKebabCase,
|
|
2298
|
+
toPascalCase,
|
|
2299
|
+
toSnakeCase,
|
|
2300
|
+
toString,
|
|
2301
|
+
toTitleCase,
|
|
2302
|
+
today,
|
|
2303
|
+
tomorrow,
|
|
2304
|
+
trim,
|
|
2305
|
+
trimEnd,
|
|
2306
|
+
trimStart,
|
|
2307
|
+
truncate,
|
|
2308
|
+
unescapeHtml,
|
|
2309
|
+
validateBooleanConstraint,
|
|
2310
|
+
validateDate,
|
|
2311
|
+
wordWrap,
|
|
2312
|
+
xnor,
|
|
2313
|
+
xor,
|
|
2314
|
+
yearsBetween,
|
|
2315
|
+
yesterday
|
|
2316
|
+
};
|