# tag-params
**Repository Path**: mirrors_WebReflection/tag-params
## Basic Information
- **Project Name**: tag-params
- **Description**: Transform a generic string into parameters suitable for template literals functions tags.
- **Primary Language**: Unknown
- **License**: ISC
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-08-19
- **Last Updated**: 2025-12-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# tag-params
[](https://travis-ci.com/WebReflection/tag-params) [](https://coveralls.io/github/WebReflection/tag-params?branch=master)
Transform a generic string into parameters suitable for template literals functions tags.
```js
import {params} from 'tag-params';
// const {params} = require('tag-params');
//
console.log(
params('Random: ${Math.random()}!')
);
// [["Random: ", "!"], 0.3456787643]
console.log(
params('Hello ${user}', {user: 'test'})
);
// [["Hello ", ""], "test"]
// invoke tags through the returned parameters
genericTag(...params(content, namespace));
```
## API
There are 3 utilities exported by this module, so that accordingly with your import, you should get:
* `params`, the main utility, which parses and resolves _values_ in one go.
* `parse`, which returns a `{template, values}` object, with mapped "_chunks_" in the template, and the list of _values_ (interpolations/holes), where each value is a string.
* `partial`, which uses `parse` and returns a callback that will map new values through the optional `object` passed along.
### params(content:string, object?:any) => [string[], ...any[]]
It's the "_default_" use case of this utility. It parses the `content` and returns a `[template, ...values]` _Array_ with values retrieved through the optional `object`. If no `object` is passed along, it simply evaluates interpolations as plain JavaScript.
This utility is a shortcut for a one-off `partial(content)(object)` call.
### parse(content:string, transform?:function) => {template:string[], values:string[]}
It parses a string, and it uses the optional `transform` callback, which is _no-op_ as default, to assign each _value_ to the list of expected _values_.
The `transform` optional callback is specially useful when the interpolated content might contains _HTML_ normalized chars, such as `value => stuff(value)` instead of `value => stuff(value)`, which is normal when the content is retrieved via `element.innerHTML`, as example.
The `template` property contains all chunks around `${...}` interpolations, while `values` contains all interpolations content as string.
### partial(content:string, transform?:function) => (object?) => [string[], ...any[]]
This utility parses the `content` through an optional `transform`, and returns a _callback_ that accepts a new object each time.
This is particularly useful to avoid parsing the same template content over and over, and just update its interpolation _values_ through the optional `object`.
```js
import {partial} from 'tag-params';
const update = partial('Hello ${user}!');
console.log(update({user: 'First'}));
// [["Hello ", "!"], "First"]
console.log(update({user: 'Second'}));
// [["Hello ", "!"], "Second"]
// always true
console.assert(
update({user: 'First'})[0] ===
update({user: 'Second'})[0]
);
```
The main advantage of this utility is that it parses the `content` and it creates the `template` _Array_ only **once**, meaning every template literal based library could benefit from it, using the uniqueness of the `template` to parse complex chunks of _HTML_, or anything else, once, enabling repeated updates at almost zero performance cost.
## Use Cases
The most common/requested use case for this is likely landing templates on the page and use their content, [as shown in this CodePen example](https://codepen.io/WebReflection/pen/OJMRZow?editors=0010):
```html
${items.map(function (item) {
return html`