Home / File/ decode-arbitrary-value.ts — tailwindcss Source File

decode-arbitrary-value.ts — tailwindcss Source File

Architecture documentation for decode-arbitrary-value.ts, a typescript file in the tailwindcss codebase. 3 imports, 3 dependents.

File typescript Oxide Extractor 3 imports 3 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  7df4d001_f01e_1e05_13ee_91edb05bf1f8["decode-arbitrary-value.ts"]
  d9175aea_5971_a6c1_773d_004ce3789372["value-parser.ts"]
  7df4d001_f01e_1e05_13ee_91edb05bf1f8 --> d9175aea_5971_a6c1_773d_004ce3789372
  27cd040f_e3a9_f8b7_3c7c_0547b201eeb0["math-operators.ts"]
  7df4d001_f01e_1e05_13ee_91edb05bf1f8 --> 27cd040f_e3a9_f8b7_3c7c_0547b201eeb0
  6297163c_7d9c_1e1c_ddff_20f6098d38f6["addWhitespaceAroundMathOperators"]
  7df4d001_f01e_1e05_13ee_91edb05bf1f8 --> 6297163c_7d9c_1e1c_ddff_20f6098d38f6
  ba6fca27_7720_5839_0f92_bc2abb8db636["candidate.ts"]
  ba6fca27_7720_5839_0f92_bc2abb8db636 --> 7df4d001_f01e_1e05_13ee_91edb05bf1f8
  23a9c1fc_7b0d_cc80_077d_a315655e8821["decode-arbitrary-value.bench.ts"]
  23a9c1fc_7b0d_cc80_077d_a315655e8821 --> 7df4d001_f01e_1e05_13ee_91edb05bf1f8
  e2a619ea_5503_35b0_c537_dcd71ac1a4ab["decode-arbitrary-value.test.ts"]
  e2a619ea_5503_35b0_c537_dcd71ac1a4ab --> 7df4d001_f01e_1e05_13ee_91edb05bf1f8
  style 7df4d001_f01e_1e05_13ee_91edb05bf1f8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as ValueParser from '../value-parser'
import { addWhitespaceAroundMathOperators } from './math-operators'

export function decodeArbitraryValue(input: string): string {
  // There are definitely no functions in the input, so bail early
  if (input.indexOf('(') === -1) {
    return convertUnderscoresToWhitespace(input)
  }

  let ast = ValueParser.parse(input)
  recursivelyDecodeArbitraryValues(ast)
  input = ValueParser.toCss(ast)

  input = addWhitespaceAroundMathOperators(input)

  return input
}

/**
 * Convert `_` to ` `, except for escaped underscores `\_` they should be
 * converted to `_` instead.
 */
function convertUnderscoresToWhitespace(input: string, skipUnderscoreToSpace = false) {
  let output = ''
  for (let i = 0; i < input.length; i++) {
    let char = input[i]

    // Escaped underscore
    if (char === '\\' && input[i + 1] === '_') {
      output += '_'
      i += 1
    }

    // Unescaped underscore
    else if (char === '_' && !skipUnderscoreToSpace) {
      output += ' '
    }

    // All other characters
    else {
      output += char
    }
  }

  return output
}

function recursivelyDecodeArbitraryValues(ast: ValueParser.ValueAstNode[]) {
  for (let node of ast) {
    switch (node.kind) {
      case 'function': {
        if (node.value === 'url' || node.value.endsWith('_url')) {
          // Don't decode underscores in url() but do decode the function name
          node.value = convertUnderscoresToWhitespace(node.value)
          break
        }

        if (
          node.value === 'var' ||
          node.value.endsWith('_var') ||
          node.value === 'theme' ||
          node.value.endsWith('_theme')
        ) {
          node.value = convertUnderscoresToWhitespace(node.value)
          for (let i = 0; i < node.nodes.length; i++) {
            // Don't decode underscores to spaces in the first argument of var()
            if (i == 0 && node.nodes[i].kind === 'word') {
              node.nodes[i].value = convertUnderscoresToWhitespace(node.nodes[i].value, true)
              continue
            }
            recursivelyDecodeArbitraryValues([node.nodes[i]])
          }
          break
        }

        node.value = convertUnderscoresToWhitespace(node.value)
        recursivelyDecodeArbitraryValues(node.nodes)
        break
      }
      case 'separator':
      case 'word': {
        node.value = convertUnderscoresToWhitespace(node.value)
        break
      }
      default:
        never(node)
    }
  }
}

function never(value: never): never {
  throw new Error(`Unexpected value: ${value}`)
}

Domain

Subdomains

Frequently Asked Questions

What does decode-arbitrary-value.ts do?
decode-arbitrary-value.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the Oxide domain, Extractor subdomain.
What functions are defined in decode-arbitrary-value.ts?
decode-arbitrary-value.ts defines 4 function(s): convertUnderscoresToWhitespace, decodeArbitraryValue, never, recursivelyDecodeArbitraryValues.
What does decode-arbitrary-value.ts depend on?
decode-arbitrary-value.ts imports 3 module(s): addWhitespaceAroundMathOperators, math-operators.ts, value-parser.ts.
What files import decode-arbitrary-value.ts?
decode-arbitrary-value.ts is imported by 3 file(s): candidate.ts, decode-arbitrary-value.bench.ts, decode-arbitrary-value.test.ts.
Where is decode-arbitrary-value.ts in the architecture?
decode-arbitrary-value.ts is located at packages/tailwindcss/src/utils/decode-arbitrary-value.ts (domain: Oxide, subdomain: Extractor, directory: packages/tailwindcss/src/utils).

Analyze Your Own Codebase

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

Try Supermodel Free