UNPKG

@amaui/utils

Version:
42 lines (36 loc) 1.33 kB
import is from './is'; import getStringVariables from './getStringVariables'; const optionsDefault = { getVariables: true, cleanVariables: true, placeholderPrefix: '_' }; const setStringVariables = function (value) { let variablesToValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; let options_ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; const options = { ...optionsDefault, ...options_ }; if (is('string', value)) { let newValue = value; let stringVariables = { variables: variablesToValue.map(variable => variable.key) }; if (options.getVariables) { stringVariables = getStringVariables(value, { cleanVariables: options.cleanVariables, placeholderPrefix: options.placeholderPrefix }); newValue = stringVariables.valueWithPlaceholders; } stringVariables.variables.forEach((variable, index) => { const variableItem = variablesToValue.find(item => item.key === variable); if (variableItem) { const variableValue = variableItem.value; if (variableValue !== undefined) newValue = newValue.replace(new RegExp("\\".concat(options.placeholderPrefix).concat(index)), variableValue); } }); return newValue; } }; export default setStringVariables;