如何将js中的字符串作为模板求值

Zied Hamdi

我想以集中控制的方式进行路由,为此我定义了一些视图,并且希望对其进行评估,就好像它是以下格式的模板一样:

`this is a  ${variable} in the template`

这是我的代码:

const Views = Object.freeze({
    Home: {path:"/"},
    Kind:{path:"/${kind}", values:{kind:{notNull:true}}},
    SearchEntity:{path:"/${kind}/search/${name}", values:{kind:{notNull:true}, name:{}}},
    Entity:{path:"/${kind}/shop/${eid}", values:{kind:{notNull:true}, eid:{notNull:true}}},
    Issue:{path:"/${kind}/shop/${eid}/issue/${issueId}", values:{kind:{notNull:true}, eid:{notNull:true}, issueId:{notNull:true}}},
    NewShop:{path:"/${kind}/newShop/${name}", values:{kind:{notNull:true}, name:{}}},
})

eval.call({kind:"myKind", eid:"myeid", issueId:"myIssueId"}, Views.Issue.path)

这显然行不通,这就是为什么我问这个问题:)

eval方法无法理解上下文:

undefined:1
/${kind}/shop/${eid}/issue/${issueId}
^

SyntaxError: Invalid regular expression flags
    at Object.eval (<anonymous>)
    at Object.<anonymous> (C:\Users\Zied\work\weally\src\util.js:10:6)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:240:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)

自然地,我可以使用函数而不是模板来解决问题,但是我想拥有更清晰易读的代码,并且我想在中间添加一些控件(例如notNull验证检查等)。集中方式。所以我想使声明与执行分开

一定的表现

无需eval-拥有path财产是一个函数返回一个模板文字代替,并调用该函数:

const Views = Object.freeze({
  Issue: {
    path: ({kind, eid, issueId }) => `/${kind}/shop/${eid}/issue/${issueId}`,
    values: {
      kind: {
        notNull: true
      },
      eid: {
        notNull: true
      },
      issueId: {
        notNull: true
      }
    }
  },
});
console.log(Views.Issue.path({
  kind: "myKind",
  eid: "myeid",
  issueId: "myIssueId"
}));

如果您不能在path属性中放置函数,则可以使用正则表达式匹配${varName}字符串,并在输入对象中将其替换为相同的属性:

const Views = Object.freeze({
  Issue: {
    path: '/${kind}/shop/${eid}/issue/${issueId}',
    values: {
      kind: {
        notNull: true
      },
      eid: {
        notNull: true
      },
      issueId: {
        notNull: true
      }
    }
  },
});

const replace = (template, obj) => template.replace(/\${(\w+)}/g, (_, varName) => obj[varName]);
console.log(replace(Views.Issue.path, {
  kind: "myKind",
  eid: "myeid",
  issueId: "myIssueId"
}));

如果您也想验证:

const Views = Object.freeze({
  Issue: {
    path: '/${kind}/shop/${eid}/issue/${issueId}',
    values: {
      kind: {
        notNull: true
      },
      eid: {
        notNull: true
      },
      issueId: {
        notNull: true
      }
    }
  },
});

const replace = (template, conditions, obj) => {
  const required = Object.entries(conditions)
    .filter(([, { notNull }]) => notNull)
    .map(([key]) => key);
  if (!required.every(prop => obj.hasOwnProperty(prop))) throw new Error('required missing');
  return template.replace(/\${(\w+)}/g, (_, varName) => obj[varName]);
};


console.log(replace(
  Views.Issue.path,
  Views.Issue.values,
  {
  kind: "myKind",
  eid: "myeid",
  issueId: "myIssueId"
}));


console.log(replace(
  Views.Issue.path,
  Views.Issue.values,
  {
  kind: "myKind",
  eid: "myeid",
}));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将字符串作为变量名求值

在R中,如何创建一个将参数作为字符串求值的函数?

如何将链接放在字符串模板中?

如何将Blade模板视图作为原始HTML字符串获取?

如何将HTML文件作为模板导入汇总并编译为串联的字符串

Excel VBA将单元格中的字符串作为带变量的公式求值

如何将字符串作为字符值访问

如何将包含斜杠('/')作为参数的字符串传递给vue.js中的路由

如何将烧瓶应用程序中的HTML字符串作为DOM元素传递到HTML模板,然后将appendChild传递给HTML

如何将模板字符串呈现为HTML?

如何将空数组放入模板字符串

如何将AngularJS模板转换为字符串?

如何将字符串作为gzip Node.js上传到s3

Ansible:如何将“ {{ABC}}”作为字符串传递

Rails - 如何将字符串作为查询发送?

如何将整个文档HTML作为字符串?

如何将字符串作为 Type 对象?

如何将公式作为字符串传递?

如何将字符串 var 作为函数调用?

如何将字符串基数 2 作为整数?

如何将 CharField 作为字符串操作 Django

如何将文档注释作为字符串获取

在 Swift 中,如何将字符串作为类的属性名称放入函数的参数中?

如何将使用模板T的函数调用到主函数中,以便将最大值作为字符串打印出来?

如何将存储为字符串的脚本标签输出到AngularJS模板中?

如何将枚举int值转换为离子模板中的字符串

如何将当前查询字符串添加到Django模板中的URL?

如何将Django模板中的{{fieldset.fields}}转换为字符串?

Django - 如何将字符串转换为模板中的链接?