Home / Function/ compare() — tailwindcss Function Reference

compare() — tailwindcss Function Reference

Architecture documentation for the compare() function in variants.ts from the tailwindcss codebase.

Function typescript OxideEngine Scanner calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  7ab199f0_ea5e_0bae_2efa_8b253a0f26bd["compare()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa["Variants"]
  7ab199f0_ea5e_0bae_2efa_8b253a0f26bd -->|defined in| ff716ee9_bd1e_1568_4100_c1d5b3ab8daa
  9b965fd7_d8e9_0b43_cd5d_c9294ab598ed["buildDesignSystem()"]
  9b965fd7_d8e9_0b43_cd5d_c9294ab598ed -->|calls| 7ab199f0_ea5e_0bae_2efa_8b253a0f26bd
  9b8bd17a_1b05_f86d_02dc_1f5d07860201["get()"]
  7ab199f0_ea5e_0bae_2efa_8b253a0f26bd -->|calls| 9b8bd17a_1b05_f86d_02dc_1f5d07860201
  style 7ab199f0_ea5e_0bae_2efa_8b253a0f26bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/variants.ts lines 203–270

  compare(a: Variant | null, z: Variant | null): number {
    if (a === z) return 0
    if (a === null) return -1
    if (z === null) return 1

    if (a.kind === 'arbitrary' && z.kind === 'arbitrary') {
      // SAFETY: The selectors don't need to be checked for equality as they
      // are guaranteed to be unique since we sort a list of de-duped variants
      return a.selector < z.selector ? -1 : 1
    } else if (a.kind === 'arbitrary') {
      return 1
    } else if (z.kind === 'arbitrary') {
      return -1
    }

    let aOrder = this.variants.get(a.root)!.order
    let zOrder = this.variants.get(z.root)!.order

    let orderedByVariant = aOrder - zOrder
    if (orderedByVariant !== 0) return orderedByVariant

    if (a.kind === 'compound' && z.kind === 'compound') {
      let order = this.compare(a.variant, z.variant)
      if (order !== 0) return order

      if (a.modifier && z.modifier) {
        // SAFETY: The modifiers don't need to be checked for equality as they
        // are guaranteed to be unique since we sort a list of de-duped variants
        return a.modifier.value < z.modifier.value ? -1 : 1
      } else if (a.modifier) {
        return 1
      } else if (z.modifier) {
        return -1
      } else {
        return 0
      }
    }

    let compareFn = this.compareFns.get(aOrder)
    if (compareFn !== undefined) return compareFn(a, z)

    if (a.root !== z.root) return a.root < z.root ? -1 : 1

    // SAFETY: Variants `a` and `z` are both functional at this point. Static
    // variants are de-duped by the `DefaultMap` and checked earlier.
    let aValue = (a as Extract<Variant, { kind: 'functional' }>).value
    let zValue = (z as Extract<Variant, { kind: 'functional' }>).value

    // While no functional variant in core supports a "default" value the parser
    // will see something like `data:flex` and still parse and store it as a
    // functional variant even though it actually produces no CSS. This means
    // that we need to handle the case where the value is `null` here. Even
    // though _for valid utilities_ this never happens.
    if (aValue === null) return -1
    if (zValue === null) return 1

    // Variants with arbitrary values should appear after any with named values
    if (aValue.kind === 'arbitrary' && zValue.kind !== 'arbitrary') return 1
    if (aValue.kind !== 'arbitrary' && zValue.kind === 'arbitrary') return -1

    // SAFETY: The values don't need to be checked for equality as they are
    // guaranteed to be unique since we sort a list of de-duped variants. The
    // only way this could matter would be when two different variants parse to
    // the same AST. That is only possible with arbitrary values when spaces are
    // involved. e.g. `data-[a_b]:flex` and `data-[a ]:flex` but this is not a
    // concern for us because spaces are not allowed in variant names.
    return aValue.value < zValue.value ? -1 : 1
  }

Domain

Subdomains

Calls

Frequently Asked Questions

What does compare() do?
compare() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/variants.ts.
Where is compare() defined?
compare() is defined in packages/tailwindcss/src/variants.ts at line 203.
What does compare() call?
compare() calls 1 function(s): get.
What calls compare()?
compare() is called by 1 function(s): buildDesignSystem.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free