梦回苍石居  |
Cangshi Live

PlateVia 35 Views

作者:
苍石
进度:
测试
Progress: 50%
项目开始时间:
2021-05-24
项目结束时间:
项目发表时间:
2021-05-26 16:40:06
最后编辑于:
4 个月前
标签:
TEMPLATE模版解析JAVSCRIPT

查看演示

PlateVia for Javascript

A javascript parser to parse Template or Expression, based on AST.

Install

npm install -D platevia

CLI

npx platevia -v # Get current version
npx platevia -h # Get the cli help text

Demo

Parse Expression


const { parsePlate } = require('platevia');

const toParseText = '1 + a'

const parsedResult = parsePlate(toParseText).via({a: 100})

console.log(parsedResult); // > 101

Parse Template

In fact, the expression is being parsed, but the template string based on ES6 is replaced


const { parsePlate } = require('platevia');

const toParseText = '`Hello ${ yourName }, there are ${ clients.length } clients: ${ each(clients, client => client.name, \', \') }`'

const parsedResult = parsePlate(toParseText).via(
    {
        yourName: 'Weals',
        clients: [
            {
                name: 'Ane'
            },
            {
                name: 'Lee'
            },
            {
                name: 'Joe'
            }
        ],
        each: function(array, each, separator){
            return array.map(each).join(separator);
        }
    }
)

console.log(parsedResult); // > Hello Weals, there are 3 clients: Ane, Lee, Joe

API Documentation

https://github.com/canguser/platevia/blob/master/docs/modules.md

留言 ( 0 )

Person name
未登录用户可以发表匿名评论