Home / Function/ tryValueReplacements() — tailwindcss Function Reference

tryValueReplacements() — tailwindcss Function Reference

Architecture documentation for the tryValueReplacements() function in canonicalize-candidates.ts from the tailwindcss codebase.

Function typescript Oxide Extractor calls 3 called by 1

Entity Profile

Dependency Diagram

graph TD
  9d7dff21_77af_5de5_322b_3d9047c15d83["tryValueReplacements()"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  9d7dff21_77af_5de5_322b_3d9047c15d83 -->|defined in| f6c14bbb_2e42_58cc_18f1_c89a243da9c0
  a23fd354_f688_0a39_de4f_3c403164704a["arbitraryValueToBareValueUtility()"]
  a23fd354_f688_0a39_de4f_3c403164704a -->|calls| 9d7dff21_77af_5de5_322b_3d9047c15d83
  e7db6358_7af5_e4b2_792d_749691a304cc["add()"]
  9d7dff21_77af_5de5_322b_3d9047c15d83 -->|calls| e7db6358_7af5_e4b2_792d_749691a304cc
  09df19e7_0346_145d_40c4_e93ff14a2ff9["isValidSpacingMultiplier()"]
  9d7dff21_77af_5de5_322b_3d9047c15d83 -->|calls| 09df19e7_0346_145d_40c4_e93ff14a2ff9
  2b202e81_c79a_bf7e_74f9_fe9a98bbea87["isPositiveInteger()"]
  9d7dff21_77af_5de5_322b_3d9047c15d83 -->|calls| 2b202e81_c79a_bf7e_74f9_fe9a98bbea87
  style 9d7dff21_77af_5de5_322b_3d9047c15d83 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 1470–1527

function* tryValueReplacements(
  candidate: Extract<Candidate, { kind: 'functional' }>,
  value: string = candidate.value?.value ?? '',
  seen: Set<string> = new Set(),
): Generator<NamedUtilityValue> {
  if (seen.has(value)) return
  seen.add(value)

  // 0. Just try to drop the square brackets and see if it works
  // 1. A number (with increments of .25)
  yield {
    kind: 'named',
    value,
    fraction: null,
  }

  // 2. A percentage (with increments of .25 followed by a `%`)
  //    Try to drop the `%` and see if it works
  if (value.endsWith('%') && isValidSpacingMultiplier(value.slice(0, -1))) {
    yield {
      kind: 'named',
      value: value.slice(0, -1),
      fraction: null,
    }
  }

  // 3. A ratio with whole numbers
  if (value.includes('/')) {
    let [numerator, denominator] = value.split('/')
    if (isPositiveInteger(numerator) && isPositiveInteger(denominator)) {
      yield {
        kind: 'named',
        value: numerator,
        fraction: `${numerator}/${denominator}`,
      }
    }
  }

  // It could also be that we have `20px`, we can try just `20` and see if it
  // results in the same signature.
  let allNumbersAndFractions = new Set<string>()

  // Figure out all numbers and fractions in the value
  for (let match of value.matchAll(/(\d+\/\d+)|(\d+\.?\d+)/g)) {
    allNumbersAndFractions.add(match[0].trim())
  }

  // Sort the numbers and fractions where the smallest length comes first. This
  // will result in the smallest replacement.
  let options = Array.from(allNumbersAndFractions).sort((a, z) => {
    return a.length - z.length
  })

  // Try all the options
  for (let option of options) {
    yield* tryValueReplacements(candidate, option, seen)
  }
}

Domain

Subdomains

Frequently Asked Questions

What does tryValueReplacements() do?
tryValueReplacements() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is tryValueReplacements() defined?
tryValueReplacements() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 1470.
What does tryValueReplacements() call?
tryValueReplacements() calls 3 function(s): add, isPositiveInteger, isValidSpacingMultiplier.
What calls tryValueReplacements()?
tryValueReplacements() is called by 1 function(s): arbitraryValueToBareValueUtility.

Analyze Your Own Codebase

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

Try Supermodel Free