Home / Function/ compareBreakpoints() — tailwindcss Function Reference

compareBreakpoints() — tailwindcss Function Reference

Architecture documentation for the compareBreakpoints() function in compare-breakpoints.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  0015fc82_ad3f_130f_e7fe_1e92f2f624a1["compareBreakpoints()"]
  51f244f2_138c_0d69_05db_b2df310d0f23["compare-breakpoints.ts"]
  0015fc82_ad3f_130f_e7fe_1e92f2f624a1 -->|defined in| 51f244f2_138c_0d69_05db_b2df310d0f23
  bd84428d_2ac4_b86f_d49f_6939ec1e5898["buildCustomContainerUtilityRules()"]
  bd84428d_2ac4_b86f_d49f_6939ec1e5898 -->|calls| 0015fc82_ad3f_130f_e7fe_1e92f2f624a1
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66["createVariants()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 0015fc82_ad3f_130f_e7fe_1e92f2f624a1
  style 0015fc82_ad3f_130f_e7fe_1e92f2f624a1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/utils/compare-breakpoints.ts lines 1–48

export function compareBreakpoints(a: string, z: string, direction: 'asc' | 'desc') {
  if (a === z) return 0

  // Assumption: when a `(` exists, we are dealing with a CSS function.
  //
  // E.g.: `calc(100% - 1rem)`
  let aIsCssFunction = a.indexOf('(')
  let zIsCssFunction = z.indexOf('(')

  let aBucket =
    aIsCssFunction === -1
      ? // No CSS function found, bucket by unit instead
        a.replace(/[\d.]+/g, '')
      : // CSS function found, bucket by function name
        a.slice(0, aIsCssFunction)

  let zBucket =
    zIsCssFunction === -1
      ? // No CSS function found, bucket by unit
        z.replace(/[\d.]+/g, '')
      : // CSS function found, bucket by function name
        z.slice(0, zIsCssFunction)

  let order =
    // Compare by bucket name
    (aBucket === zBucket ? 0 : aBucket < zBucket ? -1 : 1) ||
    // If bucket names are the same, compare by value
    (direction === 'asc' ? parseInt(a) - parseInt(z) : parseInt(z) - parseInt(a))

  // If the groups are the same, and the contents are not numbers, the
  // `order` will result in `NaN`. In this case, we want to make sorting
  // stable by falling back to a string comparison.
  //
  // This can happen when using CSS functions such as `calc`.
  //
  // E.g.:
  //
  // - `min-[calc(100%-1rem)]` and `min-[calc(100%-2rem)]`
  // - `@[calc(100%-1rem)]` and `@[calc(100%-2rem)]`
  //
  // In this scenario, we want to alphabetically sort `calc(100%-1rem)` and
  // `calc(100%-2rem)` to make it deterministic.
  if (Number.isNaN(order)) {
    return a < z ? -1 : 1
  }

  return order
}

Domain

Subdomains

Frequently Asked Questions

What does compareBreakpoints() do?
compareBreakpoints() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/utils/compare-breakpoints.ts.
Where is compareBreakpoints() defined?
compareBreakpoints() is defined in packages/tailwindcss/src/utils/compare-breakpoints.ts at line 1.
What calls compareBreakpoints()?
compareBreakpoints() is called by 2 function(s): buildCustomContainerUtilityRules, createVariants.

Analyze Your Own Codebase

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

Try Supermodel Free