UNPKG

@senx/discovery-widgets

Version:

Discovery Widgets Elements

95 lines (94 loc) 3.27 kB
/* * Copyright 2022-2025 SenX S.A.S. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ export class LangUtils { static prepare(ws, vars = {}, skippedVars, type, lang = 'warpscript') { switch (lang) { case 'flows': return LangUtils.generateFlows(ws, vars, skippedVars, type); case 'warpscript': return LangUtils.generateWarpscript(ws, vars, skippedVars, type); } } static generateFlowsVars(key, value) { if (typeof value === 'string') { return `${key} = "${value}"`; } else if (typeof value === 'number') { return `${key} = "${value}"`; } else { if (value.hasOwnProperty('type') && value.hasOwnProperty('value')) { if (value.type === 'string') { return `${key} = "${value.value}"`; } else { return `${key} = ${value.value}`; } } else { return `${key} = JSON->('${encodeURIComponent(JSON.stringify(value))}')`; } } } static generateWarpscriptVars(key, value) { if (value === null || value === undefined) { return `NULL "${key}" STORE`; } else if (typeof value === 'string') { return `"${encodeURIComponent(value)}" "${key}" STORE`; } else if (typeof value === 'number') { return `${value} "${key}" STORE`; } else { if (value && value.hasOwnProperty('type') && value.hasOwnProperty('value')) { if (value.type === 'string') { return `"${encodeURIComponent(value.value)}" "${key}" STORE`; } else { return `${value.value} "${key}" STORE`; } } else { return ` <' ${JSON.stringify(value)} '> JSON-> "${key}" STORE`; } } } static generateFlows(ws, vars, skippedVars, _type) { const varsStr = Object.keys(vars || {}) .filter(k => !(skippedVars || []).includes(k)) .map(k => LangUtils.generateFlowsVars(k, vars[k])).join('\n') + '\n'; return `<' ${varsStr} ${ws} '> FLOWS`; } static generateWarpscript(ws, vars, skippedVars, _type) { const varsStr = Object.keys(vars !== null && vars !== void 0 ? vars : {}) .filter(k => !(skippedVars !== null && skippedVars !== void 0 ? skippedVars : []).includes(k)) .map(k => LangUtils.generateWarpscriptVars(k, vars[k])).join('\n') + '\n'; return ` ${varsStr} ${ws} `; } } //# sourceMappingURL=lang-utils.js.map