UNPKG

@frank-auth/react

Version:

Flexible and customizable React UI components for Frank Authentication

3 lines (2 loc) 7.44 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=(t,e={},r="en")=>{try{const n=typeof t=="string"||typeof t=="number"?new Date(t):t;if(isNaN(n.getTime()))return"Invalid Date";const o={year:"numeric",month:"short",day:"numeric",...e};return new Intl.DateTimeFormat(r,o).format(n)}catch{return"Invalid Date"}},d=(t,e={},r="en")=>{const n={year:"numeric",month:"short",day:"numeric",hour:"numeric",minute:"2-digit",...e};return i(t,n,r)},b=(t,e={},r="en")=>{const n={hour:"numeric",minute:"2-digit",second:"2-digit",hour12:!0,...e};return i(t,n,r)},l=(t,e="en")=>{try{const r=typeof t=="string"||typeof t=="number"?new Date(t):t;if(isNaN(r.getTime()))return"Invalid Date";const o=Math.floor((new Date().getTime()-r.getTime())/1e3);if(o<60)return"just now";const a=Math.floor(o/60);if(a<60)return`${a} minute${a===1?"":"s"} ago`;const s=Math.floor(a/60);if(s<24)return`${s} hour${s===1?"":"s"} ago`;const c=Math.floor(s/24);if(c<7)return`${c} day${c===1?"":"s"} ago`;const m=Math.floor(c/7);if(m<4)return`${m} week${m===1?"":"s"} ago`;const f=Math.floor(c/30);if(f<12)return`${f} month${f===1?"":"s"} ago`;const p=Math.floor(c/365);return`${p} year${p===1?"":"s"} ago`}catch{return"Invalid Date"}},M=t=>{if(t<0)return"0s";const e=Math.floor(t/1e3),r=Math.floor(e/60),n=Math.floor(r/60),o=Math.floor(n/24);return o>0?`${o}d ${n%24}h`:n>0?`${n}h ${r%60}m`:r>0?`${r}m ${e%60}s`:`${e}s`},C=t=>l(t),u=(t,e={},r="en")=>{try{return new Intl.NumberFormat(r,e).format(t)}catch{return t.toString()}},w=(t,e="USD",r="en")=>u(t,{style:"currency",currency:e},r),y=(t,e=1,r="en")=>u(t/100,{style:"percent",minimumFractionDigits:e,maximumFractionDigits:e},r),N=t=>{if(t===0)return"0 B";const e=1024,r=["B","KB","MB","GB","TB"],n=Math.floor(Math.log(t)/Math.log(e));return`${Number.parseFloat((t/Math.pow(e,n)).toFixed(1))} ${r[n]}`},T=(t,e="en")=>{if(Math.abs(t)<1e3)return t.toString();try{return new Intl.NumberFormat(e,{notation:"compact",compactDisplay:"short"}).format(t)}catch{const n=["","K","M","B","T"],o=Math.floor(Math.log(Math.abs(t))/Math.log(1e3));return`${Number.parseFloat((t/Math.pow(1e3,o)).toFixed(1))}${n[o]}`}},h=(t,e)=>[t,e].filter(Boolean).join(" "),D=t=>t.split(" ").map(e=>e.charAt(0).toUpperCase()).slice(0,2).join(""),I=t=>t.firstName||t.lastName?h(t.firstName,t.lastName):t.username?t.username:t.emailAddress?t.emailAddress.split("@")[0]:"Unknown User",A=t=>t.trim().toLowerCase(),j=(t,e="national")=>{const r=t.replace(/[^\d+]/g,"");if(r.length===10){const n=r.match(/^(\d{3})(\d{3})(\d{4})$/);if(n)switch(e){case"international":return`+1 ${n[1]} ${n[2]} ${n[3]}`;case"e164":return`+1${r}`;case"national":default:return`(${n[1]}) ${n[2]}-${n[3]}`}}if(r.length===11&&r.startsWith("1")){const o=r.substring(1).match(/^(\d{3})(\d{3})(\d{4})$/);if(o)switch(e){case"international":return`+1 ${o[1]} ${o[2]} ${o[3]}`;case"e164":return`+${r}`;case"national":default:return`(${o[1]}) ${o[2]}-${o[3]}`}}return r.startsWith("+")?r:t},S=t=>{const[e,r]=t.split("@");if(!r)return t;if(e.length<=3)return`${e[0]}***@${r}`;const n=e[0],o=e[e.length-1],a=Math.max(3,e.length-2);return`${n}${"*".repeat(a)}${o}@${r}`},B=t=>{const e=t.replace(/[^\d]/g,"");if(e.length>=10){const r=e.slice(-4);return`${"*".repeat(e.length-4)}${r}`}return t},F=(t,e,r="...")=>t.length<=e?t:t.substring(0,e-r.length)+r,$=(t,e,r="...")=>{if(t.length<=e)return t;const n=r.length,o=e-n,a=Math.ceil(o/2),s=Math.floor(o/2);return t.substring(0,a)+r+t.substring(t.length-s)},g=(t,e)=>{switch(e){case"camel":return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(r,n)=>n===0?r.toLowerCase():r.toUpperCase()).replace(/\s+/g,"");case"pascal":return t.replace(/(?:^\w|[A-Z]|\b\w)/g,r=>r.toUpperCase()).replace(/\s+/g,"");case"snake":return t.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_");case"kebab":return t.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("-");case"sentence":return t.charAt(0).toUpperCase()+t.slice(1).toLowerCase();case"title":return t.replace(/\w\S*/g,r=>r.charAt(0).toUpperCase()+r.substr(1).toLowerCase());case"upper":return t.toUpperCase();case"lower":return t.toLowerCase();default:return t}},k=t=>t.length>12?$(t,12):t,L=t=>{if(t.length<=8)return t;const e=t.substring(0,4),r=t.substring(t.length-4);return`${e}...${r}`},O=t=>[t.street,t.city,t.state&&t.postalCode?`${t.state} ${t.postalCode}`:t.state||t.postalCode,t.country].filter(Boolean).join(", "),U=(t,e={})=>{const{style:r="long",type:n="conjunction",locale:o="en"}=e;if(t.length===0)return"";if(t.length===1)return t[0];try{return new Intl.DateTimeFormat(o,{style:r,type:n}).format(t)}catch{if(t.length===2)return`${t[0]} ${n==="conjunction"?"and":"or"} ${t[1]}`;const a=t[t.length-1],s=t.slice(0,-1),c=n==="conjunction"?"and":"or";return`${s.join(", ")}, ${c} ${a}`}},R=(t,e=2,r=10)=>{try{return JSON.stringify(t,null,e)}catch{return"[Circular Reference]"}},P=t=>{try{return JSON.stringify(t)}catch{return"[Circular Reference]"}},W=t=>{try{return new URL(t).hostname}catch{return t}},E=t=>!t.startsWith("http://")&&!t.startsWith("https://")?`https://${t}`:t,J=t=>{const e=t.replace("#","");return e.length===3?`#${e.split("").map(r=>r+r).join("")}`:e.length===6?`#${e}`:t},G=(t,e,r)=>`rgb(${Math.round(t)}, ${Math.round(e)}, ${Math.round(r)})`,v=(t,e,r,n)=>`rgba(${Math.round(t)}, ${Math.round(e)}, ${Math.round(r)}, ${n})`,z=(t,e,r)=>{const n=g(t,"sentence");switch(e){case"required":return`${n} is required`;case"email":return`${n} must be a valid email address`;case"minLength":return`${n} must be at least ${r} characters`;case"maxLength":return`${n} must be no more than ${r} characters`;case"pattern":return`${n} format is invalid`;case"number":return`${n} must be a number`;case"min":return`${n} must be at least ${r}`;case"max":return`${n} must be no more than ${r}`;default:return`${n} is invalid`}};function Z(t){switch(t){case"left":return"text-left";case"center":return"text-center";case"right":return"text-right";default:return"text-left"}}const H={formatDate:i,formatDateTime:d,formatTime:b,formatRelativeTime:l,formatDuration:M,formatTimeAgo:C,formatNumber:u,formatCurrency:w,formatPercentage:y,formatFileSize:N,formatCompactNumber:T,formatName:h,formatInitials:D,formatDisplayName:I,formatEmail:A,formatPhoneNumber:j,maskEmail:S,maskPhoneNumber:B,truncateText:F,truncateMiddle:$,formatTextCase:g,formatId:k,formatTokenPreview:L,formatAddress:O,formatList:U,formatJSON:R,formatJSONCompact:P,formatDomain:W,formatURL:E,formatHexColor:J,formatRGBColor:G,formatRGBAColor:v,formatValidationError:z};exports.FormatUtils=H;exports.formatAddress=O;exports.formatCompactNumber=T;exports.formatCurrency=w;exports.formatDate=i;exports.formatDateTime=d;exports.formatDisplayName=I;exports.formatDomain=W;exports.formatDuration=M;exports.formatEmail=A;exports.formatFileSize=N;exports.formatHexColor=J;exports.formatId=k;exports.formatInitials=D;exports.formatJSON=R;exports.formatJSONCompact=P;exports.formatList=U;exports.formatName=h;exports.formatNumber=u;exports.formatPercentage=y;exports.formatPhoneNumber=j;exports.formatRGBAColor=v;exports.formatRGBColor=G;exports.formatRelativeTime=l;exports.formatTextCase=g;exports.formatTime=b;exports.formatTimeAgo=C;exports.formatTokenPreview=L;exports.formatURL=E;exports.formatValidationError=z;exports.getTitleAlignment=Z;exports.maskEmail=S;exports.maskPhoneNumber=B;exports.truncateMiddle=$;exports.truncateText=F; //# sourceMappingURL=format.cjs.map