https://github.com/hustcc/tplv
:footprints: Nano string template library for modern, based on ES6 template string syntax.
https://github.com/hustcc/tplv
es6 string strings template tplv
Last synced: 3 months ago
JSON representation
:footprints: Nano string template library for modern, based on ES6 template string syntax.
- Host: GitHub
- URL: https://github.com/hustcc/tplv
- Owner: hustcc
- License: mit
- Created: 2019-12-16T02:23:20.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-01T13:30:11.000Z (over 3 years ago)
- Last Synced: 2025-03-17T07:51:29.915Z (3 months ago)
- Topics: es6, string, strings, template, tplv
- Language: TypeScript
- Homepage: https://github.com/hustcc/tplv
- Size: 48.8 KB
- Stars: 31
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# TPLV
> Nano(~170 bytes), High performance string template library, based on ES6 String template syntax.
[](https://www.npmjs.com/package/tplv)
[](https://github.com/hustcc/tplv/actions)
[](https://coveralls.io/github/hustcc/tplv?branch=master)
[](https://www.npmjs.com/package/tplv)## Install
```bash
$ npm i --save tplv
```## Usage
- `render` template string
```ts
import { render } from 'tplv';const template = '${ name }, ${value}(${percent} | Top ${array[2]})';
const data = {
name: 'Hangzhou',
value: 1200,
percent: '13%',
array: [1, 2, 3, 4]
};render(template, data); // `Hangzhou, 1200(13% | Top 3)` will be got
```- `compile` mode
> For `13x` faster performance then render mode.
```ts
import { compile } from 'tplv';const template = '${ name }, ${value}(${percent} | Top ${array[2]})';
const data = {
name: 'Hangzhou',
value: 1200,
percent: '13%',
array: [1, 2, 3, 4]
};const fn = compile(template, ['name', 'value', 'percent', 'array']);
fn(data); // `Hangzhou, 1200(13% | Top 3)` will be got
```## Perf
Run performance test with [rendering-test](https://aui.github.io/art-template/rendering-test/).

## Principle
The core code and principles are as follows:
```ts
function render(template, data) {
const ks = Object.keys(data);
const vs = ks.map((k) => data[k]);const t = `return \`${template}\``;
const f = new Function(...ks, t);return f(...vs);
}
```## Dev
```bash
# install dependence
$ npm install# run test cases
$ npm run test# run performance for render / compile mode
$ npm run perf# build package
$ npm run build
```## License
MIT@[hustcc](https://github.com/hustcc).