@universal-packages/variable-replacer
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,1 +0,1 @@ | ||
export declare function evaluateAndReplace(input: string, enclosures?: [string, string]): string; | ||
export declare function evaluateAndReplace(input: string, scope?: Record<string, any>, enclosures?: [string, string]): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.evaluateAndReplace = void 0; | ||
function evaluateAndReplace(input, enclosures = ['<%', '%>']) { | ||
const isolatedEval_1 = require("./isolatedEval"); | ||
function evaluateAndReplace(input, scope = {}, enclosures = ['<%', '%>']) { | ||
let finalValue = input; | ||
@@ -17,3 +18,3 @@ const enclosureA = `\\${enclosures[0].split('').join('\\')}`; | ||
const jsCode = execResult[1]; | ||
const evaluation = eval(jsCode); | ||
const evaluation = (0, isolatedEval_1.____MMMEVALUEATE98786875674674)(jsCode, scope); | ||
// "result is << 1 + 1 >>" --> "result is 2" | ||
@@ -20,0 +21,0 @@ finalValue = finalValue.replace(currentMatch, evaluation); |
{ | ||
"name": "@universal-packages/variable-replacer", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Easily inject environment variables or provided variables to compose richer strings.", | ||
@@ -5,0 +5,0 @@ "author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)", |
@@ -77,3 +77,3 @@ # Variable Replacer | ||
#### **`evaluateAndReplace(input: string, [enclosures: [string, string]])`** | ||
#### **`evaluateAndReplace(input: string, scope: Object, [enclosures: [string, string]])`** | ||
@@ -93,2 +93,17 @@ Captures what is between the enclosures and evaluates it as a JS expression. The result is then used to replace the match in the string. | ||
#### Scope | ||
You can provide your own scope to use in your expression. | ||
```js | ||
import { evaluateAndReplace } from '@universal-packages/variable-replacer' | ||
const string = 'key: <% cpusCount / 2 %>, another: <% mem / 2 %>' | ||
const finalString = evaluateAndReplace(string, { cpusCount: 4, mem: 16 }) | ||
console.log(finalString) | ||
// > 'key: 2, another: 8' | ||
``` | ||
#### Enclosures | ||
@@ -95,0 +110,0 @@ |
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
17727
21
110
187