const handle1 = {}; const handle2= {}; function hierachy(data, feed) { if (this.isLast()) { return feed.close(); } if (data.token) { if (data.token[1] === 0) { handle2[data.value.numero_national_de_structure] = data.value.libelle data.value.regroupe.forEach((child)=> { if (!handle1[child]) { handle1[child] = []; } handle1[child].push(data.value.numero_national_de_structure); }) } if (data.token[1] === 1) { const structure_englobante = handle1[data.value.numero_national_de_structure] && handle1[data.value.numero_national_de_structure][0] ? handle1[data.value.numero_national_de_structure][0] : 'n/a'; data.value.structure_englobante = { id: structure_englobante, value: handle2[structure_englobante] }; data.value.structuration_hierachique = [data.value.structure_englobante]; let id = structure_englobante; let i = 0; while (id && handle1[id] && handle1[id][0] && i < 5 ) { data.value.structuration_hierachique.unshift({ id: handle1[id][0], value: handle2[handle1[id][0]] }); id = handle1[id][0]; i++; } data.value.structures_rattachees = data.value.regroupe.map(id => ({ id, value: handle2[id] })); delete data.value.regroupe; } feed.send(data); } } module.exports = { hierachy, };