Skip to content

Commit

Permalink
optimized mergeClassList
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev committed Jul 28, 2024
1 parent b901f7f commit 80e6af4
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/lib/merge-classlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,16 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
* @example 'md:!pr'
*/
const classGroupsInConflict: string[] = []
// const classGroupsInConflict = new Set<string>()

// let result: string[] = []
let result = ''

let currentClass = ''
for (let i = classList.length - 1; i >= 0; --i) {
const char = classList[i]!
// more performant than Regex check - suitable for our case
const isSpace = char === ' '
if (!isSpace) {
currentClass = char + currentClass
if (i !== 0) continue
} else if (currentClass.length === 0) {
continue
for (let i = classList.length - 1; i >= 0; ) {
while (classList[i] === ' ') {
--i
}

const originalClassName = currentClass
currentClass = ''
const nextI = classList.lastIndexOf(' ', i)
const originalClassName = classList.slice(nextI === -1 ? 0 : nextI + 1, i + 1)
i = nextI

const { modifiers, hasImportantModifier, baseClassName, maybePostfixModifierPosition } =
parseClassName(originalClassName)
Expand All @@ -45,15 +36,13 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
if (!classGroupId) {
if (!hasPostfixModifier) {
result = originalClassName + (result.length > 0 ? ' ' + result : result)
// result.push(originalClassName)
continue
}

classGroupId = getClassGroupId(baseClassName)

if (!classGroupId) {
result = originalClassName + (result.length > 0 ? ' ' + result : result)
// result.push(originalClassName)
continue
}

Expand All @@ -80,10 +69,8 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
classGroupsInConflict.push(modifierId + group)
}

// result.push(originalClassName)
result = originalClassName + (result.length > 0 ? ' ' + result : result)
}

return result
// return result.join(' ')
}

0 comments on commit 80e6af4

Please sign in to comment.