UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

116 lines (112 loc) 3.92 kB
/*! * NENT 2022 */ import { a as addDataProvider, g as getDataProvider, r as removeDataProvider } from './factory-acbf0d3d.js'; import { E as EventEmitter } from './index-f7016b94.js'; import { D as DATA_EVENTS } from './interfaces-8c5cd1b8.js'; import { r as requireValue, i as isValue, d as isJson, g as getPropertyValue, c as isString, b as isObject, a as isNotValue } from './values-ddfac998.js'; import { a as state } from './state-27a8a5bc.js'; import { w as warn } from './logging-5a93c8af.js'; import './index-4bfabbbd.js'; import './index-916ca544.js'; import './promises-584c4ece.js'; /* istanbul ignore file */ class DataItemProvider { constructor(data, setter) { this.data = data; this.setter = setter; this.changed = new 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(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) { requireValue(textWithTokens, 'valueExpression'); if (!state.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; } export { hasToken, resolveTokens };