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
  f3e1eba5_3ac5_6422_9f8f_ceda1ec5ffc7["compareBreakpoints()"]
  f9776eb1_2e39_1c04_0c09_5a1f1eec4204["buildCustomContainerUtilityRules()"]
  f9776eb1_2e39_1c04_0c09_5a1f1eec4204 -->|calls| f3e1eba5_3ac5_6422_9f8f_ceda1ec5ffc7
  7c96535c_85cb_d6bf_efe4_875fba595c4f["createVariants()"]
  7c96535c_85cb_d6bf_efe4_875fba595c4f -->|calls| f3e1eba5_3ac5_6422_9f8f_ceda1ec5ffc7
  style f3e1eba5_3ac5_6422_9f8f_ceda1ec5ffc7 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
}

Subdomains

Frequently Asked Questions

What does compareBreakpoints() do?
compareBreakpoints() is a function in the tailwindcss codebase.
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