* Apply a single update to an array of class strings. * Returns the modified array.
(classes: string[], update: ClassNameUpdate)
| 303 | * Returns the modified array. |
| 304 | */ |
| 305 | function applyUpdate(classes: string[], update: ClassNameUpdate): string[] { |
| 306 | const newClass = buildClass(update); |
| 307 | const result = [...classes]; |
| 308 | |
| 309 | // 1. Check relatedPrefixes for shorthand splitting |
| 310 | for (const relatedPrefix of update.relatedPrefixes ?? []) { |
| 311 | const existingIdx = result.findIndex((c) => |
| 312 | classMatchesPrefix(c, relatedPrefix) |
| 313 | ); |
| 314 | if (existingIdx === -1) continue; |
| 315 | |
| 316 | const existingCls = result[existingIdx]; |
| 317 | const split = SHORTHAND_SPLITS[relatedPrefix]; |
| 318 | if (!split) continue; |
| 319 | |
| 320 | const token = split.extractToken(existingCls); |
| 321 | // Remove the shorthand class |
| 322 | result.splice(existingIdx, 1); |
| 323 | // Insert individual side classes, replacing the edited side with the new value |
| 324 | const expansions: string[] = []; |
| 325 | for (const side of split.sides) { |
| 326 | if (side === update.tailwindPrefix) { |
| 327 | expansions.push(newClass); |
| 328 | } else { |
| 329 | expansions.push(token === "DEFAULT" ? side : `${side}-${token}`); |
| 330 | } |
| 331 | } |
| 332 | result.splice(existingIdx, 0, ...expansions); |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | // 2. Find and replace existing class with same prefix |
| 337 | const directIdx = result.findIndex((c) => |
| 338 | update.classPattern |
| 339 | ? new RegExp(update.classPattern).test(c) |
| 340 | : classMatchesPrefix(c, update.tailwindPrefix) |
| 341 | ); |
| 342 | |
| 343 | if (directIdx !== -1) { |
| 344 | result[directIdx] = newClass; |
| 345 | } else { |
| 346 | result.push(newClass); |
| 347 | } |
| 348 | |
| 349 | return result; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Apply updates to a class string (space-separated list of classes). |
no test coverage detected