skydiet-lib-node
Advanced tools
Comparing version 1.0.32 to 1.0.33
@@ -7,3 +7,3 @@ import { IAttendanceQuestion } from "./models/IAttendanceQuestion"; | ||
}; | ||
export declare const generateIdentifiers: (array: any[], fields: string[]) => unknown[]; | ||
export declare const generateIdentifiers: (array: any[], fields: string[]) => any[]; | ||
export declare const group: (items: any[], fieldsToGroup: string[], fieldsToAddInRoot: string[], internalOrders: { | ||
@@ -15,3 +15,3 @@ fields: string[]; | ||
sense: "asc" | "desc"; | ||
}) => { | ||
}, questionsAnswereds: IAttendanceQuestion[]) => { | ||
[key: string]: any; | ||
@@ -21,1 +21,3 @@ id: any; | ||
}[]; | ||
export declare const buildFormula: (questionsAnswereds: IAttendanceQuestion[], formula: string, replaceDefault?: string) => string; | ||
export declare const execFormula: (defaultResult: any, formula: string) => any; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.group = exports.generateIdentifiers = exports.processAnswer = void 0; | ||
exports.execFormula = exports.buildFormula = exports.group = exports.generateIdentifiers = exports.processAnswer = void 0; | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
@@ -45,3 +45,3 @@ const moment_1 = __importDefault(require("moment")); | ||
const generateIdentifiers = (array, fields) => { | ||
const result = new Set(); | ||
const result = new Map(); // Utilizamos um Map para evitar duplicatas | ||
array.forEach(item => { | ||
@@ -51,3 +51,3 @@ // Gera combinações dinâmicas para os campos | ||
if (Array.isArray(item[field])) { | ||
return item[field]; | ||
return item[field] ?? ""; | ||
} | ||
@@ -60,9 +60,9 @@ else if (item[field] !== undefined) { | ||
// Combina todas as variações entre os campos | ||
const allCombinations = combinations.reduce((acc, values) => { | ||
const allCombinations = combinations.reduce((acc, values, index) => { | ||
if (acc.length === 0) | ||
return values; | ||
return values.map((value) => ({ [fields[index]]: value })); | ||
const newCombinations = []; | ||
acc.forEach((accValue) => { | ||
acc.forEach((accCombination) => { | ||
values.forEach((value) => { | ||
newCombinations.push(`${accValue}_${value}`); | ||
newCombinations.push({ ...accCombination, [fields[acc.length]]: value }); | ||
}); | ||
@@ -72,12 +72,29 @@ }); | ||
}, []); | ||
// Adiciona todas as combinações ao resultado | ||
allCombinations.forEach((combination) => result.add(combination)); | ||
// Adiciona ao Map somente combinações únicas com o ID gerado | ||
allCombinations.forEach((combination) => { | ||
const id = fields.map(field => combination[field]).join("_"); | ||
if (!result.has(id)) { | ||
result.set(id, { id, ...combination }); | ||
} | ||
}); | ||
}); | ||
return Array.from(result); | ||
return Array.from(result.values()); // Converte o Map em um array de objetos | ||
}; | ||
exports.generateIdentifiers = generateIdentifiers; | ||
const group = (items, fieldsToGroup, fieldsToAddInRoot, internalOrders, externalOrders) => { | ||
const result = (0, exports.generateIdentifiers)(items, fieldsToGroup); | ||
const groupeds = items.reduce((acc, currentValue, currentIndex, array) => { | ||
const itemAux = {}; | ||
const group = (items, fieldsToGroup, fieldsToAddInRoot, internalOrders, externalOrders, questionsAnswereds) => { | ||
items.forEach((item, index) => { | ||
//MONTAGEM DA FORMULA COM OS VALORES | ||
let formulaToEnable = item.formulaToEnable; | ||
formulaToEnable = (0, exports.buildFormula)(questionsAnswereds, formulaToEnable); | ||
//TENTAR EXECUTAR A FORMULA | ||
let enable = false; | ||
enable = (0, exports.execFormula)(false, formulaToEnable); | ||
if (!enable) { | ||
items.splice(index, 1); | ||
} | ||
}); | ||
// const result = generateIdentifiers(items, fieldsToGroup); | ||
// console.log(result) | ||
const groupeds2 = items.reduce((acc, currentValue, currentIndex, array) => { | ||
let itemAux = {}; | ||
for (const field of fieldsToGroup) { | ||
@@ -94,6 +111,8 @@ if (currentValue[field]) { | ||
const chavesOfItem = (0, exports.generateIdentifiers)([currentValue], fieldsToGroup); | ||
// console.log("chavesOfItem", chavesOfItem) | ||
for (const chave of chavesOfItem) { | ||
const existingGrouped = acc.find((i) => i.id === chave); | ||
const existingGrouped = acc.find((i) => i.id === chave.id); | ||
if (!existingGrouped) { | ||
itemAux.id = chave; | ||
// itemAux.id = chave | ||
itemAux = { ...itemAux, ...chave }; | ||
itemAux.items = []; | ||
@@ -127,3 +146,43 @@ itemAux.items.push(currentValue); | ||
}, []); | ||
groupeds.sort((a, b) => { | ||
// const groupeds = items.reduce((previousValue: any, currentValue: any, currentIndex: number, array: any[]) => { | ||
// const ids = {} as { [key: string]: any, id: any, items: any[] } | ||
// const chaves: any = [] | ||
// for (const field of fieldsToGroup) { | ||
// if (currentValue[field]) { | ||
// chaves.push(currentValue[field].toString()) | ||
// ids[field] = currentValue[field] | ||
// } | ||
// } | ||
// for (const field of fieldsToAddInRoot) { | ||
// if (currentValue[field]) { | ||
// ids[field] = currentValue[field] | ||
// } | ||
// } | ||
// const key = chaves.join("_") | ||
// const existingGrouped = previousValue.find((i: any) => i.id === key) | ||
// if (!existingGrouped) { | ||
// ids.id = key | ||
// ids.items = [] | ||
// ids.items.push(currentValue) | ||
// previousValue.push(ids); | ||
// } else { | ||
// existingGrouped.items.push(currentValue) | ||
// // Ordena os itens dentro do grupo por id | ||
// existingGrouped.items.sort((a: any, b: any) => { | ||
// for (const fieldOrder of internalOrders.fields) { | ||
// if (internalOrders.sense === "asc") { | ||
// if (a[fieldOrder] < b[fieldOrder]) return -1; | ||
// if (a[fieldOrder] > b[fieldOrder]) return 1; | ||
// } else { | ||
// if (a[fieldOrder] > b[fieldOrder]) return -1; | ||
// if (a[fieldOrder] < b[fieldOrder]) return 1; | ||
// } | ||
// } | ||
// return 0; // Se ambos | ||
// }); | ||
// } | ||
// return previousValue | ||
// }, []); | ||
// Ordena os itens dentro do grupo por id | ||
groupeds2.sort((a, b) => { | ||
for (const fieldOrder of externalOrders.fields) { | ||
@@ -145,6 +204,6 @@ if (externalOrders.sense === "asc") { | ||
}); | ||
groupeds.forEach((group) => { | ||
groupeds2.forEach((group) => { | ||
group.items = [...group.items.filter((q) => q.questionParent === undefined)]; | ||
}); | ||
groupeds.forEach((group) => { | ||
groupeds2.forEach((group) => { | ||
group.items.forEach((item) => { | ||
@@ -170,3 +229,4 @@ item.childs?.sort((a, b) => { | ||
}); | ||
return groupeds; | ||
console.log("AGRUPAMENTO na lib", groupeds2); | ||
return groupeds2; | ||
}; | ||
@@ -290,6 +350,6 @@ exports.group = group; | ||
let formulaToEnable = question.formulaToEnable; | ||
formulaToEnable = buildFormula(questionsAnswereds, formulaToEnable); | ||
formulaToEnable = (0, exports.buildFormula)(questionsAnswereds, formulaToEnable); | ||
//TENTAR EXECUTAR A FORMULA | ||
let enable = false; | ||
enable = execFormula(false, formulaToEnable); | ||
enable = (0, exports.execFormula)(false, formulaToEnable); | ||
if (enable) { | ||
@@ -309,6 +369,6 @@ if (question.typeAnswer === IQuestion_1.TYPE_ANSWER.CHOICE_SINGLE || question.typeAnswer === IQuestion_1.TYPE_ANSWER.CHOICE_MULTIPLE) { | ||
let formulaToEnable = question.formulaToEnable; | ||
formulaToEnable = buildFormula(questionsAnswereds, formulaToEnable); | ||
formulaToEnable = (0, exports.buildFormula)(questionsAnswereds, formulaToEnable); | ||
//TENTAR EXECUTAR A FORMULA | ||
let enable = false; | ||
enable = execFormula(false, formulaToEnable); | ||
enable = (0, exports.execFormula)(false, formulaToEnable); | ||
if (enable === false) { | ||
@@ -331,4 +391,4 @@ question.answer = undefined; | ||
let formula = question.formula; | ||
formula = buildFormula(questionsAnswereds, formula); | ||
const answer = execFormula(undefined, formula); | ||
formula = (0, exports.buildFormula)(questionsAnswereds, formula); | ||
const answer = (0, exports.execFormula)(undefined, formula); | ||
question.answer = answer; | ||
@@ -363,8 +423,8 @@ if ((0, c2_util_front_1.isEmpty)(answer) || (question.typeAnswer === IQuestion_1.TYPE_ANSWER.OPEN_NUMBER && isNaN(answer))) { | ||
let formula = question.formula; | ||
formula = buildFormula(questionsAnswereds, formula); | ||
formula = buildFormula(questionsAnswereds, formula); | ||
formula = buildFormula(questionsAnswereds, formula); | ||
formula = (0, exports.buildFormula)(questionsAnswereds, formula); | ||
formula = (0, exports.buildFormula)(questionsAnswereds, formula); | ||
formula = (0, exports.buildFormula)(questionsAnswereds, formula); | ||
formula = buildFormulaMets(questionsAnswereds, formula, metsActivities); | ||
//TENTAR EXECUTAR A FORMULA | ||
const answer = execFormula(undefined, formula); | ||
const answer = (0, exports.execFormula)(undefined, formula); | ||
question.answer = answer; | ||
@@ -412,2 +472,3 @@ if ((0, c2_util_front_1.isEmpty)(answer) || (question.typeAnswer === IQuestion_1.TYPE_ANSWER.OPEN_NUMBER && isNaN(answer))) { | ||
}; | ||
exports.buildFormula = buildFormula; | ||
const buildFormulaMets = (questionsAnswereds, formula, metsActivities) => { | ||
@@ -463,2 +524,3 @@ if ((0, c2_util_front_1.isEmpty)(formula)) { | ||
}; | ||
exports.execFormula = execFormula; | ||
const getOptions = (question, questionsAnswereds) => { | ||
@@ -472,6 +534,6 @@ const optionsEnabled = []; | ||
let formulaToEnableOption = opt.formula; | ||
formulaToEnableOption = buildFormula(questionsAnswereds, formulaToEnableOption); | ||
formulaToEnableOption = (0, exports.buildFormula)(questionsAnswereds, formulaToEnableOption); | ||
//TENTAR EXECUTAR A FORMULA | ||
let enableOption = false; | ||
enableOption = execFormula(true, formulaToEnableOption); | ||
enableOption = (0, exports.execFormula)(true, formulaToEnableOption); | ||
if (enableOption === false) { | ||
@@ -478,0 +540,0 @@ continue; |
{ | ||
"name": "skydiet-lib-node", | ||
"version": "1.0.32", | ||
"version": "1.0.33", | ||
"description": "Biblioteca para funções skydiet", | ||
@@ -5,0 +5,0 @@ "author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>", |
156
src/index.ts
@@ -58,32 +58,38 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
export const generateIdentifiers = (array: any[], fields: string[]) => { | ||
const result = new Set(); | ||
const result = new Map(); // Utilizamos um Map para evitar duplicatas | ||
array.forEach(item => { | ||
// Gera combinações dinâmicas para os campos | ||
const combinations = fields.map(field => { | ||
if (Array.isArray(item[field])) { | ||
return item[field]; | ||
} else if (item[field] !== undefined) { | ||
return [item[field]]; | ||
} | ||
return []; | ||
}); | ||
array.forEach(item => { | ||
// Gera combinações dinâmicas para os campos | ||
const combinations = fields.map(field => { | ||
if (Array.isArray(item[field])) { | ||
return item[field] ?? ""; | ||
} else if (item[field] !== undefined) { | ||
return [item[field]]; | ||
} | ||
return []; | ||
}); | ||
// Combina todas as variações entre os campos | ||
const allCombinations = combinations.reduce((acc, values) => { | ||
if (acc.length === 0) return values; | ||
const newCombinations: any = []; | ||
acc.forEach((accValue: any) => { | ||
values.forEach((value: any) => { | ||
newCombinations.push(`${accValue}_${value}`); | ||
// Combina todas as variações entre os campos | ||
const allCombinations = combinations.reduce((acc, values, index) => { | ||
if (acc.length === 0) return values.map((value: any) => ({ [fields[index]]: value })); | ||
const newCombinations: any[] = []; | ||
acc.forEach((accCombination: any) => { | ||
values.forEach((value: any) => { | ||
newCombinations.push({ ...accCombination, [fields[acc.length]]: value }); | ||
}); | ||
}); | ||
return newCombinations; | ||
}, []); | ||
// Adiciona ao Map somente combinações únicas com o ID gerado | ||
allCombinations.forEach((combination: any) => { | ||
const id = fields.map(field => combination[field]).join("_"); | ||
if (!result.has(id)) { | ||
result.set(id, { id, ...combination }); | ||
} | ||
}); | ||
}); | ||
return newCombinations; | ||
}, []); | ||
}); | ||
// Adiciona todas as combinações ao resultado | ||
allCombinations.forEach((combination: any) => result.add(combination)); | ||
}); | ||
return Array.from(result.values()); // Converte o Map em um array de objetos | ||
return Array.from(result); | ||
} | ||
@@ -96,9 +102,27 @@ | ||
internalOrders: { fields: string[], sense: "asc" | "desc" }, | ||
externalOrders: { fields: string[], sense: "asc" | "desc" }): { [key: string]: any, id: any, items: any[] }[] => { | ||
externalOrders: { fields: string[], sense: "asc" | "desc" }, | ||
questionsAnswereds: IAttendanceQuestion[]): { [key: string]: any, id: any, items: any[] }[] => { | ||
const result = generateIdentifiers(items, fieldsToGroup); | ||
const groupeds = items.reduce((acc: any, currentValue: any, currentIndex: number, array: any[]) => { | ||
const itemAux = {} as { [key: string]: any, id: any, items: any[] } | ||
items.forEach((item: any, index: number) => { | ||
//MONTAGEM DA FORMULA COM OS VALORES | ||
let formulaToEnable: string = item.formulaToEnable | ||
formulaToEnable = buildFormula(questionsAnswereds, formulaToEnable) | ||
//TENTAR EXECUTAR A FORMULA | ||
let enable: boolean = false | ||
enable = execFormula(false, formulaToEnable) | ||
if (!enable) { | ||
items.splice(index, 1) | ||
} | ||
}) | ||
// const result = generateIdentifiers(items, fieldsToGroup); | ||
// console.log(result) | ||
const groupeds2 = items.reduce((acc: any, currentValue: any, currentIndex: number, array: any[]) => { | ||
let itemAux = {} as { [key: string]: any, id: any, items: any[] } | ||
for (const field of fieldsToGroup) { | ||
@@ -116,9 +140,12 @@ if (currentValue[field]) { | ||
const chavesOfItem = generateIdentifiers([currentValue], fieldsToGroup); | ||
// console.log("chavesOfItem", chavesOfItem) | ||
for (const chave of chavesOfItem) { | ||
const existingGrouped = acc.find((i: any) => i.id === chave) | ||
const existingGrouped = acc.find((i: any) => i.id === chave.id) | ||
if (!existingGrouped) { | ||
itemAux.id = chave | ||
// itemAux.id = chave | ||
itemAux = { ...itemAux, ...chave} | ||
itemAux.items = [] | ||
@@ -150,3 +177,53 @@ itemAux.items.push(currentValue) | ||
groupeds.sort((a: any, b: any) => { | ||
// const groupeds = items.reduce((previousValue: any, currentValue: any, currentIndex: number, array: any[]) => { | ||
// const ids = {} as { [key: string]: any, id: any, items: any[] } | ||
// const chaves: any = [] | ||
// for (const field of fieldsToGroup) { | ||
// if (currentValue[field]) { | ||
// chaves.push(currentValue[field].toString()) | ||
// ids[field] = currentValue[field] | ||
// } | ||
// } | ||
// for (const field of fieldsToAddInRoot) { | ||
// if (currentValue[field]) { | ||
// ids[field] = currentValue[field] | ||
// } | ||
// } | ||
// const key = chaves.join("_") | ||
// const existingGrouped = previousValue.find((i: any) => i.id === key) | ||
// if (!existingGrouped) { | ||
// ids.id = key | ||
// ids.items = [] | ||
// ids.items.push(currentValue) | ||
// previousValue.push(ids); | ||
// } else { | ||
// existingGrouped.items.push(currentValue) | ||
// // Ordena os itens dentro do grupo por id | ||
// existingGrouped.items.sort((a: any, b: any) => { | ||
// for (const fieldOrder of internalOrders.fields) { | ||
// if (internalOrders.sense === "asc") { | ||
// if (a[fieldOrder] < b[fieldOrder]) return -1; | ||
// if (a[fieldOrder] > b[fieldOrder]) return 1; | ||
// } else { | ||
// if (a[fieldOrder] > b[fieldOrder]) return -1; | ||
// if (a[fieldOrder] < b[fieldOrder]) return 1; | ||
// } | ||
// } | ||
// return 0; // Se ambos | ||
// }); | ||
// } | ||
// return previousValue | ||
// }, []); | ||
// Ordena os itens dentro do grupo por id | ||
groupeds2.sort((a: any, b: any) => { | ||
for (const fieldOrder of externalOrders.fields) { | ||
@@ -165,7 +242,7 @@ if (externalOrders.sense === "asc") { | ||
groupeds.forEach((group: any) => { | ||
groupeds2.forEach((group: any) => { | ||
group.items = [...group.items.filter((q: IAttendanceQuestion) => q.questionParent === undefined)] | ||
}) | ||
groupeds.forEach((group: any) => { | ||
groupeds2.forEach((group: any) => { | ||
group.items.forEach((item: any) => { | ||
@@ -189,4 +266,5 @@ | ||
}) | ||
console.log("AGRUPAMENTO na lib", groupeds2) | ||
return groupeds | ||
return groupeds2 | ||
} | ||
@@ -459,3 +537,3 @@ | ||
const buildFormula = (questionsAnswereds: IAttendanceQuestion[], formula: string, replaceDefault?: string) => { | ||
export const buildFormula = (questionsAnswereds: IAttendanceQuestion[], formula: string, replaceDefault?: string) => { | ||
if (isEmpty(formula)) { | ||
@@ -529,3 +607,3 @@ return formula; | ||
const execFormula = (defaultResult: any, formula: string) => { | ||
export const execFormula = (defaultResult: any, formula: string) => { | ||
let result = defaultResult; | ||
@@ -532,0 +610,0 @@ try { |
75271
1680