interpolate-es
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,1 +0,8 @@ | ||
export { default } from "./src/interpolate.js"; | ||
/** | ||
* Interpolate a tagged template literal from the inputs | ||
* | ||
* @param {*} template the template literal string | ||
* @param {*} [tags] the tagged values in the template | ||
* @returns the template output with the tagged literals applied | ||
*/ | ||
export default function interpolate(template: any, tags?: any): any; |
35
index.js
@@ -1,1 +0,34 @@ | ||
export { default } from './src/interpolate.js'; | ||
/* eslint-disable no-new-func */ | ||
/** | ||
* Interpolate a tagged template literal from the inputs | ||
* | ||
* @param {*} template the template literal string | ||
* @param {*} [tags] the tagged values in the template | ||
* @returns the template output with the tagged literals applied | ||
*/ | ||
export default function interpolate (template, tags = {}) { | ||
const keys = Object.keys(tags); | ||
const values = Object.values(tags); | ||
try { | ||
return new Function(...keys, `return \`${template}\`;`)(...values); | ||
} catch (e) { | ||
throw new TemplateException(template, tags, e); | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
class TemplateException extends Error { | ||
constructor (template, tags, message) { | ||
super(); | ||
this.name = 'TemplateError'; | ||
let msg = '\n------------------\n'; | ||
msg += `Template: \`${template}\``; | ||
msg += '\n------------------\n'; | ||
msg += `Tags: ${JSON.stringify(tags, null, 2)}`; | ||
msg += '\n------------------\n'; | ||
msg += message; | ||
this.message = msg; | ||
} | ||
} |
{ | ||
"name": "interpolate-es", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Dynamically build tagged templates", | ||
@@ -14,12 +14,13 @@ "keywords": [ | ||
"repository": "http://github.com/vanillaes/interpolate-es/", | ||
"main": "index.js", | ||
"module": "dist/interpolate.esm.js", | ||
"type": "module", | ||
"main": "index.cjs", | ||
"exports": { | ||
"import": "./index.js", | ||
"require": "./index.cjs" | ||
}, | ||
"types": "index.d.ts", | ||
"engines": { | ||
"node": ">=13.2" | ||
}, | ||
"scripts": { | ||
"test": "tape-es", | ||
"test:watch": "tape-watch-es", | ||
"test:compat": "node test/compat.spec.cjs", | ||
"lint": "semistandard", | ||
@@ -35,3 +36,3 @@ "types": "npx tsc -p .config/jsconfig.json", | ||
"package": "npx rimraf package && npm pack | tail -n 1 | xargs tar -xf", | ||
"preversion": "npm test && npm run lint && npm run types", | ||
"preversion": "npm test && npm run test:compat && npm run lint && npm run types", | ||
"version": "npm run build", | ||
@@ -45,3 +46,6 @@ "postversion": "git push --follow-tags" | ||
"tape-es": "^1.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=13.7" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7766
105
8