Function mapTemplate

  • Replacing specific data in a template string, support ${xxxx} {{xxxx}} and {xxxx}

    Parameters

    • tmp: string

      Template string

    • data: Record<string, unknown> | (value: string) => unknown

      Template data of map function

    Returns string

    • result
    const tmp = "My name is ${name}, I'm ${age} years old."
    mapTemplate(tmp, {
    name: 'saqqdy',
    age: 18
    })
    // My name is saqqdy, I'm 18 years old.

    mapTemplate(tmp, key => ({ name: 'saqqdy', age: 28 }[key]))
    // My name is saqqdy, I'm 28 years old.

    const tmp = "My name is {{name}}, I'm {{age}} years old."
    mapTemplate(tmp, {
    name: 'saqqdy',
    age: 18
    })
    // My name is saqqdy, I'm 18 years old.

    5.9.0