UNPKG

@jdlien/validator-utils

Version:

Validation and sanitization functions used by @jdlien/Validator.

1 lines 11.9 kB
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=/^(?:[a-z+]+:)?\/\//i,t=/^(?:[-a-z+]+:)?\/\//i,n=/^\d{5}(-\d{4})?$/,r=/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/,i=/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/,a=/^\d{3}-\d{3}-\d{4}$/,o=/^-?\d*$/,s=/^-?\d*\.?\d*$/,c={midnight:0,noon:12,midday:12};function l(e){let t=e.trim().toLowerCase().replace(/\.+$/g,``);if(t===`now`){let e=new Date;return{hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds()}}if(t in c)return{hour:c[t],minute:0,second:0};t=t.replace(/\s+/g,``).replace(/\.+$/g,``);let n=t.replace(/^(\d{1,2})(\d{2})([ap]?m?\.?)$/i,`$1:$2$3`).match(/^(\d{1,2})(?::(\d{1,2}))?(?::(\d{1,2}))?\s*([ap])?\.?m?\.?$/i);if(!n)return null;let r=+n[1],i=+(n[2]||0),a=+(n[3]||0),o=n[4]?.toLowerCase();return o===`p`&&r<12&&(r+=12),o===`a`&&r===12&&(r=0),r>23||i>59||a>59?null:{hour:r,minute:i,second:a}}function u(e){return l(e)!==null}function d(e){return/^[ap]\.?m?\.?$/i.test(e.replace(/\s/g,``))}var f=RegExp(`(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*`,`i`);function p(e){return new Date(`1 ${e} 2000`).getMonth()}function m(e){if(typeof e==`string`&&(e=parseInt(e.replace(/\D/g,``))),e>99)return e;let t=(new Date().getFullYear()+20)%100;return e+(e<t?2e3:1900)}function h(e){if(e instanceof Date)return e;let t=e.trim().toLowerCase(),n=new Date(new Date().setHours(0,0,0,0));if(/^(now|today)$/.test(t))return n;if(t===`yesterday`)return new Date(n.setDate(n.getDate()-1));if(t===`tomorrow`)return new Date(new Date().setHours(0,0,0,0)+864e5);let r=e.match(/(\d{2,4})\s*[年년]\s*(\d{1,2})\s*[月월]\s*(\d{1,2})\s*[日일]?/);if(r)return new Date(m(+r[1]),r[2]-1,+r[3],0,0,0);t=t.replace(/\b(mon|tue|wed|thu|fri|sat|sun|lun|mar(?:di|tes)|mer|jeu|ven|sam|dim|dom)[a-z]*\.?\b/gi,``).trim();let i=0,a=0,o=0,s=t.match(/(\d{1,2}:\d{2}(?::\d{2})?\s*[ap]?\.?m?\.?)/i);if(s){let e=l(s[1]);if(e&&({hour:i,minute:a,second:o}=e),t=t.replace(s[0],``).trim(),!t||t.length<=2){let e=new Date;return new Date(e.getFullYear(),e.getMonth(),e.getDate(),i,a,o)}}if(/^\d{8}$/.test(t))return new Date(+t.slice(0,4),t.slice(4,6)-1,+t.slice(6,8),i,a,o);if(/^\d{6}$/.test(t))return new Date(m(+t.slice(0,2)),t.slice(2,4)-1,+t.slice(4,6),i,a,o);let c=t.match(f),u=-1,d=0,h=0;c&&(u=p(c[1]),t=t.replace(c[0],` `).trim());let g=t.match(/'(\d{2})\b/);g&&(d=m(+g[1]),t=t.replace(g[0],` `).trim());let _=t.match(/\d+/g)?.map(Number)||[];if(u>=0)_.length>=2?_[0]>99?(d=_[0],h=_[1]):_[1]>99?(d=_[1],h=_[0]):_[0]>31?(h=_[1],d=m(_[0])):_[1]>31?(h=_[0],d=m(_[1])):(h=_[0],d||=m(_[1])):_.length===1&&(h=_[0],d||=new Date().getFullYear());else if(_.length>=3){let[e,t,n]=_;e>31?(d=e,u=t-1,h=n):e>12&&n>12?(h=e,u=t-1,d=n>31?n:m(n)):n>31||n>12?(u=e-1,h=t,d=n>31?n:m(n)):(u=e-1,h=t,d=m(n))}else _.length===2&&(u=_[0]-1,h=_[1],d||=new Date().getFullYear());return d&&u>=0&&h&&h>=1&&h<=31?new Date(d>99?d:m(d),u,h,i,a,o):new Date(``)}function g(e){if(e instanceof Date)return e;let t=e.trim();if(t.length<3)return null;if(t=t.replace(/(\d)T(\d)/i,`$1 $2`),t=t.replace(/(^|[\sT])(\d{1,2})\.(\d{1,2})(?:\.(\d{1,2}))?(?=\s*[ap]\.?m?\.?\b|(?:\s|$))/gi,(e,t,n,r,i)=>`${t}${i?`${n}:${r}:${i}`:`${n}:${r}`}`),/^now$/i.test(t)){let e=new Date;return e.setMilliseconds(0),e}if(/^(noon|midday)$/i.test(t)){let e=new Date;return new Date(e.getFullYear(),e.getMonth(),e.getDate(),12,0,0)}if(/^midnight$/i.test(t)){let e=new Date;return new Date(e.getFullYear(),e.getMonth(),e.getDate(),0,0,0)}let n=null,r=t;for(let e of[/\bmidnight\b/i,/\bmidday\b/i,/(\d{1,2}:\d{1,2}(?::\d{2})?)\s*([ap]\.?m?\.?)?/i,/\b(\d{1,2})\s*([ap]\.?m?\.?)\b/i,/\b(\d{3,4})([ap])m?\b/i]){let i=t.match(e);if(i){let e=l(i[0]);if(e){n=e,r=t.replace(i[0],` `).replace(/[\s.]+$/g,``).replace(/\s+/g,` `).trim();break}}}if(!n){let e=r.match(/^(\d{4}[\-\/\.\s]\d{1,2}[\-\/\.\s]\d{1,2}|\d{8})\s+(\d{1,6})(\s*[ap]\.?m?\.?)?(?:\s*(?:z|utc|gmt|[+-]\d{2}:?\d{2})\b)?$/i);if(e){let t=e[2],i=(e[3]||``).replace(/\s+/g,``),a=t+i;t.length===6&&(a=`${t.slice(0,2)}:${t.slice(2,4)}:${t.slice(4,6)}${i}`);let o=l(a);o&&(n=o,r=e[1])}}if(!n){let e=r.match(/^(.+?)\s+(\d{1,2})(\s*[ap]\.?m?\.?)?$/i);if(e){let t=l(e[2]+(e[3]||``));t&&f.test(e[1])&&(n=t,r=e[1])}}if(n&&(!r||/^,?\s*$/.test(r))){let e=new Date;return new Date(e.getFullYear(),e.getMonth(),e.getDate(),n.hour,n.minute,n.second)}let i=h(r);if(isNaN(i.getTime()))return null;let a=n||{hour:0,minute:0,second:0};return new Date(i.getFullYear(),i.getMonth(),i.getDate(),a.hour,a.minute,a.second)}function _(e,t=`YYYY-MM-DD`){if(typeof e==`string`&&(e=h(e)),isNaN(e.getTime()))return``;let n=e.getFullYear(),r=e.getMonth(),i=e.getDate(),a=e.getDay(),o=e.getHours(),s=e.getMinutes(),c=e.getSeconds(),l=e.getMilliseconds(),u=(e,t=2)=>String(e).padStart(t,`0`),d=o%12||12,f=o<12?`AM`:`PM`,p=[`January`,`February`,`March`,`April`,`May`,`June`,`July`,`August`,`September`,`October`,`November`,`December`],m=[`Sunday`,`Monday`,`Tuesday`,`Wednesday`,`Thursday`,`Friday`,`Saturday`],g={YYYY:n,YY:String(n).slice(-2),MMMM:p[r],MMM:p[r].slice(0,3),MM:u(r+1),M:r+1,DD:u(i),D:i,dddd:m[a],ddd:m[a].slice(0,3),dd:m[a].slice(0,2),d:a,HH:u(o),H:o,hh:u(d),h:d,mm:u(s),m:s,ss:u(c),s:c,SSS:u(l,3),A:f,a:f.toLowerCase()};return t.replace(/\[([^\]]+)]|YYYY|YY|MMMM|MMM|MM|M|DD|D|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|SSS|A|a/g,(e,t)=>t??String(g[e]))}var v={YYYY:`Y`,YY:`y`,MMMM:`F`,MMM:`M`,MM:`m`,M:`n`,DD:`d`,D:`j`,dddd:`l`,ddd:`D`,dd:`D`,d:`w`,HH:`H`,H:`G`,hh:`h`,mm:`i`,m:`i`,ss:`S`,s:`s`,A:`K`,a:`K`},y=/YYYY|YY|MMMM|MMM|MM|M|DD|D|dddd|ddd|dd|d|HH|H|hh|mm|m|ss|s|A|a/g;function b(e){return e.replace(y,e=>v[e])}function x(e){return typeof e!=`string`&&!(e instanceof Date)?!1:!isNaN(h(e).getTime())}function S(e){if(typeof e!=`string`&&!(e instanceof Date))return!1;let t=g(e);return t!==null&&!isNaN(t.getTime())}function C(e,t=`YYYY-MMM-DD`){let n=h(e);return isNaN(n.getTime())?``:_(n,t)}function w(e,t=`YYYY-MMM-DD h:mm A`){let n=g(e);return n&&!isNaN(n.getTime())?_(n,t):``}function T(e,t=`h:mm A`){let n=l(e);if(!n)return``;let r=new Date;return r.setHours(n.hour,n.minute,n.second,0),_(r,t)}var E={ja:0,en:0,fe:1,fé:1,ap:3,ab:3,av:3,mai:4,juin:5,juil:6,au:7,ag:7,ao:7,se:8,o:9,n:10,d:11};function D(e){if(typeof e==`number`)return e-1;let t=parseInt(e);if(!isNaN(t))return t-1;let n=new Date(`1 ${e} 2000`).getMonth();if(!isNaN(n))return n;let r=e.toLowerCase();for(let e in E)if(r.startsWith(e))return E[e];throw Error(`Invalid month name: `+e)}function O(e){let t=e.match(/^([+-])?(\d+)([dwmy])$/i);if(!t)return null;let[,n,r,i]=t,a=parseInt(r)*(n===`-`?-1:1),o=new Date;switch(o.setHours(0,0,0,0),i.toLowerCase()){case`d`:o.setDate(o.getDate()+a);break;case`w`:o.setDate(o.getDate()+a*7);break;case`m`:o.setMonth(o.getMonth()+a);break;case`y`:o.setFullYear(o.getFullYear()+a);break}return o}var k=/(\d{1,2}:\d{2}|\d{1,2}\.\d{2}|\b\d{1,2}\s*[ap]\b|\b[ap]\.?m\.?\b|\bnoon\b|\bmidday\b|\bmidnight\b|\bnow\b|T\d{1,2})/i,A=/(\d{1,2}[-/.]\d{1,2}(?:[-/.]\d{2,4})?|\b\d{4}\b|\b\d{6,8}\b|\b(?:jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)\b|\btoday\b|\btomorrow\b|\byesterday\b)/i;function j(e,t){if(t===`past`)return e<=new Date;if(t===`future`)return e.getTime()>=new Date().setHours(0,0,0,0);if(t===`today`){let t=new Date;return e.getFullYear()===t.getFullYear()&&e.getMonth()===t.getMonth()&&e.getDate()===t.getDate()}let n=e=>{let t=e.trim();if(!t)return{date:null,hasTime:!1,valid:!0};let n=O(t);if(n)return{date:n,hasTime:!1,valid:!0};let r=t.search(A);if(r===-1)return{date:null,hasTime:!1,valid:!1};let i=t.search(k);if(i!==-1&&i<r)return{date:null,hasTime:!1,valid:!1};let a=g(t);return a&&!isNaN(a.getTime())?{date:a,hasTime:k.test(t),valid:!0}:{date:null,hasTime:!1,valid:!1}},r=null;if(t.includes(`:`))for(let e=0;e<t.length;e+=1){if(t[e]!==`:`)continue;let i=n(t.slice(0,e)),a=n(t.slice(e+1));if(i.valid&&a.valid){r={start:i,end:a};break}}if(!r){let r=n(t);return r.valid&&r.date?r.hasTime?e.getTime()===r.date.getTime():e.getFullYear()===r.date.getFullYear()&&e.getMonth()===r.date.getMonth()&&e.getDate()===r.date.getDate():!0}let i=r.start,a=r.end,o=i.hasTime||a.hasTime?e.getTime():new Date(e.getFullYear(),e.getMonth(),e.getDate()).getTime(),s=null;i.date&&(s=i.hasTime?i.date.getTime():new Date(i.date.getFullYear(),i.date.getMonth(),i.date.getDate()).getTime());let c=null;return a.date&&(c=a.hasTime?a.date.getTime():new Date(a.date.getFullYear(),a.date.getMonth(),a.date.getDate(),23,59,59,999).getTime()),!(s!==null&&o<s||c!==null&&o>c)}function M(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function N(e,t){return typeof t==`string`&&(t=[t]),t.includes(e.dataset.type||``)||t.includes(e.type)}function P(e){return e.length<=255&&i.test(e)}function F(e){return e.replace(/^[^2-90]+/g,``).replace(/(\d\d\d).*?(\d\d\d).*?(\d\d\d\d)(.*)/,`$1-$2-$3$4`)}function I(e){return a.test(e)}function L(e){return e.replace(/[^0-9]/g,``)}function R(e){return o.test(e)}function z(e){let t=e.replace(/[^\-0-9.]/g,``),n=``,r=!1,i=!1;for(let e=0;e<t.length;e+=1){let a=t[e];if(a===`-`){!i&&n.length===0&&(n+=`-`,i=!0);continue}if(a===`.`){r||=(n+=`.`,!0);continue}n+=a}return n}function B(e){return s.test(e)}var V={"":1,K:1e3,M:1e6,G:1e9,T:0xe8d4a51000},H={"":1,K:1024,M:1024**2,G:1024**3,T:1024**4};function U(e){let t=e.trim().match(/^(\d+(?:\.\d*)?|\.\d+)\s*(B|Ki?B?|Mi?B?|Gi?B?|Ti?B?)?$/i);if(!t)return NaN;let n=Number.parseFloat(t[1]),r=t[2]?.toUpperCase()||``,i=r.includes(`I`),a=r.replace(/I?B$/i,``).replace(/I$/i,``)||``;return n*(i?H:V)[a]}function W(e,t=!0){let n=t?1e3:1024,r=[`B`,`KB`,`MB`,`GB`,`TB`];if(e<n)return`${e} B`;let i=0,a=e;for(;a>=n&&i<r.length-1;)a/=n,i++;let o=Math.round(a*10)/10;return o>=n&&i<r.length-1&&(o=1,i++),`${o%1==0?o.toFixed(0):o.toFixed(1)} ${r[i]}`}function G(t){return t=t.trim(),e.test(t)?t:`https://`+t}function K(e){return t.test(e)}function q(e){return e=e.replace(/[^0-9]/g,``).replace(/(.{5})(.*)/,`$1-$2`).trim(),e.length===6?e.replace(/-/,``):e}function J(e){return n.test(e)}function Y(e){return e.toUpperCase().replace(/[^A-Z0-9]/g,``).replace(/(.{3})\s*(.*)/,`$1 $2`).trim()}function X(e){return r.test(e)}function Z(e){return[`transparent`,`currentColor`].includes(e)?!0:!e.trim()||typeof CSS>`u`||!CSS.supports?!1:CSS.supports(`color`,e)}var Q=null,$=new Map;function ee(e){if(e=e.trim().toLowerCase(),[`transparent`,`currentcolor`].includes(e))return e;if($.has(e))return $.get(e);Q||(Q=document.createElement(`canvas`),Q.willReadFrequently=!0);let t=Q.getContext(`2d`);t.fillStyle=e,t.fillRect(0,0,1,1);let n=t.getImageData(0,0,1,1).data,r=`#`+(`000000`+(n[0]<<16|n[1]<<8|n[2]).toString(16)).slice(-6);return $.set(e,r),r}function te(e){if(typeof e==`boolean`)return{valid:e,error:!1,messages:[]};if(typeof e==`string`)return{valid:!1,error:!1,messages:[e]};let t={valid:e.valid,error:e.error??!1,messages:[]};return typeof e.message==`string`?t.messages=[e.message]:typeof e.messages==`string`?t.messages=[e.messages]:Array.isArray(e.messages)&&(t.messages=e.messages),t}exports.formatBytes=W,exports.formatDateTime=_,exports.isColor=Z,exports.isDate=x,exports.isDateInRange=j,exports.isDateTime=S,exports.isEmail=P,exports.isFormControl=M,exports.isInteger=R,exports.isMeridiem=d,exports.isNANPTel=I,exports.isNumber=B,exports.isPostalCA=X,exports.isTime=u,exports.isType=N,exports.isUrl=K,exports.isZip=J,exports.momentToFPFormat=b,exports.monthToNumber=D,exports.normalizeValidationResult=te,exports.parseBytes=U,exports.parseColor=ee,exports.parseDate=h,exports.parseDateTime=g,exports.parseDateTimeToString=w,exports.parseDateToString=C,exports.parseInteger=L,exports.parseNANPTel=F,exports.parseNumber=z,exports.parsePostalCA=Y,exports.parseRelativeDate=O,exports.parseTime=l,exports.parseTimeToString=T,exports.parseUrl=G,exports.parseZip=q,exports.yearToFull=m;