ndc-suez
Version:
Generate standard ndc UI
197 lines • 7.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validationParams = exports.getTokenFullyWrapped = exports.extractUniqueTokens = exports.tooltipParams = exports.deduplicateNestedKeys = exports.transformGraphSheetToJson = exports.filterByScreenWithHeader2 = void 0;
const XLSX = require("xlsx");
/**
* Filters rows where the "Ecran" column contains a given value using the second row as the header.
*
* @param worksheet - The worksheet to process.
* @param filterValue - The value to filter in the "Ecran" column.
* @param takeAll
* @returns The filtered rows as an array of objects.
*/
function filterByScreenWithHeader2(worksheet, filterValue, takeAll = false) {
// Convert the worksheet to JSON using the second row as the header
const data = XLSX.utils.sheet_to_json(worksheet, { header: 2 });
// Ensure the sheet has rows to process
if (data.length === 0) {
throw new Error(`The provided worksheet has no data.`);
}
// Check if the "Ecran" column exists
if (!data[0].hasOwnProperty("Ecran") && !takeAll) {
console.log(data[0]);
throw new Error(`Column "Ecran" not found in the worksheet ${worksheet}`);
}
// Filter rows where the "Ecran" column contains the specified value
const filteredRows = data.filter((row) => takeAll || row["Ecran"]?.includes(filterValue) || row["Ecran"] == "ALL");
return filteredRows;
}
exports.filterByScreenWithHeader2 = filterByScreenWithHeader2;
function transformGraphSheetToJson(worksheet) {
let variable = '';
let unit = '';
let min = undefined;
let max = undefined;
let interval = undefined;
let customAxisLabel = undefined;
const groupBy = (arr, property) => {
return arr.reduce((result, item) => {
const key = item[property];
if (key === undefined || key === null || key === '') {
return result;
}
// remove the group key from each item
const { [property]: _, ...rest } = item;
if (!result[key]) {
result[key] = [];
}
result[key].push(rest);
return result;
}, {});
};
const graphData = XLSX.utils.sheet_to_json(worksheet, { header: 2 });
// fill in blanks for merged cells
for (let graph of graphData) {
if (graph.hasOwnProperty('Variable')) {
variable = graph.Variable;
}
else {
graph['Variable'] = variable;
}
if (graph.hasOwnProperty('Unit')) {
unit = graph.Unit;
}
else {
graph['Unit'] = unit;
}
if (graph.hasOwnProperty('Min')) {
min = graph.Min;
}
else if (graph.Type == 'S') {
graph['Min'] = min;
}
if (graph.hasOwnProperty('Max')) {
max = graph.Max;
}
else if (graph.Type == 'S') {
graph['Max'] = max;
}
if (graph.hasOwnProperty('Interval')) {
interval = graph.Interval;
}
else if (['S', 'X'].includes(graph.Type)) {
graph['Interval'] = interval;
}
if (graph.hasOwnProperty('CustomAxisLabel')) {
customAxisLabel = graph.CustomAxisLabel;
}
else {
graph['CustomAxisLabel'] = customAxisLabel;
}
}
return groupBy(graphData, 'Variable');
}
exports.transformGraphSheetToJson = transformGraphSheetToJson;
function deduplicateNestedKeys(obj, changes) {
const keysByAlerte = {};
// const changes = {};
// Helper function to extract alert level from value string
function getAlerteLevel(value) {
const match = value.match(/alerte\s*=\s*(\d+)/);
return match ? match[1] : null;
}
// First pass: group keys by their alert levels
for (const parentKey in obj) {
for (const nestedKey in obj[parentKey]) {
const value = obj[parentKey][nestedKey];
const alerteLevel = getAlerteLevel(value);
if (!keysByAlerte[nestedKey]) {
keysByAlerte[nestedKey] = {};
}
if (!keysByAlerte[nestedKey][alerteLevel]) {
keysByAlerte[nestedKey][alerteLevel] = [];
}
keysByAlerte[nestedKey][alerteLevel].push(parentKey);
}
}
// Second pass: rename only keys that appear with different alert levels
for (const nestedKey in keysByAlerte) {
const alerteLevels = Object.keys(keysByAlerte[nestedKey]);
// Only rename if this key appears with multiple different alert levels
if (alerteLevels.length > 1) {
for (const alerteLevel in keysByAlerte[nestedKey]) {
const parentKeys = keysByAlerte[nestedKey][alerteLevel];
for (const parentKey of parentKeys) {
const nestedObj = obj[parentKey];
const newKey = `${parentKey}_${nestedKey}`;
nestedObj[newKey] = nestedObj[nestedKey];
delete nestedObj[nestedKey];
changes[newKey] = nestedKey;
}
}
}
}
return changes;
}
exports.deduplicateNestedKeys = deduplicateNestedKeys;
function tooltipParams(tooltips, rowTooltip) {
const lineTooltip = tooltips.find(item => item.Name?.trim() == rowTooltip?.trim());
if (lineTooltip && lineTooltip.Fr && lineTooltip.Fr.indexOf('##') != -1) {
const tokens = extractUniqueTokens(lineTooltip.Fr)
.filter(x => !(x.endsWith('_lbl') || x.indexOf('_lbox_') !== -1));
return tokens.length == 0 ? undefined : tokens;
}
return undefined;
}
exports.tooltipParams = tooltipParams;
function extractUniqueTokens(input) {
const regex = /##(.*?)##/g;
const result = new Set();
let match;
while ((match = regex.exec(input)) !== null) {
result.add(match[1]);
}
return Array.from(result);
}
exports.extractUniqueTokens = extractUniqueTokens;
// get string only if the whole strings is between ## and ##
function getTokenFullyWrapped(value) {
const match = /^##(.+?)##$/.exec(value);
return match ? match[1] : null;
}
exports.getTokenFullyWrapped = getTokenFullyWrapped;
/**
* ODP_mess_err_nobloq_9:alerte=2 ET ODP_NH4EB_E > 0.01 * ODP_TntksolEB * ODP_NtkEB ET ODP_TntksolEB!=VIDE;blocking_key: rule
* result : [ODP_mess_err_nobloq_9, blocking_key]
* @param input
*/
function extractMessageKeysFromString(input) {
return input
.split(';')
.map(part => part.trim())
.filter(Boolean)
.map(part => part.split(':', 1)[0].trim())
.filter(Boolean);
}
/**
*
* @param messages => merge blocking and nonblocking sheet
* @param rowMessage => concat blocking and nonBlocking with ";"
* @param exclude exclude list
*/
function validationParams(messages, rowMessage, exclude) {
const result = [];
const blockingNonBlockingKeys = extractMessageKeysFromString(rowMessage);
for (const msgKey of blockingNonBlockingKeys) {
const nope = exclude.map(x => x.Name);
const lineMessages = messages.find(msg => msg.Name?.trim() == msgKey.trim());
if (lineMessages && lineMessages.Fr && lineMessages.Fr.indexOf('##') != -1) {
const tokens = extractUniqueTokens(lineMessages.Fr)
.filter(x => !(x.endsWith('_lbl') || x.indexOf('_lbox_') !== -1 || nope.includes(x)));
result.push(...tokens);
}
}
return result.length == 0 ? undefined : result;
}
exports.validationParams = validationParams;
//# sourceMappingURL=utils.js.map