UNPKG

cloud-ide-lms-model

Version:

Package for Model management of Cloud IDEsys LMS

208 lines (202 loc) 12.4 kB
"use strict"; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateObjectFromString = exports.generateStringFromObject = exports.generateRandomAlphanumericString = exports.cidePath = void 0; exports.stringInterpolation = stringInterpolation; async function stringInterpolation(variables, body, data) { /* const placeholders = { 'status': 'user.status', 'userName': 'user.name', 'userFullName': 'user.fullName', 'entityName': 'entity.name', 'entityLoginTime': 'entity.data~login', 'entityAddress': 'entity.address.city', }; const data = { user: { status: 'active', name: 'John Doe', fullName: 'John Doe Jr.', }, entity: { name: 'Acme Corp', data: [ { login: '2 days ago' }, { login: '3 days ago' }, ], address: { city: 'Metropolis', }, }, }; const template = 'Hello, {userName}! Your status is {status}. Full name is {userFullName}.' + '[entity-data] This is the row for login time: {entityLoginTime}. [/entity-data]' + 'Your entity is {entityName}. The city is {entityAddress}.'; */ var _a, e_1, _b, _c, _d, e_2, _e, _f, _g, e_3, _h, _j; var _k, _l, _m; let bodyArray = body.split("//"); let replacedBody = []; try { for (var _o = true, bodyArray_1 = __asyncValues(bodyArray), bodyArray_1_1; bodyArray_1_1 = await bodyArray_1.next(), _a = bodyArray_1_1.done, !_a; _o = true) { _c = bodyArray_1_1.value; _o = false; const body_row = _c; let body_row_init = [body_row]; if (body_row.includes('[')) { body_row_init = []; let row_count = 0; let variable_array = body_row.slice(1, (body_row === null || body_row === void 0 ? void 0 : body_row.indexOf(']'))); if (variable_array === null || variable_array === void 0 ? void 0 : variable_array.includes("-")) { let spilited_variable_array = variable_array === null || variable_array === void 0 ? void 0 : variable_array.split("-"); if ((spilited_variable_array === null || spilited_variable_array === void 0 ? void 0 : spilited_variable_array.length) == 2) { row_count = (_k = data[spilited_variable_array[0]][spilited_variable_array[1]]) === null || _k === void 0 ? void 0 : _k.length; } } else { row_count = (_l = data[variable_array]) === null || _l === void 0 ? void 0 : _l.length; } for (let i = 0; i < row_count; i++) { body_row_init.push("[" + i + "]" + body_row.slice(((body_row === null || body_row === void 0 ? void 0 : body_row.indexOf(']')) + 1))); } } try { for (var _p = true, body_row_init_1 = (e_2 = void 0, __asyncValues(body_row_init)), body_row_init_1_1; body_row_init_1_1 = await body_row_init_1.next(), _d = body_row_init_1_1.done, !_d; _p = true) { _f = body_row_init_1_1.value; _p = false; let body_row_init_row = _f; let replacedRow = []; let index = 0; if (body_row_init_row === null || body_row_init_row === void 0 ? void 0 : body_row_init_row.includes("[")) { index = parseInt(body_row_init_row === null || body_row_init_row === void 0 ? void 0 : body_row_init_row.slice(1, ((body_row_init_row === null || body_row_init_row === void 0 ? void 0 : body_row_init_row.indexOf(']')) + 1))); body_row_init_row = body_row_init_row === null || body_row_init_row === void 0 ? void 0 : body_row_init_row.slice((body_row_init_row === null || body_row_init_row === void 0 ? void 0 : body_row_init_row.indexOf(']')) + 1); } let row_array = body_row_init_row.split("\\"); try { for (var _q = true, row_array_1 = (e_3 = void 0, __asyncValues(row_array)), row_array_1_1; row_array_1_1 = await row_array_1.next(), _g = row_array_1_1.done, !_g; _q = true) { _j = row_array_1_1.value; _q = false; const row_splited = _j; let updated_row = row_splited; if ((updated_row === null || updated_row === void 0 ? void 0 : updated_row.length) > 2) { let row_data = data; if (updated_row[0] == "{" && updated_row[(updated_row === null || updated_row === void 0 ? void 0 : updated_row.length) - 1] == "}") { let variable = variables[updated_row.slice(1, ((updated_row === null || updated_row === void 0 ? void 0 : updated_row.length) - 1))]; if (variable === null || variable === void 0 ? void 0 : variable.includes("~")) { let variable_array_index = variable === null || variable === void 0 ? void 0 : variable.split("~"); if ((_m = variable_array_index[0]) === null || _m === void 0 ? void 0 : _m.includes("-")) { let array_data_path = variable_array_index[0].split("-"); if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 2) { row_data = row_data[array_data_path[0]][array_data_path[1]][index]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 3) { row_data = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]][index]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 4) { row_data = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]][array_data_path[3]][index]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 5) { row_data = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]][array_data_path[3]][array_data_path[4]][index]; } } else { row_data = row_data[variable]; } variable = variable_array_index[1]; } if (variable === null || variable === void 0 ? void 0 : variable.includes("-")) { let array_data_path = variable.split("-"); if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 2) { updated_row = row_data[array_data_path[0]][array_data_path[1]]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 3) { updated_row = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 4) { updated_row = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]][array_data_path[3]]; } if ((array_data_path === null || array_data_path === void 0 ? void 0 : array_data_path.length) == 5) { updated_row = row_data[array_data_path[0]][array_data_path[1]][array_data_path[2]][array_data_path[3]][array_data_path[4]]; } } else { updated_row = row_data[variable]; } } } replacedRow.push(updated_row); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_q && !_g && (_h = row_array_1.return)) await _h.call(row_array_1); } finally { if (e_3) throw e_3.error; } } replacedBody.push(replacedRow === null || replacedRow === void 0 ? void 0 : replacedRow.join("")); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_p && !_d && (_e = body_row_init_1.return)) await _e.call(body_row_init_1); } finally { if (e_2) throw e_2.error; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_o && !_a && (_b = bodyArray_1.return)) await _b.call(bodyArray_1); } finally { if (e_1) throw e_1.error; } } return replacedBody.join(""); } exports.cidePath = { join: (path) => { return path.join("/"); } }; // Function to generate a random alphanumeric string of a given length const generateRandomAlphanumericString = (length) => { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; const charactersLength = characters.length; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }; exports.generateRandomAlphanumericString = generateRandomAlphanumericString; // method to generate string from object this should be encripted and decript json from string const generateStringFromObject = (object) => { let string = JSON.stringify(object); // Use URL-safe base64 encoding let encryptedString = btoa(string); // Replace URL-unsafe characters with safe alternatives encryptedString = encryptedString.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); return encryptedString; }; exports.generateStringFromObject = generateStringFromObject; const generateObjectFromString = (string) => { // Restore base64 padding and replace safe characters back let paddedString = string.replace(/-/g, '+').replace(/_/g, '/'); // Add padding if needed while (paddedString.length % 4) { paddedString += '='; } let decryptedString = atob(paddedString); let object = JSON.parse(decryptedString); return object; }; exports.generateObjectFromString = generateObjectFromString;