@nent/core
Version:
119 lines (114 loc) • 3.9 kB
JavaScript
/*!
* NENT 2022
*/
;
const factory = require('./factory-0d7ddff9.js');
const index = require('./index-637e8c28.js');
const interfaces = require('./interfaces-95d0415a.js');
const values = require('./values-b2399e33.js');
const state = require('./state-f97ff0e6.js');
const logging = require('./logging-37c154cf.js');
require('./index-96f3ab3f.js');
require('./index-1829aebc.js');
require('./promises-463f4e01.js');
/* istanbul ignore file */
class DataItemProvider {
constructor(data, setter) {
this.data = data;
this.setter = setter;
this.changed = new index.EventEmitter();
}
async get(key) {
if (key === 'item') {
return this.data;
}
return this.data[key];
}
async set(key, value) {
if (this.setter) {
await this.setter(key, value);
}
else {
this.data[key] = value;
}
this.changed.emit(interfaces.DATA_EVENTS.DataChanged);
}
}
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.
*/
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>)}
*/
async function resolveTokens(textWithTokens, forExpression = false, data) {
values.requireValue(textWithTokens, 'valueExpression');
if (!state.state.dataEnabled) {
logging.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) {
factory.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 factory.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 && values.isValue(value)) {
const object = values.isJson(value) ? JSON.parse(value) : value;
let resolved = values.getPropertyValue(object, propKey, defaultValue);
value = values.isString(resolved)
? resolved
: values.isObject(resolved)
? JSON.stringify(resolved)
: `${resolved}`;
}
let replacement = values.isObject(value)
? JSON.stringify(value).split('"').join("'")
: (value === null || value === void 0 ? void 0 : value.toString()) || '';
if (forExpression && !values.isObject(value)) {
if (values.isNotValue(value) || value === '')
replacement = 'null';
else
replacement = replacement.replace(escapeStringsRegex, `'$1'`);
}
result = result.replace(expression, replacement);
}
if (data) {
factory.removeDataProvider('data');
}
return result;
}
exports.hasToken = hasToken;
exports.resolveTokens = resolveTokens;