字符串内插
此套件协助动态字符串插值和飞行值转换. 如果使用允许文本输入的CMS,并且想要在不写大量代码的情况下转换值项,你可能会发现这个有用.
示例
在现实世界里, Hi, my name is {name}. 是来自一个编辑器输入的CMS。 内容编辑器对技术一无所知 {name} 但理解这一点 {name} 改为客户端上的实际名称.
const Interpolator = require('string-interpolation');
// This isn't needed, unless you need to change your interpolation brackets to something else
const options = {
delimiter: ['{','}'],
};
const interpolator = new Interpolator(options);
const replaceThis = `Welcome back, {username}!`;
const data = {
username: 'Dan',
};
interpolator.parse(replaceThis, data);
// Welcome back, Dan!
文档
选项( 可选)
delimiter: 矩阵; 必须是2项的数组。 这是分隔符的始末值 。 默认为{}
// Defaults
{
delimiter: ['{','}'],
}
备选案文
如果数据密钥没有定义,则可以提供替代值。 仅用冒号来定义替代文本 : 在数据密钥之后。 由于当前限制,您无法使用保留符号( Q)|, (中文(简体) ), :作为备选案文.
如果您需要将保留符号作为备选文本加入, 请随意打开 PR 用于固定 :.
const replaceThis = `Hi, my name is {name:Altnerative Text}.`;
const interpolator = new Interpolator();
interpolator.parse(replaceThis);
// Hi, my name is Alternative Text.
修改器
修改器是转换插入文本的函数. 这些是通过从插符中减少解析的字符串来应用的. 通过指定管子 |,解析器将基于引管的修饰器来转换插入文本. 如果提供,这也将对替代文本进行转换.
const Interpolator = require('string-interpolator');
const interpolator = new Interpolator();
const replaceThis = `Hi, my name is {name|uppercase}.`;
const data = {
name: 'Dan',
};
interpolator.parse(replaceThis, data);
// Hi, my name is DAN.
你可以通过用逗号来吸附来添加尽可能多的修饰剂. 如果您正在使用自定义修饰符并想要减少一个字符串, 则有用, 而不是用下面的例子 .
const Interpolator = require('string-interpolator');
const interpolator = new Interpolator();
// This will transform to title case, and then lowercase
const replaceThis = `Hi, my name is {name|title,lowercase}.`;
const data = {
name: 'Dan',
};
interpolator.parse(replaceThis, data);
// Hi, my name is dan.
建于修饰器中
| 修改 | 说明 |
|---|---|
| 标题 | 转换为标题大小写 |
| 大写 | 转换为大写 |
| 小写 | 转换为小写 |
所有修饰器,无论是自定义还是预建的,都不对大小写敏感。您可以任意命名一个修改符,但都会被解析为小写文本值.
自定义修改符
也可以自建修饰器. 修饰器是接收内插字符串并进行转换的函数.
.register 修改器( 键, 修改器)
key: 唯一注释修改符的密钥modifier: 减少字符串时要执行的函数
您可以在解析时引用插值和通过的数据.
const customModifier = (val) => val.split('').reverse().join('');
简单示例
const Interpolator = require('string-interpolator');
const interpolator = new Interpolator();
const replaceThis = `Hi, my name is {name|customModifier}.`;
const data = {
name: 'Dan',
};
// `str` in this case will be "Dan"
const customModifier = str => str.split('').reverse().join('');
// Register modifier with interpolation service with the name "customModifier" This is actually parsed internally as `custommodifier`, but to keep it pretty, you should consider using camel cases.
interpolator.registerModifier('customModifier', customModifier);
interpolator.parse(replaceThis, data);
// Output: Hi, my name is naD.
高级示例
假设您不想将数据别名化到引用的数据点(见下文),您可以从修改器本身分析原始数据。 确保尝试/捕获.
const Interpolator = require('string-interpolator');
const interpolator = new Interpolator();
const replaceThis = `2015 World Series Winner: {2015|year2015}`;
const worldSeriesWinner = {
winners: [{
year: 2015,
team: 'Royals'
},
{
year: 2016,
team: 'Cubs'
},
{
year: 2017,
team: 'Astros'
}]
};
// val will be`2015`, data will be worldSeriesWinner
const advanceCustomModifier = (val, data) => {
try {
// val is always a string, which is why parseInt is neccessary if referencing a number
const winner = data.winners.find(winner => winner.year === parseInt(val));
return winner.team;
} catch (e) {
console.log(e);
return val;
}
}
interpolator.registerModifier('year2015', advanceCustomModifier);
const interpolated = interpolator.parse(replaceThis, worldSeriesWinner);
// Output: 2015 World Series Winner: Royals
数据别名
有时当数据作为对象传递时,你需要访问特定的节点或阵列项目.
考虑预期: Dan Seripap is from New York, NY我们的数据结构是这样的:
const data = {
name: {
first: 'Dan',
last: 'Seripap',
},
locations: ['New York','NY'],
}
我们要正确插入以下案文: Dan Seripap is from New York, NY。如果确切知道这些节点存在在何处,可以提供下列字符串来插入: {name.first} {name.last} is from {locations[0]} {locations[1]}。 。 。 .
然而,有时插入的文字来自其数据来源不明的来源. 在这种情况下,我们需要添加一些数据参考,说明这些数据点的实际存在地点.
addAlias(钥匙,参考)
key: 键表示正在替换的内容ref: 从父对象引用密钥值
const aliases = [{
key: 'firstName',
ref: 'name.first'
},
{
key: 'lastName',
ref: 'name.last'
},
{
key: 'city',
ref: 'locations[0]'
},
{
key: 'state',
ref: 'locations[1]'
}];
// Add aliaseses to interpolator
aliases.forEach(alias => interpolator.addAlias(alias.key, alias.ref));
这些别名现在定义了存在值的确切位置的密钥. 我们现在可以插上文字: {firstName} {lastName} is from {city} {state} 和结果一样.
移动 Alias( 关键)
删除按键引用的别名点.