Skip to content

Template

Is a function that replace all the template expressions with the corresponding values from the data object.

๐ŸŽฌ Usage

<script>
    import {template} from "@sveu/shared"

    const str = `Hi {{ name }},

    Welcome to {{ platform }}
`
    const result = template(str, {"name": "Beatrice", "platform": "svelte"})
</script>

<h1>{result}!</h1>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ‘ป Arguments

Name Description Type Required
str The string to be searched string Yes
data The data object to be used to replace the search expression Record<string, any> Yes
regex The search expression RegExp No

โ†ฉ๏ธ Returns

A string with the template expressions replaced with the corresponding values from the data object.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
/**
 * A string with values from data object using search expression
 *
 * @param str - The string to be searched
 *
 * @param data - The data object to be used to replace the search expression
 *
 * @param regex - The search expression
 *
 * @returns A string with values from data object using search expression
 */
export function template(
    str: string,
    data: Record<string, any>,
    regex = /\{\{(.+?)\}\}/g
) {
    return Array.from(str.matchAll(regex)).reduce((acc, match) => {
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        const [_, key] = match
        return acc.replace(match[0], data[key])
    }, str)
}

Last update: 2023-02-09