@nent/core
Version:
83 lines (82 loc) • 3.11 kB
JavaScript
/*!
* NENT 2022
*/
import { commonState, getPropertyValue, isJson, isNotValue, isObject, isString, isValue, requireValue, warn, } from '../common';
import { addDataProvider, getDataProvider, removeDataProvider, } from './factory';
import { DataItemProvider } from './providers/item';
const tokenRegEx = /\{\{([\w-]*):(\w*)((?:\[\d+\]|\.)[\w.\-\]]+)?(?:\?([\w.-]*))?\}\}/g;
const escapeStringsRegex = /['"]?([a-z/][\w-/?.]+)['"]?/gi;
/**
* If the value is a string, and it contains a token, return true, otherwise return false.
* @param {string} value - The string to check for tokens
* @returns A boolean value.
*/
export function hasToken(value) {
var _a;
return (((_a = value === null || value === void 0 ? void 0 : value.match(tokenRegEx)) === null || _a === void 0 ? void 0 : _a.length) || 0) > 0;
}
/**
* This function replaces all tokens: (ie `{{provider:key}}`) values with the actual values
* from the expressed provider & key. This is used by {evaluateExpression}
* before it is sent to {evaluate} for calculation.
*
* @export resolveTokens
* @param {string} textWithTokens
* @return {*} {(Promise<string|null>)}
*/
export async function resolveTokens(textWithTokens, forExpression = false, data) {
requireValue(textWithTokens, 'valueExpression');
if (!commonState.dataEnabled) {
warn(`Data-services are not enabled. Tokens are not resolved.`);
return textWithTokens;
}
let result = textWithTokens.slice();
if (textWithTokens === null || textWithTokens === '') {
return result;
}
// If this expression doesn't match, leave it alone
if (!hasToken(textWithTokens)) {
return result;
}
if (data != undefined && data != null) {
addDataProvider('data', new DataItemProvider(data));
}
// Replace each match
let match;
while ((match = tokenRegEx.exec(textWithTokens))) {
const expression = match[0];
const providerKey = match[1];
const dataKey = match[2];
const propKey = match[3] || '';
const defaultValue = match[4] || null;
const provider = await getDataProvider(providerKey);
if (provider == null && !forExpression)
continue;
let value = await (provider === null || provider === void 0 ? void 0 : provider.get(dataKey));
if (value == undefined)
value = defaultValue;
if (propKey && isValue(value)) {
const object = isJson(value) ? JSON.parse(value) : value;
let resolved = getPropertyValue(object, propKey, defaultValue);
value = isString(resolved)
? resolved
: isObject(resolved)
? JSON.stringify(resolved)
: `${resolved}`;
}
let replacement = isObject(value)
? JSON.stringify(value).split('"').join("'")
: (value === null || value === void 0 ? void 0 : value.toString()) || '';
if (forExpression && !isObject(value)) {
if (isNotValue(value) || value === '')
replacement = 'null';
else
replacement = replacement.replace(escapeStringsRegex, `'$1'`);
}
result = result.replace(expression, replacement);
}
if (data) {
removeDataProvider('data');
}
return result;
}