UNPKG

nano-string-utils

Version:

Modern string utilities with zero dependencies. Tree-shakeable (<1KB each), TypeScript-first, type-safe. Validation, XSS prevention, case conversion, fuzzy matching & more.

3 lines 29.2 kB
'use strict';var te=/[^\w\s-]/g,ne=/[\s_-]+/g,re=/^-+|-+$/g,O=e=>e&&e.toLowerCase().trim().replace(te,"").replace(ne,"-").replace(re,"");function b(e){if(!e)return [];if(typeof Intl<"u"&&"Segmenter"in Intl){let t=new Intl.Segmenter(void 0,{granularity:"grapheme"});return Array.from(t.segment(e),s=>s.segment)}return Array.from(e)}function se(e,t,s="..."){if(!e)return e;if(!/[^\x00-\x7F]/.test(e)){if(e.length<=t)return e;if(t<=s.length)return s;let i=t-s.length;return e.slice(0,i)+s}let n=b(e);if(n.length<=t)return e;if(t<=s.length)return s;let o=t-s.length;return n.slice(0,o).join("")+s}var ie=e=>e&&e.charAt(0).toUpperCase()+e.slice(1).toLowerCase();var oe=/\p{Lu}?\p{Ll}+|[0-9]+|\p{Lu}+(?!\p{Ll})|\p{Emoji_Presentation}|\p{Extended_Pictographic}|\p{L}+/gu;function y(e){return Array.from(e.match(oe)??[])}function ae(e){return e&&y(e).map((s,n)=>n===0?s.toLowerCase():s.charAt(0).toUpperCase()+s.slice(1).toLowerCase()).join("")}function ce(e){return e&&y(e).map(s=>s.toLowerCase()).join("_")}function le(e){return e&&y(e).map(s=>s.toLowerCase()).join("-")}var ue=/<[^>]*>/g,pe=e=>e&&e.replace(ue,"");var fe=/[&<>"']/g,ge={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},w=e=>e&&e.replace(fe,t=>ge[t]??t);var me=/[,;:\-–—]+$/;function he(e,t,s="..."){if(!e||e.length<=t)return e;let n=-1;for(let i=t;i>=0;i--)if(e[i]===" "){n=i;break}if(n>0){let i=e.slice(0,n).trim();return i=i.replace(me,""),i.endsWith(".")||i.endsWith("!")||i.endsWith("?")?i+"..":i+s}let o=e.indexOf(" ");return o===-1||o>t?e.slice(0,t)+s:e.slice(0,o)+s}function de(e,t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){let s=Math.floor(e);if(s<=0||t.length===0)return "";let n="",o=t.length;for(let i=0;i<s;i++)n+=t.charAt(Math.floor(Math.random()*o));return n}var xe=e=>{if(e==null)return 0;let t=2166136261;if(e.length===0)return t>>>0;for(let s=0;s<e.length;s++)t^=e.charCodeAt(s),t=t*16777619>>>0;return t};function Ce(e){return e&&y(e).map(s=>s.charAt(0).toUpperCase()+s.slice(1).toLowerCase()).join("")}var Se=e=>e&&(/[^\x00-\x7F]/.test(e)?b(e).reverse().join(""):e.split("").reverse().join(""));var Te=/^(\s*["']?\s*)([a-z])/,Ae=/([.!?])\s+([a-z])/g,Ee=/([.!?])\s*(['"])\s*([a-z])/g,ye=/([.!?])([a-z])/g,be=/\b([a-z]\.)+[a-z]$/,Le=/\b([a-z]\.){2,}$/,Re=/\bi\b/g,Ue=/\bi'([a-z])/g,Ne=/[A-Z]/,ze=/[a-z]/i;function _e(e){if(!e)return e;let t=e,s=e.toLowerCase();return s=s.replace(Te,(n,o,i)=>o+i.toUpperCase()),s=s.replace(Ae,(n,o,i,a)=>{if(o==="."){let r=s.substring(Math.max(0,a-10),a);if(be.test(r)||Le.test(r)){let u=t[a+n.indexOf(i)];return u&&Ne.test(u)?o+n.slice(o.length).replace(i,i.toUpperCase()):n}}return o+n.slice(o.length).replace(i,i.toUpperCase())}),s=s.replace(Ee,(n,o,i,a)=>o+n.slice(o.length).replace(a,a.toUpperCase())),s=s.replace(ye,(n,o,i,a)=>{if(o==="."){let r=s[a-1],c=s[a+2];if(r&&ze.test(r)&&c===".")return n}return o+i.toUpperCase()}),s=s.replace(Re,"I"),s=s.replace(Ue,(n,o)=>"I'"+o),s}var Me=/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,$=e=>!(!e||!Me.test(e)||e.includes("..")||e.endsWith(".")||e.includes("#")||e.includes("$")||e.includes("%"));var H=e=>{if(!e)return false;try{let t=new URL(e);return !(!["http:","https:","ftp:","ftps:"].includes(t.protocol)||t.hostname.includes(".."))}catch{return false}};var Ie=/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/,v=e=>e?Ie.test(e):false;var Pe=/^-?\d+(\.\d+)?$/,W=e=>{if(!e)return false;let t=e.trim();return t?Pe.test(t):false};var D=e=>e?/^[a-zA-Z0-9]+$/.test(e):false;var j=e=>e?/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e):false;var Oe=/\s+/,we=e=>{if(!e)return 0;let t=e.trim().split(Oe);return t[0]===""?0:t.length};function $e(e,t){return t.split(".").reduce((s,n)=>s?.[n],e)}function B(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function I(e,t,s){if(!e)return e;let n={delimiters:["{{","}}"],fallback:"",keepUnmatched:false,...s},[o,i]=n.delimiters,a=new RegExp(`${B(o)}\\s*([^${B(i)}]+?)\\s*${B(i)}`,"g");return e.replace(a,(r,c)=>{let u=c.trim(),p=$e(t,u);return p==null?n.keepUnmatched?r:n.fallback??"":String(p)})}function k(e){if(typeof e=="string")return w(e);if(Array.isArray(e))return e.map(k);if(e&&typeof e=="object"&&e.constructor===Object){let t={};for(let[s,n]of Object.entries(e))t[s]=k(n);return t}return e}function He(e,t,s){if(!e)return e;let n=k(t);return s?I(e,n,s):I(e,n)}function ve(e,t,s=" "){if(e==null)return e;let n=Array.from(e).length;if(t<=n)return e;let o=s||" ",i=Array.from(o),a=i.length,r=t-n,c=Math.floor(r/2),u=r-c;if(a===1)return o.repeat(c)+e+o.repeat(u);let p="";for(let g=0;g<c;g++)p+=i[g%a];let l="";for(let g=0;g<u;g++)l+=i[g%a];return p+e+l}function We(e,t,s=" "){if(e==null)return e;let n=Array.from(e).length;if(t<=n||t<=0)return e;let o=s||" ",i=Array.from(o),a=i.length,r=t-n,c=[];for(let p=0;p<r;p++)c.push(i[p%a]);return c.join("")+e}function De(e,t,s=" "){if(e==null)return e;let n=Array.from(e).length;if(t<=n||t<=0)return e;let o=s||" ",i=Array.from(o),a=i.length,r=t-n,c=[];for(let p=0;p<r;p++)c.push(i[p%a]);let u=c.join("");return e+u}var je=/[øØłŁđĐðÐþÞßæÆœŒ]/g,Be={\u00F8:"o",\u00D8:"O",\u0142:"l",\u0141:"L",\u0111:"d",\u0110:"D",\u00F0:"d",\u00D0:"D",\u00FE:"th",\u00DE:"Th",\u00DF:"ss",\u00E6:"ae",\u00C6:"Ae",\u0153:"oe",\u0152:"Oe"};function F(e){return e&&e.replace(je,s=>Be[s]||s).normalize("NFD").replace(/[\u0300-\u036f]/g,"").normalize("NFC")}var ke=/(\s+|[^\s\w'-]+)/g,Y=/[A-Za-z]/,Fe=/^[A-Z]{2,4}$/,q=new Set(["a","an","the","and","but","or","nor","for","yet","so","at","by","in","of","on","to","up","as","from","with","into","onto","upon","about","after","against","along","among","around","before","behind","below","beneath","beside","between","beyond","during","except","inside","outside","over","through","under","until","without"]);function Ve(e,t={}){if(!e||!e.trim())return "";let s=t.exceptions?new Set([...q,...t.exceptions.map(a=>a.toLowerCase())]):q,n=e.split(ke).filter(Boolean),o=0,i=n.filter(a=>Y.test(a)).length;return n.map(a=>{if(!Y.test(a))return a;let r=o===0,c=o===i-1;if(o++,Fe.test(a))return a;if(a.includes("-"))return a.split("-").map(p=>p&&V(p)).join("-");if(a.includes("'")){let p=a.split("'"),l=p[0]||"",g=p[1]||"";return V(l)+"'"+g.toLowerCase()}let u=a.toLowerCase();return !r&&!c&&s.has(u)?u:V(a)}).join("")}function V(e){return e?e.charAt(0).toUpperCase()+e.slice(1).toLowerCase():""}var Ze=/([A-Z]+)([A-Z][a-z])/g,Ge=/([a-z0-9])([A-Z])/g,Ke=/([a-zA-Z])([0-9])/g,Ye=/([0-9])([a-zA-Z])/g,qe=/[^a-z0-9]+/gi,Xe=/\s+/;function Je(e){return e&&e.trim().replace(Ze,"$1 $2").replace(Ge,"$1 $2").replace(Ke,"$1 $2").replace(Ye,"$1 $2").replace(qe," ").trim().split(Xe).filter(Boolean).map(s=>s.toUpperCase()).join("_")}var Qe=/([A-Z]+)([A-Z][a-z])/g,et=/([a-z0-9])([A-Z])/g,tt=/([a-zA-Z])([0-9])/g,nt=/([0-9])([a-zA-Z])/g,rt=/[^a-z0-9]+/gi,st=/^\.+|\.+$/g,it=/\.+/g;function ot(e){return e&&e.trim().replace(Qe,"$1.$2").replace(et,"$1.$2").replace(tt,"$1.$2").replace(nt,"$1.$2").replace(rt,".").replace(st,"").replace(it,".").toLowerCase()}var at=/([A-Z]+)([A-Z][a-z])/g,ct=/([a-z0-9])([A-Z])/g,lt=/([a-zA-Z])([0-9])/g,ut=/([0-9])([a-zA-Z])/g,pt=/[^a-z0-9]+/gi,ft=/^\/+|\/+$/g,gt=/\/+/g;function mt(e){return e&&e.trim().replace(at,"$1/$2").replace(ct,"$1/$2").replace(lt,"$1/$2").replace(ut,"$1/$2").replace(pt,"/").replace(ft,"").replace(gt,"/").toLowerCase()}function ht(e){if(!e)return [];let t=[];for(let s of e){let n=s.codePointAt(0);n!==void 0&&t.push(n);}return t}function dt(e){if(e==null)return false;for(let t=0;t<e.length;t++)if(e.charCodeAt(t)>127)return false;return true}var xt=/[\s\u00A0\u1680\u2000-\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF]+/g,Ct=/[\s\u00A0\u1680\u2000-\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF]/g,St=/[^\S\n]+/g,X=/[\u00A0\u1680\u2000-\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF\t\r\f\v]/g;function Tt(e,t={}){let{trim:s=true,collapse:n=true,preserveNewlines:o=false}=t;if(!e)return e;let i;return o?n?i=e.replace(X," ").replace(St," "):i=e.replace(X," "):n?i=e.replace(xt," "):i=e.replace(Ct," "),s&&(i=i.trim()),i}function At(e,t={}){let{keepSpace:s=true,keepNewlines:n=false,keepTabs:o=false,keepCarriageReturns:i=false}=t;if(!e)return e;let a="";for(let r of e){let c=r.charCodeAt(0);if(c<=31||c===127){(c===9&&o||c===10&&n||c===13&&i)&&(a+=r);continue}c===32&&!s||c>=128&&c<=159||c!==173&&(c===8203||c===8204||c===8206||c===8207||c>=8234&&c<=8238||c>=8288&&c<=8303||c!==65279&&(c>=65520&&c<=65535||(a+=r)));}return a}var Et=new Map([["\xA0"," "],["\u2003"," "],["\u2002"," "],["\u2009"," "],["\u200B",""],["\u201C",'"'],["\u201D",'"'],["\u2018","'"],["\u2019","'"],["\u201A",","],["\u201E",'"'],["\xAB",'"'],["\xBB",'"'],["\u2039","'"],["\u203A","'"],["\u2013","-"],["\u2014","-"],["\u2012","-"],["\u2015","-"],["\u2026","..."],["\u2022","*"],["\xB7","."],["\xBF","?"],["\xA1","!"],["\xD7","x"],["\xF7","/"],["\xB1","+/-"],["\u2248","~"],["\u2260","!="],["\u2264","<="],["\u2265",">="],["\u221E","inf"],["\u221A","sqrt"],["\u20AC","EUR"],["\xA3","GBP"],["\xA5","JPY"],["\xA2","c"],["\u20B9","INR"],["\u20BD","RUB"],["\xBD","1/2"],["\xBC","1/4"],["\xBE","3/4"],["\u2153","1/3"],["\u2154","2/3"],["\u2192","->"],["\u2190","<-"],["\u2191","^"],["\u2193","v"],["\u2194","<->"],["\xA9","(c)"],["\xAE","(R)"],["\u2122","(TM)"],["\xB0","deg"],["\xA7","S"],["\xB6","P"],["\u2020","+"],["\u2021","++"],["\u2116","No"],["\u0391","A"],["\u03B1","a"],["\u0392","B"],["\u03B2","b"],["\u0393","G"],["\u03B3","g"],["\u0394","D"],["\u03B4","d"],["\u0395","E"],["\u03B5","e"],["\u0396","Z"],["\u03B6","z"],["\u0397","H"],["\u03B7","h"],["\u0398","Th"],["\u03B8","th"],["\u0399","I"],["\u03B9","i"],["\u039A","K"],["\u03BA","k"],["\u039B","L"],["\u03BB","l"],["\u039C","M"],["\u03BC","m"],["\u039D","N"],["\u03BD","n"],["\u039E","X"],["\u03BE","x"],["\u039F","O"],["\u03BF","o"],["\u03A0","P"],["\u03C0","p"],["\u03A1","R"],["\u03C1","r"],["\u03A3","S"],["\u03C3","s"],["\u03C2","s"],["\u03A4","T"],["\u03C4","t"],["\u03A5","Y"],["\u03C5","u"],["\u03A6","Ph"],["\u03C6","ph"],["\u03A7","Ch"],["\u03C7","ch"],["\u03A8","Ps"],["\u03C8","ps"],["\u03A9","O"],["\u03C9","o"],["\u0410","A"],["\u0430","a"],["\u0411","B"],["\u0431","b"],["\u0412","V"],["\u0432","v"],["\u0413","G"],["\u0433","g"],["\u0414","D"],["\u0434","d"],["\u0415","E"],["\u0435","e"],["\u0401","E"],["\u0451","e"],["\u0416","Zh"],["\u0436","zh"],["\u0417","Z"],["\u0437","z"],["\u0418","I"],["\u0438","i"],["\u0419","Y"],["\u0439","y"],["\u041A","K"],["\u043A","k"],["\u041B","L"],["\u043B","l"],["\u041C","M"],["\u043C","m"],["\u041D","N"],["\u043D","n"],["\u041E","O"],["\u043E","o"],["\u041F","P"],["\u043F","p"],["\u0420","R"],["\u0440","r"],["\u0421","S"],["\u0441","s"],["\u0422","T"],["\u0442","t"],["\u0423","U"],["\u0443","u"],["\u0424","F"],["\u0444","f"],["\u0425","Kh"],["\u0445","kh"],["\u0426","Ts"],["\u0446","ts"],["\u0427","Ch"],["\u0447","ch"],["\u0428","Sh"],["\u0448","sh"],["\u0429","Sch"],["\u0449","sch"],["\u042A",""],["\u044A",""],["\u042B","Y"],["\u044B","y"],["\u042C",""],["\u044C",""],["\u042D","E"],["\u044D","e"],["\u042E","Yu"],["\u044E","yu"],["\u042F","Ya"],["\u044F","ya"]]),yt=/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g;function bt(e,t={}){if(!e)return e;let s="";for(let n of e){let o=Et.get(n);s+=o!==void 0?o:n;}return s=F(s),s=s.replace(yt,""),t.placeholder!==void 0?s=Array.from(s).map(o=>{let i=o.charCodeAt(0);return i===9||i===10||i===13||i>=32&&i<=126?o:t.placeholder}).join(""):s=Array.from(s).filter(o=>{let i=o.charCodeAt(0);return i===9||i===10||i===13||i>=32&&i<=126}).join(""),s}function Lt(e,t,s={}){if(!e||!t||Array.isArray(t)&&t.length===0)return e||"";let{caseSensitive:n=false,wholeWord:o=false,wrapper:i=["<mark>","</mark>"],className:a,escapeHtml:r=false}=s,c=Array.isArray(t)?t:[t],u=[...new Set(c.filter(h=>h))];if(u.length===0)return e;let l=u.sort((h,T)=>T.length-h.length).map(h=>h.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")),g=o?"\\b":"",f=l.map(h=>`${g}(${h})${g}`).join("|"),x=n?"g":"gi",m=new RegExp(f,x),d=i[0],C=i[1];a&&i[0]==="<mark>"&&(d=`<mark class="${a}">`);let S=e;if(r){let h={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};S=e.replace(/[&<>"']/g,T=>h[T]??T);}return S.replace(m,h=>`${d}${h}${C}`)}function Rt(e,t){if(e===t)return e;if(!e)return `{+${t}+}`;if(!t)return `[-${e}-]`;let s=[...e],n=[...t],o=0,i=Math.min(s.length,n.length);for(;o<i&&s[o]===n[o];)o++;let a=0,r=Math.min(s.length-o,n.length-o);for(;a<r&&s[s.length-1-a]===n[n.length-1-a];)a++;let c=s.slice(0,o).join(""),u=s.slice(s.length-a).join(""),p=s.slice(o,s.length-a).join(""),l=n.slice(o,n.length-a).join("");return !p&&!l?c+u:p?l?c+`[-${p}-]{+${l}+}`+u:c+`[-${p}-]`+u:c+`{+${l}+}`+u}function Z(e,t,s){if(e===t)return 0;let n=e.length,o=t.length;if(n===0)return o;if(o===0)return n;if(s!==void 0&&Math.abs(n-o)>s)return 1/0;let i=0,a=Math.min(n,o);for(;i<a&&e.charCodeAt(i)===t.charCodeAt(i);)i++;if(i===a)return Math.abs(n-o);let r=0,c=Math.min(n-i,o-i);for(;r<c&&e.charCodeAt(n-1-r)===t.charCodeAt(o-1-r);)r++;let u=i,p=n-r,l=i,g=o-r;if(n=p-u,o=g-l,n===0)return o;if(o===0)return n;let f=e,x=t,m=u,d=n,C=l,S=o;n>o&&(f=t,x=e,m=l,d=o,C=u,S=n);let h=Array(d+1);for(let T=0;T<=d;T++)h[T]=T;for(let T=1;T<=S;T++){let K=h[0];h[0]=T;let P=T;for(let E=1;E<=d;E++){let Q=h[E],ee=f.charCodeAt(m+E-1)===x.charCodeAt(C+T-1)?0:1;h[E]=Math.min(h[E]+1,h[E-1]+1,K+ee),P=Math.min(P,h[E]),K=Q;}if(s!==void 0&&P>s)return 1/0}return h[d]}function Ut(e,t){if(e.length===0&&t.length===0)return 1;if(e.length===0||t.length===0)return 0;let s=Z(e,t),n=Math.max(e.length,t.length);return 1-s/n}function Nt(e,t,s={}){let{caseSensitive:n=false,threshold:o=0}=s;if(!e)return {matched:false,score:0};if(!t||e.length>t.length)return null;let i=n?e:e.toLowerCase(),a=n?t:t.toLowerCase();if(i===a)return {matched:true,score:n?1:.95};let r=a.startsWith(i);if(r&&o>0&&o<=.85)return {matched:true,score:.85};let c=0,u=0,p=0,l=[];for(;c<i.length&&u<a.length;){if(i[c]===a[u]){if(l.push(u),l.length>1){let d=l[l.length-1],C=l[l.length-2];d-C===1&&p++;}c++;}u++;}if(c!==i.length)return null;let f=e.length/t.length*.4;if(o>0&&f+.25+.1+.35<o)return null;if(p>0&&(f+=p/e.length*.25),l.length>0){let C=1-l.reduce((S,h)=>S+h,0)/l.length/t.length;f+=C*.1;}if(o>0){if(f>=o){if(o<.75&&!r)return f=Math.min(Math.max(f,0),1),{matched:true,score:Math.round(f*1e3)/1e3}}else if(f+.35<o)return null}let x=/[\s\-_./\\]/,m=0;for(let d=0;d<l.length;d++){let C=l[d];if(C===0)m++;else if(C>0&&C<t.length){let S=t[C-1],h=t[C];(S&&x.test(S)||S&&h&&S===S.toLowerCase()&&h===h.toUpperCase())&&m++;}}return m>0&&(f+=m/e.length*.35),r&&(f=Math.max(f,.85)),m===e.length&&(f=Math.max(f,.75)),f=Math.min(Math.max(f,0),1),f<o?null:{matched:true,score:Math.round(f*1e3)/1e3}}var zt=/[aeiou]y$/;function _t(e,t){if(t===1)return e;if(!e)return e+"s";let s=e===e.toUpperCase(),n=e[0]===e[0]?.toUpperCase(),o=e.toLowerCase(),a={person:"people",child:"children",man:"men",woman:"women",tooth:"teeth",foot:"feet",mouse:"mice",goose:"geese",sheep:"sheep",deer:"deer",fish:"fish",series:"series",datum:"data",index:"indices"}[o];if(a)return s?a.toUpperCase():n&&a[0]?a[0].toUpperCase()+a.slice(1):a;let r=e;return o.endsWith("is")?r=e.slice(0,-2)+"es":o.endsWith("us")&&!["bus","plus"].includes(o)?r=e.slice(0,-2)+"i":o.endsWith("s")||o.endsWith("ss")||o.endsWith("sh")||o.endsWith("ch")||o.endsWith("x")||o.endsWith("z")||o.endsWith("o")&&!["photo","piano","halo"].includes(o)?s?r=e+"ES":r=e+"es":o.endsWith("y")&&!zt.test(o)?s?r=e.slice(0,-1)+"IES":r=e.slice(0,-1)+"ies":o.endsWith("f")?r=e.slice(0,-1)+"ves":o.endsWith("fe")?r=e.slice(0,-2)+"ves":s&&e.length===1?r=e+"s":s?r=e+"S":r=e+"s",r}function Mt(e){if(!e)return e;let t=e===e.toUpperCase(),s=e[0]===e[0]?.toUpperCase(),n=e.toLowerCase(),i={people:"person",children:"child",men:"man",women:"woman",teeth:"tooth",feet:"foot",mice:"mouse",geese:"goose",sheep:"sheep",deer:"deer",fish:"fish",series:"series",data:"datum",indices:"index"}[n];if(i)return t?i.toUpperCase():s&&i[0]?i[0].toUpperCase()+i.slice(1):i;let a=e;if(n.endsWith("ies")&&n.length>4)t?a=e.slice(0,-3)+"Y":a=e.slice(0,-3)+"y";else if(n.endsWith("ves")){let r=e.slice(0,-3);["kni","wi","li"].includes(r.toLowerCase())?a=r+"fe":a=r+"f";}else if(n.endsWith("sses"))a=e.slice(0,-2);else if(n.endsWith("zzes"))a=e.slice(0,-3);else if(n.endsWith("zes"))a=e.slice(0,-1);else if(n.endsWith("oes")||n.endsWith("xes")||n.endsWith("ches")||n.endsWith("shes"))a=e.slice(0,-2);else if(n.endsWith("ses")&&!n.endsWith("sses")){let r=e.slice(0,-2);["lyses","ases","ises","eses","oses"].some(c=>n.endsWith(c))?a=r+"is":a=r;}else n.endsWith("i")&&n.length>2&&!["ski","taxi","chi","hi"].includes(n)?a=e.slice(0,-1)+"us":n.endsWith("a")&&(n.endsWith("ta")||n.endsWith("ia"))&&n.length>3?a=e.slice(0,-1)+"on":n.endsWith("es")&&n.length>3&&!n.endsWith("ies")&&!n.endsWith("oes")?a=e.slice(0,-1):n==="as"?(a="a",t&&(a="A"),s&&(a="A")):n==="is"?(a="i",t&&(a="I"),s&&(a="I")):n.endsWith("s")&&!n.endsWith("ss")&&n.length>2&&(a=e.slice(0,-1));return a}function It(e,t={}){let{maxSize:s=100,getKey:n}=t,o=new Map,i=n||((...a)=>{if(a.length===0)return "";if(a.length===1){let r=a[0];if(r===null)return "__null__";if(r===void 0)return "__undefined__";if(typeof r=="string"||typeof r=="number"||typeof r=="boolean")return String(r)}try{return JSON.stringify(a)}catch{return a.map(String).join("|")}});return((...a)=>{let r=i(...a);if(o.has(r)){let u=o.get(r);return o.delete(r),o.set(r,u),u}let c=e(...a);if(o.size>=s){let u=o.keys().next().value;u!==void 0&&o.delete(u);}return o.set(r,c),c})}var Pt=/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b/g,Ot=/https?:\/\/[^\s<>"\{\}\|\\^\[\]`]+/g,wt=/@[a-zA-Z0-9_]+\b/g,$t=/#[a-zA-Z0-9_]+\b/g,Ht=/(?:\+?[1-9]\d{0,3})?[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}\b/g,vt=/\b(?:\d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}|\d{4}[-\/]\d{1,2}[-\/]\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* \d{1,2},? \d{4})\b/gi,Wt=/[$€£¥₹]\s?\d{1,3}(?:[,.\s]\d{3})*(?:[.,]\d{1,2})?(?:[kKmMbB])?(?!\w)/g,Dt=e=>{if(!e)return {emails:[],urls:[],mentions:[],hashtags:[],phones:[],dates:[],prices:[]};let t=[...new Set(e.match(Pt)||[])],s=[...new Set(e.match(Ot)||[])],n=[...new Set(e.match(wt)||[])],o=[...new Set(e.match($t)||[])],i=e.match(Ht)||[],a=[...new Set(i.map(p=>p.trim()).filter(p=>{let l=p.replace(/\D/g,"");return l.length>=7&&l.length<=15}))],r=e.match(vt)||[],c=[...new Set(r.filter(p=>!p.match(/^\d{1,4}$/)&&!p.match(/^\d{1,2}[-\/]\d{1,2}$/)))],u=[...new Set(e.match(Wt)||[])];return {emails:t,urls:s,mentions:n,hashtags:o,phones:a,dates:c,prices:u}};var jt=e=>{if(!e)return [];let t="\0",s="",n="",o="",i=e;i=i.replace(/\.{3}(\s+)([A-Z])/g,`${t}$1$2`),i=i.replace(/\.{3}/g,s),i=i.replace(/(\d)\.(\d)/g,`$1${n}$2`);let a=["Dr","Mr","Mrs","Ms","Prof","Sr","Jr","Ph\\.D","M\\.D","B\\.A","M\\.A","B\\.S","M\\.S","M\\.I\\.T","U\\.S","U\\.K","Inc","Ltd","Co","Corp"],c=["The","He","She","It","They","We","I","You","This","That","These","Those","There","Here"].join("|");a.forEach(l=>{let g=new RegExp(`\\b(${l})\\.(?!\\s+(${c})\\b)`,"gi");i=i.replace(g,`$1${o}`);});let u=[],p="";for(let l=0;l<i.length;l++){let g=i[l];if(p+=g,g==="."||g==="!"||g==="?"||g===t){let f=l+1,x="";for(;f<i.length;){let m=i[f];if(!m||m===" "||!/["\'\)\]]*/.test(m))break;x+=m,f++;}if(f<i.length&&i[f]===" "){let m=f+1;for(;m<i.length&&i[m]===" ";)m++;let d=i[m];if(d&&/[A-Z]/.test(d)){p+=x,u.push(p.trim()),p="",l=f;continue}}p+=x,l=f-1;}}return p.trim()&&u.push(p.trim()),u.map(l=>l.replace(new RegExp(t,"g"),"...").replace(new RegExp(s,"g"),"...").replace(new RegExp(n,"g"),".").replace(new RegExp(o,"g"),"."))};var Bt=(e,t={})=>{let{conjunction:s="and",oxford:n=true,quotes:o=false}=t,i=e.filter(l=>l!=null).map(l=>String(l)),a=o?i.map(l=>`"${l}"`):i,r=a.length;if(r===0)return "";if(r===1)return a[0];if(r===2)return `${a[0]} ${s} ${a[1]}`;let c=a.slice(0,-1),u=a[r-1],p=n?",":"";return `${c.join(", ")}${p} ${s} ${u}`};function kt(e){if(!e||e.length===0)return "unknown";let t={latin:0,cjk:0,arabic:0,cyrillic:0,hebrew:0,devanagari:0,greek:0,thai:0},s=Math.min(e.length,100),n=0;for(let a=0;a<s;a++){let r=e.charCodeAt(a),c=1;r>=65&&r<=90||r>=97&&r<=122||r>=192&&r<=255||r>=256&&r<=383||r>=384&&r<=591||r>=7680&&r<=7935?(t.latin+=c,n++):r>=19968&&r<=40959||r>=13312&&r<=19903||r>=12352&&r<=12447||r>=12448&&r<=12543||r>=12784&&r<=12799||r>=44032&&r<=55215||r>=4352&&r<=4607||r>=12592&&r<=12687?(c=2,t.cjk+=c,n++):r>=1536&&r<=1791||r>=1872&&r<=1919||r>=2208&&r<=2303||r>=64336&&r<=65023||r>=65136&&r<=65279?(c=2,t.arabic+=c,n++):r>=1024&&r<=1279||r>=1280&&r<=1327||r>=11744&&r<=11775||r>=42560&&r<=42655?(c=2,t.cyrillic+=c,n++):r>=1424&&r<=1535||r>=64285&&r<=64335?(c=2,t.hebrew+=c,n++):r>=2304&&r<=2431||r>=43232&&r<=43263?(c=2,t.devanagari+=c,n++):r>=880&&r<=1023||r>=7936&&r<=8191?(c=2,t.greek+=c,n++):r>=3584&&r<=3711&&(c=2,t.thai+=c,n++);}if(n===0)return "unknown";let o=0,i="unknown";for(let[a,r]of Object.entries(t))r>o&&(o=r,i=a);return i}function Ft(e){if(!e||typeof e!="string")return {type:"text",confidence:.5};let t=e.trim();if(!t)return {type:"text",confidence:.5};if(/^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i.test(t))return {type:"url",confidence:1};if(/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t))return {type:"email",confidence:1};if(t[0]==="{"||t[0]==="[")try{return JSON.parse(t),{type:"json",confidence:1}}catch{}if(/^[\+]?[(]?[0-9]{1,4}[)]?[-\s\.]?[(]?[0-9]{1,4}[)]?[-\s\.]?[0-9]{1,5}[-\s\.]?[0-9]{1,5}$/.test(t.replace(/\s/g,"")))return {type:"phone",confidence:.95};let n=/^(who|what|when|where|why|how|is|are|can|could|would|should|do|does|did|will|won't|isn't|aren't|wasn't|weren't|haven't|hasn't|hadn't|couldn't|wouldn't|shouldn't)[^.!]*\?$/i;if(n.test(t)||/\?$/.test(t))return {type:"question",confidence:t.match(n)?1:.8};let o=(t.match(/<[^>]+>/g)||[]).length,i=o/(t.split(/\s+/).length||1);if(o>0&&(i>.2||/^<!DOCTYPE|^<html/i.test(t)))return {type:"html",confidence:Math.min(.6+i*.4,.95)};let r=[/function\s+\w+\s*\(/,/const\s+\w+\s*=/,/let\s+\w+\s*=/,/var\s+\w+\s*=/,/if\s*\(/,/for\s*\(/,/while\s*\(/,/class\s+\w+/,/import\s+.*from/,/export\s+(default\s+)?/,/=>\s*{/,/\w+\s*:\s*\w+/,/[;{}()[\]]/].reduce((C,S)=>C+(S.test(t)?1:0),0),c=(t.match(/;/g)||[]).length>1,u=/[{}]/.test(t);if(r>2||r>0&&(c||u))return {type:"code",confidence:Math.min(.7+r*.05,.95)};let l=[/^#{1,6}\s/m,/\*\*[^*]+\*\*/,/\*[^*]+\*/,/\[[^\]]+\]\([^)]+\)/,/^[-*+]\s/m,/^>\s/m,/```[\s\S]*```/,/`[^`]+`/].reduce((C,S)=>C+(S.test(t)?1:0),0);if(l>0)return {type:"markdown",confidence:Math.min(.6+l*.1,.9)};let g=t.match(/\d+\.?\d*/g)||[],f=t.replace(/[\s,]/g,"").length,x=f>0?g.join("").length/f:0,m=/[+\-*/=%]/.test(t),d=/^[\d,.\s]+$/.test(t)&&g.length>2;return x>.5||x>.3&&m||d?{type:"numeric",confidence:Math.min(.6+x*.4,.9)}:{type:"text",confidence:.7}}function G(e,t={}){if(!e||typeof e!="string")return "";let{allowedTags:s=[],allowedAttributes:n=[],stripHtml:o=true,escapeHtml:i=false,removeScripts:a=true,removeNonPrintable:r=true,normalizeWhitespace:c=false,maxLength:u,removePatterns:p=[]}=t,l=e;for(let g of p)l=l.replace(g,"");if(a&&(l=l.replace(/(?:^|\s|[^=\s])(?:javascript|data|vbscript|mhtml|x-javascript|mocha|livescript):[^\s<>]*/gi,(g,f,x)=>{let m=x.substring(0,f),d=m.lastIndexOf("="),C=m.lastIndexOf(" "),S=m.lastIndexOf(">");return d>Math.max(S,-1)&&d>C?g:g.replace(/(?:javascript|data|vbscript|mhtml|x-javascript|mocha|livescript):[^\s<>]*/gi,"")})),(o||a)&&!(i&&!o)&&(a&&(l=l.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,""),l=l.replace(/<script\b[^>]*>.*$/gi,""),l=l.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi,"")),o)){if(s.length>0){let g=s.join("|"),f=new RegExp(`<(?!\\/?(?:${g})\\b)[^>]+>`,"gi");if(l=l.replace(f,""),a&&s.length>0&&(l=l.replace(/\son\w+\s*=\s*["'][^"']*["']/gi,""),l=l.replace(/\son\w+\s*=\s*[^\s>]*/gi,""),l=l.replace(/\s(href|src|action)\s*=\s*["'](?:javascript|data|vbscript):[^"']*["']/gi,""),l=l.replace(/\s(href|src|action)\s*=\s*(?:javascript|data|vbscript):[^\s>]*/gi,"")),n.length>0){let x=n.join("|"),m=new RegExp(`\\s(?!(?:${x})\\s*=)[a-zA-Z-]+\\s*=\\s*["'][^"']*["']`,"gi");l=l.replace(m,"");}}else l=l.replace(/<[^>]+>/g,"");l=l.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&#x27;/g,"'").replace(/&#x2F;/g,"/").replace(/&amp;/g,"&");}return i&&!o&&(l=l.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")),r&&(l=l.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g,"")),c&&(l=l.replace(/\s+/g," ").trim()),u&&u>0&&(l=l.slice(0,u)),l}function Vt(e,t={}){let{types:s=["ssn","creditCard","email","phone"],customPatterns:n=[],strategy:o="partial",partialLength:i=4}=t,a=e,r={ssn:{pattern:/\b\d{3}[-\s]?\d{2}[-\s]?\d{4}\b/g,replacer:c=>{if(o==="full")return "***-**-****";let p=c.replace(/[-\s]/g,"").slice(-i);return i>=4?`***-**-${p}`:i===3?`***-**-*${p}`:i===2?`***-**-**${p}`:`***-**-***${p}`}},creditCard:{pattern:/\b\d{4}[-\s]?\d{4,6}[-\s]?\d{4,5}[-\s]?\d{0,4}\b/g,replacer:c=>{if(o==="full")return "*".repeat(c.length);let u=c.replace(/[-\s]/g,""),p=u.slice(-i);if(c.includes("-")?"-":c.includes(" ")?" ":""){let g=Math.floor(u.length/4);return "**** ".repeat(g-1).trim()+` ${p}`}return "*".repeat(u.length-i)+p}},email:{pattern:/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g,replacer:c=>{if(o==="full")return "***@***.***";let u=c.split("@"),p=u[0],l=u[1];if(!p||!l)return c;let g=p.length===2?1:Math.min(p.length,3);return `${p.slice(0,g)+"***"}@${l}`}},phone:{pattern:/(?:\+?1[-.\s]?)?(\()?([0-9]{3})(\))?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})\b/g,replacer:c=>{let u=c.replace(/[^\d]/g,""),l=(u.length===11&&u.startsWith("1")?u.slice(1):u).slice(-i),g=c.includes("(")&&c.includes(")"),f=/\d\.\d/.test(c);if(o==="full")return g?"(***) ***-****":f?"***.***.****":c.includes("-")?"***-***-****":"**********";if(g)return `(***) ***-${l}`;if(f)return `***.***-${l}`;if(c.includes("-"))return `***-***-${l}`;{let x=10-i;return "*".repeat(x)+l}}}};for(let c of s){let u=r[c];if(u){let{pattern:p,replacer:l}=u;a=a.replace(p,l);}}for(let{pattern:c,replacement:u}of n)a=a.replace(c,u);return a}var A=class extends Error{constructor(t,s){super(`Invalid ${t}: "${s}"`),this.name="BrandedTypeError";}};var Zt=/^[a-z0-9]+(?:-[a-z0-9]+)*$/;function L(e){return $(e)}function R(e){return H(e)}function U(e){return Zt.test(e)}function N(e){return v(e)}function z(e){return W(e)}function _(e){return D(e)}function M(e){return j(e)}function Gt(e,t){if(!L(e))throw new A(t||"Email",e)}function Kt(e,t){if(!R(e))throw new A(t||"URL",e)}function Yt(e,t){if(!U(e))throw new A(t||"Slug",e)}function qt(e,t){if(!N(e))throw new A(t||"HexColor",e)}function Xt(e,t){if(!z(e))throw new A(t||"NumericString",e)}function Jt(e,t){if(!_(e))throw new A(t||"AlphanumericString",e)}function Qt(e,t){if(!M(e))throw new A(t||"UUID",e)}function en(e){return L(e)?e:null}function tn(e){return R(e)?e:null}function J(e){return O(e)}function nn(e){return e}function rn(e){return e}function sn(e){return e}function on(e){return U(e)?e:J(e)}function an(e,t){return G(e,{stripHtml:true,removeScripts:true,removeNonPrintable:true,...t})}function cn(e){return e}function ln(e){return N(e)?e:null}function un(e){return e}function pn(e){return z(e)?e:null}function fn(e){return e}function gn(e){return _(e)?e:null}function mn(e){return e}function hn(e){return M(e)?e:null}function dn(e){return e} exports.BrandedTypeError=A;exports.assertAlphanumericString=Jt;exports.assertEmail=Gt;exports.assertHexColor=qt;exports.assertNumericString=Xt;exports.assertSlug=Yt;exports.assertUUID=Qt;exports.assertUrl=Kt;exports.camelCase=ae;exports.capitalize=ie;exports.classifyText=Ft;exports.codePoints=ht;exports.constantCase=Je;exports.deburr=F;exports.detectScript=kt;exports.diff=Rt;exports.dotCase=ot;exports.ensureSlug=on;exports.escapeHtml=w;exports.excerpt=he;exports.extractEntities=Dt;exports.fuzzyMatch=Nt;exports.graphemes=b;exports.hashString=xe;exports.highlight=Lt;exports.humanizeList=Bt;exports.isASCII=dt;exports.isAlphanumeric=D;exports.isEmail=$;exports.isHexColor=v;exports.isNumeric=W;exports.isSlug=U;exports.isUUID=j;exports.isUrl=H;exports.isValidAlphanumeric=_;exports.isValidEmail=L;exports.isValidHexColor=N;exports.isValidNumeric=z;exports.isValidUUID=M;exports.isValidUrl=R;exports.kebabCase=le;exports.levenshtein=Z;exports.levenshteinNormalized=Ut;exports.memoize=It;exports.normalizeWhitespace=Tt;exports.pad=ve;exports.padEnd=De;exports.padStart=We;exports.pascalCase=Ce;exports.pathCase=mt;exports.pluralize=_t;exports.randomString=de;exports.redact=Vt;exports.removeNonPrintable=At;exports.reverse=Se;exports.sanitize=G;exports.sentenceCase=_e;exports.singularize=Mt;exports.slugify=O;exports.smartSplit=jt;exports.snakeCase=ce;exports.stripHtml=pe;exports.template=I;exports.templateSafe=He;exports.titleCase=Ve;exports.toASCII=bt;exports.toAlphanumericString=gn;exports.toEmail=en;exports.toHexColor=ln;exports.toNumericString=pn;exports.toSafeHTML=an;exports.toSlug=J;exports.toUUID=hn;exports.toUrl=tn;exports.truncate=se;exports.unsafeAlphanumericString=mn;exports.unsafeEmail=nn;exports.unsafeHexColor=un;exports.unsafeNumericString=fn;exports.unsafeSafeHTML=cn;exports.unsafeSlug=sn;exports.unsafeUUID=dn;exports.unsafeUrl=rn;exports.wordCount=we;//# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map