@universal-packages/variable-replacer
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,1 +0,2 @@ | ||
export declare function evaluateAndReplace(input: string, scope?: Record<string, any>, enclosures?: [string, string]): string; | ||
import { EvaluateAndReplaceOptions } from './evaluateAndReplace.types'; | ||
export declare function evaluateAndReplace(input: string, options?: EvaluateAndReplaceOptions): string; |
@@ -5,6 +5,7 @@ "use strict"; | ||
const isolatedEval_1 = require("./isolatedEval"); | ||
function evaluateAndReplace(input, scope = {}, enclosures = ['<%', '%>']) { | ||
function evaluateAndReplace(input, options) { | ||
const finalOptions = { scope: {}, enclosures: ['<%', '%>'], ...options }; | ||
let finalValue = input; | ||
const enclosureA = `\\${enclosures[0].split('').join('\\')}`; | ||
const enclosureB = `\\${enclosures[1].split('').join('\\')}`; | ||
const enclosureA = `\\${finalOptions.enclosures[0].split('').join('\\')}`; | ||
const enclosureB = `\\${finalOptions.enclosures[1].split('').join('\\')}`; | ||
const enclosureRegex = `${enclosureA}((?:(?!${enclosureB})[\\s\\S])*?)${enclosureB}`; | ||
@@ -19,3 +20,3 @@ const matches = input.match(new RegExp(enclosureRegex, 'g')); | ||
const jsCode = execResult[1]; | ||
const evaluation = (0, isolatedEval_1.____MMMEVALUEATE98786875674674)(jsCode, scope); | ||
const evaluation = (0, isolatedEval_1.____MMMEVALUEATE98786875674674)(jsCode, finalOptions.scope); | ||
// "result is << 1 + 1 >>" --> "result is 2" | ||
@@ -22,0 +23,0 @@ finalValue = finalValue.replace(currentMatch, evaluation); |
{ | ||
"name": "@universal-packages/variable-replacer", | ||
"version": "1.3.0", | ||
"version": "1.4.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, scope: Object, [enclosures: [string, string]])`** | ||
#### **`evaluateAndReplace(input: string, [options: Object])`** | ||
@@ -93,32 +93,32 @@ 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 | ||
#### Options | ||
You can provide your own scope to use in your expression. | ||
- **`scope`** `Object` | ||
You can provide your own scope to use in your expression. | ||
```js | ||
import { evaluateAndReplace } from '@universal-packages/variable-replacer' | ||
```js | ||
import { evaluateAndReplace } from '@universal-packages/variable-replacer' | ||
const string = 'key: <% cpusCount / 2 %>, another: <% mem / 2 %>' | ||
const finalString = evaluateAndReplace(string, { cpusCount: 4, mem: 16 }) | ||
const string = 'key: <% cpusCount / 2 %>, another: <% mem / 2 %>' | ||
const finalString = evaluateAndReplace(string, { cpusCount: 4, mem: 16 }) | ||
console.log(finalString) | ||
console.log(finalString) | ||
// > 'key: 2, another: 8' | ||
``` | ||
// > 'key: 2, another: 8' | ||
``` | ||
#### Enclosures | ||
- **`enclosures`** `[string, string]` | ||
You can provide your own enclosure characters to match for replacements. | ||
You can provide your own enclosure characters to match for replacements. | ||
```js | ||
import { evaluateAndReplace } from '@universal-packages/variable-replacer' | ||
```js | ||
import { evaluateAndReplace } from '@universal-packages/variable-replacer' | ||
const string = 'key: ${ 1 + 1 }$ , another: ${2+2}$' | ||
const finalString = evaluateAndReplace(string, ['${', '}$']) | ||
const string = 'key: ${ 1 + 1 }$ , another: ${2+2}$' | ||
const finalString = evaluateAndReplace(string, ['${', '}$']) | ||
console.log(finalString) | ||
console.log(finalString) | ||
// > 'key: 2, another: 4' | ||
``` | ||
// > 'key: 2, another: 4' | ||
``` | ||
## Combine replacements | ||
@@ -125,0 +125,0 @@ |
Sorry, the diff of this file is not supported yet
18393
24
118