Value Transformers

Value transformers allow you to transform prompt and variable values before they’re used in your configurations. This enables powerful data manipulation and validation workflows.

Table of Contents


Overview

Transformers are applied to:

Key Features


Built-in Transformers

Scaffoldfy provides these built-in transformers:

lowercase

Converts the value to lowercase.

{
  "id": "email",
  "message": "Enter your email",
  "type": "input",
  "transformers": ["lowercase"]
}

uppercase

Converts the value to uppercase.

{
  "id": "constantName",
  "message": "Enter constant name",
  "type": "input",
  "transformers": ["uppercase"]
}

trim

Removes leading and trailing whitespace.

{
  "id": "username",
  "message": "Enter username",
  "type": "input",
  "transformers": ["trim"]
}

slugify

Converts a string to a URL-friendly slug (lowercase, spaces to hyphens, special chars removed).

{
  "id": "projectName",
  "message": "Enter project name",
  "type": "input",
  "transformers": ["slugify"]
}

Example:

capitalize

Capitalizes the first letter of the string.

{
  "id": "title",
  "message": "Enter title",
  "type": "input",
  "transformers": ["capitalize"]
}

Example:

titlecase

Converts the string to title case (capitalizes the first letter of each word with spaces). This transformer properly handles all input formats (camelCase, kebab-case, snake_case, etc.) and converts them to a properly formatted title.

{
  "id": "headline",
  "message": "Enter headline",
  "type": "input",
  "transformers": ["titlecase"]
}

Examples:

This is the recommended transformer when you want to convert any format to a human-readable title with proper capitalization and spacing.

capitalCase

Converts the string to capital case (capitalizes the first letter of each word).

{
  "id": "headline",
  "message": "Enter headline",
  "type": "input",
  "transformers": ["capitalCase"]
}

Example:

camelcase

Converts the string to camelCase.

{
  "id": "variableName",
  "message": "Enter variable name",
  "type": "input",
  "transformers": ["camelcase"]
}

Example:

pascalcase

Converts the string to PascalCase.

{
  "id": "className",
  "message": "Enter class name",
  "type": "input",
  "transformers": ["pascalcase"]
}

Example:

snakecase

Converts the string to snake_case.

{
  "id": "fileName",
  "message": "Enter file name",
  "type": "input",
  "transformers": ["snakecase"]
}

Example:

kebabcase

Converts the string to kebab-case.

{
  "id": "cssClass",
  "message": "Enter CSS class",
  "type": "input",
  "transformers": ["kebabcase"]
}

Example:

constantcase

Converts the string to CONSTANT_CASE.

{
  "id": "envVar",
  "message": "Enter environment variable",
  "type": "input",
  "transformers": ["constantcase"]
}

Example:

alphanumeric

Removes all non-alphanumeric characters from the string.

{
  "id": "cleanId",
  "message": "Enter identifier",
  "type": "input",
  "transformers": ["alphanumeric"]
}

Example:

collapse-spaces

Collapses multiple consecutive spaces into a single space.

{
  "id": "description",
  "message": "Enter description",
  "type": "input",
  "transformers": ["collapse-spaces"]
}

Example:

remove-spaces

Removes all spaces from the string.

{
  "id": "compactName",
  "message": "Enter compact name",
  "type": "input",
  "transformers": ["remove-spaces"]
}

Example:

urlencode

URL-encodes the string.

{
  "id": "encodedUrl",
  "message": "Enter URL",
  "type": "input",
  "transformers": ["urlencode"]
}

Example:

dasherize

Converts the string to dash-separated format (similar to kebab-case but may handle special cases).

{
  "id": "dashedName",
  "message": "Enter dashed name",
  "type": "input",
  "transformers": ["dasherize"]
}

Example:

underscore

Converts the string to underscore-separated format (similar to snake_case but may handle special cases).

{
  "id": "underscoredName",
  "message": "Enter underscored name",
  "type": "input",
  "transformers": ["underscore"]
}

Example:

adaCase

Converts the string to Ada_Case format (capitalized words separated by underscores).

{
  "id": "adaName",
  "message": "Enter Ada-style name",
  "type": "input",
  "transformers": ["adaCase"]
}

Example:

cobolCase

Converts the string to COBOL-CASE format (uppercase words separated by hyphens).

{
  "id": "cobolName",
  "message": "Enter COBOL-style name",
  "type": "input",
  "transformers": ["cobolCase"]
}

Example:

dotNotation

Converts the string to dot.notation format (lowercase words separated by dots).

{
  "id": "propertyPath",
  "message": "Enter property path",
  "type": "input",
  "transformers": ["dotNotation"]
}

Example:

pathCase

Converts the string to path/case format (words separated by forward slashes).

{
  "id": "filePath",
  "message": "Enter file path",
  "type": "input",
  "transformers": ["pathCase"]
}

Example:

spaceCase

Converts the string to space case format (words separated by spaces, lowercase).

{
  "id": "displayName",
  "message": "Enter display name",
  "type": "input",
  "transformers": ["spaceCase"]
}

Example:

trainCase

Converts the string to Train-Case format (capitalized words separated by hyphens).

{
  "id": "trainName",
  "message": "Enter Train-Case name",
  "type": "input",
  "transformers": ["trainCase"]
}

Example:

upperCamelCase

Converts the string to UpperCamelCase format (same as PascalCase).

{
  "id": "componentName",
  "message": "Enter component name",
  "type": "input",
  "transformers": ["upperCamelCase"]
}

Example:


Transformer Types

Regex Transformer

Apply regular expression find-and-replace operations.

{
  "type": "regex",
  "pattern": "-",
  "replacement": "_",
  "flags": "g"
}

Properties:

Example:

{
  "id": "snakeCaseName",
  "value": "",
  "transformers": [
    {
      "type": "regex",
      "pattern": "-",
      "replacement": "_",
      "flags": "g"
    }
  ]
}

Computed Transformer

Evaluate a JavaScript expression with access to all context variables.

{
  "type": "computed",
  "expression": "projectName.length"
}

Properties:

Example:

{
  "id": "nameLength",
  "value": "computed",
  "transformers": [
    {
      "type": "computed",
      "expression": "projectName.length"
    }
  ]
}

Available Context: All prompts, variables, and environment variables are available in the expression:

{
  "type": "computed",
  "expression": "projectName.toUpperCase() + '_' + version"
}

Using Transformers

On Prompts

Transform user input before storing:

{
  "prompts": [
    {
      "id": "projectName",
      "message": "What is the project name?",
      "type": "input",
      "transformers": ["slugify"]
    },
    {
      "id": "description",
      "message": "Project description",
      "type": "input",
      "transformers": ["trim"]
    }
  ]
}

On Variables

Transform computed variable values:

{
  "variables": [
    {
      "id": "upperProjectName",
      "value": "",
      "transformers": ["uppercase"]
    },
    {
      "id": "packageName",
      "value": "",
      "transformers": [
        "lowercase",
        {
          "type": "regex",
          "pattern": "\\s+",
          "replacement": "-",
          "flags": "g"
        }
      ]
    }
  ]
}

Transformer Chains

Apply multiple transformers in sequence by using an array:

{
  "id": "cleanedName",
  "value": "  My Project  ",
  "transformers": ["trim", "lowercase", "slugify"]
}

Execution Order:

  1. trim β†’ "My Project"
  2. lowercase β†’ "my project"
  3. slugify β†’ "my-project"

Complex Chain Example

{
  "id": "formattedName",
  "value": "",
  "transformers": [
    "trim",
    {
      "type": "regex",
      "pattern": "[^a-zA-Z0-9\\s-]",
      "replacement": "",
      "flags": "g"
    },
    "lowercase",
    {
      "type": "regex",
      "pattern": "\\s+",
      "replacement": "-",
      "flags": "g"
    }
  ]
}

Custom Transformers

Define custom transformers globally and reference them by ID throughout your configuration.

Defining Custom Transformers

{
  "transformers": [
    {
      "id": "reverse",
      "type": "custom",
      "handler": "value => value.split('').reverse().join('')"
    },
    {
      "id": "capitalize",
      "type": "custom",
      "handler": "value => value.charAt(0).toUpperCase() + value.slice(1)"
    }
  ]
}

Using Custom Transformers

Reference custom transformers by their ID:

{
  "id": "reversedName",
  "value": "",
  "transformers": ["reverse"]
}

Or combine with other transformers:

{
  "id": "processedName",
  "value": "",
  "transformers": ["trim", "lowercase", "capitalize"]
}

Examples

Complete Configuration with Transformers

{
  "$schema": "https://unpkg.com/@pixpilot/scaffoldfy/schema",
  "name": "transformers-example",
  "description": "Example configuration demonstrating value transformers",
  "prompts": [
    {
      "id": "projectName",
      "message": "What is the project name?",
      "type": "input",
      "required": true,
      "transformers": ["slugify"]
    },
    {
      "id": "description",
      "message": "Project description",
      "type": "input",
      "transformers": ["trim"]
    },
    {
      "id": "authorEmail",
      "message": "Author email",
      "type": "input",
      "transformers": ["lowercase"]
    }
  ],
  "variables": [
    {
      "id": "upperProjectName",
      "value": "",
      "transformers": ["uppercase"]
    },
    {
      "id": "packageName",
      "value": "",
      "transformers": [
        "lowercase",
        {
          "type": "regex",
          "pattern": "-",
          "replacement": "_",
          "flags": "g"
        }
      ]
    },
    {
      "id": "nameLength",
      "value": "computed",
      "transformers": [
        {
          "type": "computed",
          "expression": "projectName.length"
        }
      ]
    }
  ],
  "tasks": [
    {
      "id": "create-readme",
      "name": "Create README",
      "type": "write",
      "config": {
        "file": "README.md",
        "template": "# \n\n\n\nPackage: \nAuthor: \nName Length: \n"
      }
    }
  ]
}

Real-World Use Cases

1. NPM Package Name from Project Name

{
  "prompts": [
    {
      "id": "projectName",
      "message": "Project name",
      "type": "input",
      "transformers": [
        "trim",
        "lowercase",
        {
          "type": "regex",
          "pattern": "[^a-z0-9-]",
          "replacement": "",
          "flags": "g"
        }
      ]
    }
  ]
}

2. Generate Multiple Name Variants

{
  "prompts": [
    {
      "id": "componentName",
      "message": "Component name",
      "type": "input"
    }
  ],
  "variables": [
    {
      "id": "componentKebab",
      "value": "",
      "transformers": ["slugify"]
    },
    {
      "id": "componentPascal",
      "value": "",
      "transformers": [
        {
          "type": "regex",
          "pattern": "[-_\\s]+(.)?",
          "replacement": "$1",
          "flags": "g"
        },
        {
          "type": "computed",
          "expression": "componentName.charAt(0).toUpperCase() + componentName.slice(1)"
        }
      ]
    }
  ]
}

3. Clean and Validate Email

{
  "prompts": [
    {
      "id": "email",
      "message": "Email address",
      "type": "input",
      "transformers": ["trim", "lowercase"]
    }
  ]
}

Best Practices

  1. Chain Order Matters: Apply transformers in logical order (e.g., trim before lowercase)
  2. Use Built-ins First: Leverage built-in transformers for common operations
  3. Keep Custom Simple: Complex logic is better in computed transformers
  4. Test Expressions: Verify computed transformer expressions work with your data
  5. Document Custom Transformers: Add descriptions for reusable custom transformers

Limitations


See Also