UNPKG

unocss-merge

Version:

simple utility to merge unocss class names

1 lines 8.77 kB
import{clsx as e}from"clsx";function t(e){return[...new Set(e)]}function n(e,t){if(!e)throw typeof t==`string`?Error(t):t}function r(...e){return e.flat(1/0).map(e=>e?.toString()).filter(Boolean).join(` `)}function i(e,t){let n={"(":`)`,"{":`}`,"[":`]`},r=e[t],i=n[r];if(!i)return-1;let a=1;for(let n=t+1,o=e.length;n<o;n++){let t=e[n];if(t===i){if(a--,a===0)return n}else t===r&&a++}return-1}function a(e){let t=/^(?:(?:[\w-]+|\[\S+\]):)+/.exec(e),n;if(t){let r=t[0];e=e.slice(r.length);let a=[];for(;r;){if(/^[\w-]+:/.test(r)){let e=r.slice(0,r.indexOf(`:`)+1);a.push(e),r=r.slice(e.length);continue}if(/^\[\S+\]:/.test(r)){let e=i(r,0);if(e!==-1){let t=r.slice(0,e+2);a.push(t),r=r.slice(e+2);continue}}let e=r.indexOf(`:`);if(e===-1){let e=r;a.push(e),r=``}else{let t=r.slice(0,e+1);a.push(t),r=r.slice(e+1)}}n=a.filter(Boolean).sort().join(``)}return{cls:e,variantsPrefix:n}}const o=String.raw,s=(e,...t)=>new RegExp(o(e,...t)),c=o`(\d+(\.\d+)?)?(xs|sm|md|lg|xl)`,l=`(solid|dashed|dotted|double|none|hidden)`;function u(e,t){return n(e.endsWith(`-`),"prefix must end with `-`"),t.map(t=>e+t)}const d=[[[...[`block`,`flex`,`grid`,`table`].flatMap(e=>[e,`inline-${e}`]),`inline`,`flow-root`,`contents`,`list-item`,`hidden`,...u(`table-`,[`row`,`column`,`cell`,`caption`,`row-group`,`column-group`,`header-group`,`footer-group`])],`display`],[[`isolate`,`isolation-auto`],`isolation`],[[`static`,`fixed`,`absolute`,`relative`,`sticky`],`position`],[[`visible`,`invisible`,`collapse`],`visibility`],[u(`flex-`,[`row`,`row-reverse`,`col`,`col-reverse`]),`flex-direction`],[u(`flex-`,[`wrap`,`wrap-reverse`,`nowrap`]),`flex-wrap`],[u(`object-`,[`contain`,`cover`,`fill`,`none`,`scale-down`]),`object-fit`],[u(`object-`,[`bottom`,`center`,`left`,`left-bottom`,`left-top`,`right`,`right-bottom`,`right-top`,`top`]),`object-position`],[[`antialiased`,`subpixel-antialiased`],`font-smoothing`],[[`italic`,`not-italic`,`oblique`,`not-oblique`],`font-style`],[u(`font-`,[`mono`,`sans`,`serif`]),`font-family`],[/^(?:font|fw)-?(thin|extralight|light|normal|medium|semibold|bold|extrabold|black|\d+)$/,`font-weight`],[[`normal-nums`,`ordinal`,`slashed-zero`,`lining-nums`,`oldstyle-nums`,`proportional-nums`,`tabular-nums`,`diagonal-fractions`,`stacked-fractions`],`font-variant-numeric`],[s`^(text|text-size|font-size)-(\d+|${c}$)`,`font-size`],[/^(leading|line-height)-/,`line-height`],[u(`list-`,[`inside`,`outside`]),`list-style-position`],[u(`list-`,[`none`,`disc`,`decimal`]),`list-style-type`],[u(`text-`,[`left`,`center`,`right`,`justify`,`start`,`end`]),`text-align`],[[`underline`,`overline`,`line-through`,`no-underline`],`text-decoration-line`],[u(`decoration-`,[`solid`,`double`,`dotted`,`dashed`,`wavy`]),`text-decoration-style`],[/^decoration-(\d+|(auto|from-font)$)/,`text-decoration-thickness`],[[`truncate`,`text-ellipsis`,`text-clip`],`text-overflow`],[u(`text-`,[`wrap`,`nowrap`,`balance`,`pretty`]),`text-wrap`],[u(`bg-`,[`bottom`,`center`,`left`,`left-bottom`,`left-top`,`right`,`right-bottom`,`right-top`,`top`]),`background-position`],[/^bg(-no)?-repeat($|-)/,`background-repeat`],[u(`bg-`,[`auto`,`cover`,`contain`]),`background-size`],[[`bg-none`,/^bg-gradient-to-/],`background-image`],[/^(?:border-|b-)?(?:rounded|rd)(?:-(.+))?$/,`rounded`],[s`^b(?:order)?-${l}$`,`border-style`],[s`^b(?:order)?-([tblrxyse])-${l}$`,(e,t)=>`border-${t?.[1]}-style`],[/^b(?:order)?($|-\d+)/,`border-width`],[/^b(?:order)?-([tblrxyse])($|-\d+)/,(e,t)=>`border-${t?.[1]}-width`],[/^(?:border|b)-(?:color-)?(.+)$/,`border-color`],[/^(?:border|b)-([rltbse])-(?:color-)?(.+)$/,(e,t)=>`border-${t?.[1]}-color`],[/^outline-\d+/,`outline-width`],[[`outline`,s`^outline-${l}$`],`outline-style`],[s`^divide-${l}$`,`divide-style`],[/^divide-(x|y)($|-\d+)/,(e,t)=>`divide-${t?.[1]}-width`],[[`ring`,/^ring-\d+/],`ring-width`],[[/^ring-offset-\d+/],`ring-offset-width`],[[`shadow`,...u(`shadow-`,[`inner`,`none`]),s`^shadow-${c}$`],`box-shadow`],[u(`border-`,[`collapse`,`separate`]),`border-collapse`],[u(`table-`,[`auto`,`fixed`]),`table-layout`],[u(`caption-`,[`top`,`bottom`]),`caption-side`],[u(`scroll-`,[`auto`,`smooth`]),`scroll-behavior`],[u(`snap-`,[`start`,`end`,`center`,`align-none`]),`scroll-snap-align`],[u(`snap-`,[`normal`,`always`]),`scroll-snap-stop`],[u(`snap-`,[`none`,`x`,`y`,`both`,`mandatory`,`proximity`]),`scroll-snap-type`],[/^stroke-\d+/,`stroke-width`],[[`sr-only`,`not-sr-only`],`Screen-Readers`],[/^(flex-)?grow($|-\d+$)/,`flex-grow`],[/^(flex-)?shrink($|-\d+$)/,`flex-shrink`],[/^(flex-)?basis-(.+)$/,`flex-basis`],[/^flex-(initial|auto|none|\d+)$/,`flex`],[/^col-start-/,`grid-column-start`],[/^col-end-/,`grid-column-end`],[[/^col-/,/^col-span-/],`grid-column`],[/^row-start-/,`grid-row-start`],[/^row-end-/,`grid-row-end`],[[/^row-/,/^row-span-/],`grid-row`],[`transition-none`,`transition-none`],[/^transition-delay-(.+)$/,`transition-delay`],[/^transition-ease(?:-(.+))?$/,`transition-timing-function`],[/^transition(-duration)?-\d+(m?s)?$/,`transition-duration`],[/^transition-(all$|\w+)/,`transition-property`],...[`ring-inset`,`divide-x-reverse`,`divide-y-reverse`].map(e=>[e,e]),...[`resize`,`blur`,`drop-shadow`,`grayscale`,`invert`,`sepia`,`backdrop-blur`,`backdrop-grayscale`,`backdrop-invert`,`backdrop-sepia`].map(e=>[s`^${e}($|-)`,e])],f=new Map,p=new Map;for(let[e,t]of d){let n=[e].flat(),r=n.filter(e=>typeof e==`string`),i=n.filter(e=>typeof e==`object`&&e instanceof RegExp);r.forEach(e=>{let n=typeof t==`string`?t:t(e);f.set(e,n)}),i.forEach(e=>{p.set(e,t)})}function m(e){if(f.has(e))return f.get(e);for(let[t,n]of p.entries())if(t.test(e)){let r=t.exec(e);return typeof n==`string`?n:n(e,r)}}function h(e){return g.map(e=>typeof e==`string`?[e,e]:e).find(([t,n])=>e.startsWith(`${t}-`))}const g=[`break-after`,`break-before`,`break-inside`,`grid-flow`,`align`,`whitespace`,`mix-blend`,`bg-blend`,`ease`,`origin`,`cursor`,`touch`,`color`,[`text`,`color`],[`c`,`color`],[`bg`,`background-color`],[`decoration`,`text-decoration-color`],[`from`,`gradient-color-stops`],[`divide`,`divide-color`],[`outline`,`outline-color`],[`ring`,`ring-color`],[`ring-offset`,`ring-offset-color`],[`shadow`,`box-shadow-color`],[`accent`,`accent-color`],[`caret`,`caret-color`],`fill`,`stroke`],_=new Map(Object.entries({flex:[`flex-grow`,`flex-shrink`,`flex-basis`],"transition-none":[`transition-duration`,`transition-delay`,`transition-timing-function`,`transition-property`],"transition-property":[`transition-none`],m:[`mt`,`mb`,`ml`,`mr`,`mx`,`my`,`ms`,`me`,`mbs`,`mbe`],mx:[`ml`,`mr`,`ms`,`me`],my:[`mt`,`mb`,`mbs`,`mbe`],p:[`pt`,`pb`,`pl`,`pr`,`px`,`py`,`ps`,`pe`,`pbs`,`pbe`],px:[`pl`,`pr`,`ps`,`pe`],py:[`pt`,`pb`,`pbs`,`pbe`],inset:[`top`,`right`,`bottom`,`left`,`start`,`end`,`inset-x`,`inset-y`],"inset-x":[`left`,`right`,`start`,`end`],"inset-y":[`top`,`bottom`],rounded:[`rounded-t`,`rounded-r`,`rounded-b`,`rounded-l`,`rounded-s`,`rounded-e`,`rounded-tl`,`rounded-tr`,`rounded-br`,`rounded-bl`,`rounded-ss`,`rounded-se`,`rounded-es`,`rounded-ee`],"rounded-s":[`rounded-ss`,`rounded-es`],"rounded-e":[`rounded-se`,`rounded-ee`],"border-width":[`border-x-width`,`border-y-width`,`border-t-width`,`border-r-width`,`border-b-width`,`border-l-width`,`border-s-width`,`border-e-width`],"border-x-width":[`border-l-width`,`border-r-width`,`border-s-width`,`border-e-width`],"border-y-width":[`border-t-width`,`border-b-width`],gap:[`gap-x`,`gap-y`],size:[`w`,`h`],"scroll-m":[`scroll-mx`,`scroll-my`,`scroll-ms`,`scroll-me`,`scroll-mt`,`scroll-mr`,`scroll-mb`,`scroll-ml`],"scroll-p":[`scroll-px`,`scroll-py`,`scroll-ps`,`scroll-pe`,`scroll-pt`,`scroll-pr`,`scroll-pb`,`scroll-pl`]}));function v(e){return(e||``).split(` `).map(e=>e.trim()).filter(Boolean)}function y(...n){let r=new Map;return v(e(...n)).filter(Boolean).forEach(e=>b(e,r)),t(Array.from(r.values())).join(` `)}function b(e,t){let n=e,r;function i(e){[e].flat().forEach(e=>{_.has(e)&&[_.get(e)].flat().filter(e=>e!==void 0).forEach(e=>{t.delete(r?r+e:e)}),t.set(r?r+e:e,n)})}function o(){if(!(e.at(0)===`[`&&e.at(-1)===`]`))return;let t=e.slice(1,-1);if(!t)return;let[n,r,...a]=t.split(`:`).map(e=>e.trim()).filter(Boolean);if(!(a.length||!n||!r||!/^[\w-]+$/.test(n)||!/^[\w-]+$/.test(r)))return i(n),!0}function s(){let t=m(e);if(t)return i(t),!0}function c(){let t=h(e);if(!t)return;let[n,r]=t;return i(r),!0}function l(){let t=e,n=/(\[[\w,()/-]+\])$/;n.test(e)&&(t=e.replace(n,function(e,t){return`*`.repeat(t.length)})),t=t.replaceAll(/--(?=\d)/g,`-*`);let r=t.lastIndexOf(`-`);if(r!==-1)return i(e.slice(0,r)),!0}function u(){return i(e)}function d(){if(e.startsWith(`!`)){e=`important:`+e.slice(1);return}if(e.endsWith(`!`)){e=`important:`+e.slice(0,-1);return}}o()||s()||c()||(d(),{cls:e,variantsPrefix:r}=a(e),o()||s()||c()||l()||u())}export{y as n,r,v as t};