Compass helpers for browser platforms
    Preparing search index...

    Function replaceVariablesInString

    • Parameters

      • templateString: string

        字符串模板

      • params: Record<string, VariableValue>

        参数对象,支持字符串、数字、布尔值、null、undefined

      • Optionaloptions: ReplaceVariablesOptions = {}

        配置选项

        • wrapper

          变量包裹符号

        • strict

          是否严格模式,如果为true,未找到的变量会抛出错误

        • defaultValue

          未找到变量时的默认值,仅在非严格模式下生效

      Returns string

      替换字符串中包裹起来的变量

      当输入参数类型无效时抛出错误

      当严格模式下存在未定义变量时抛出错误

      import { replaceVariablesInString } from '@compass-aiden/utils';

      // 基础用法
      replaceVariablesInString('Hello {{name}}!', { name: 'World' }); // 'Hello World!'

      // 支持多种数据类型
      replaceVariablesInString('用户ID: {{id}}, 年龄: {{age}}, 是否VIP: {{isVip}}', {
      id: 12345,
      age: 25,
      isVip: true
      }); // '用户ID: 12345, 年龄: 25, 是否VIP: true'

      // 自定义包裹符号
      replaceVariablesInString('Hello [name]!', { name: 'World' }, {
      wrapper: ['[', ']']
      }); // 'Hello World!'

      // 严格模式
      replaceVariablesInString('Hello {{name}}!', {}, { strict: true }); // 抛出错误

      // 默认值
      replaceVariablesInString('Hello {{name}}!', {}, {
      defaultValue: 'Guest'
      }); // 'Hello Guest!'