53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
|
import React from 'react'
|
||
|
import parameters from '../load-parameters'
|
||
|
import deepAssign from 'deep-assign'
|
||
|
|
||
|
let
|
||
|
groupedByVariableName = parameters
|
||
|
.filter(p => p && p.variable)
|
||
|
.reduce((acc, p) => {
|
||
|
let variableName = p.variable
|
||
|
if (acc[variableName])
|
||
|
acc[variableName].push(p)
|
||
|
else
|
||
|
acc[variableName] = [p]
|
||
|
return acc
|
||
|
}, {}),
|
||
|
|
||
|
conflictingTags = (tags1, tags2) =>
|
||
|
Object.keys(tags1).reduce((conflicts, k) => {
|
||
|
if (typeof tags2[k] != 'undefined' && tags2[k] !== tags1[k])
|
||
|
conflicts.push(k)
|
||
|
return conflicts
|
||
|
}, []),
|
||
|
|
||
|
groupedMergedVariables =
|
||
|
Object.keys(groupedByVariableName)
|
||
|
.reduce((list, name) => {
|
||
|
let items = groupedByVariableName[name]
|
||
|
/* Les items sont des fragments de variables.
|
||
|
Les premiers fragments vont être fusionnés dans les suivants,
|
||
|
sauf s'il introduit un écrasement d'un tag */
|
||
|
let variableList = items.slice(1).reduce((mergedItems, item) => {
|
||
|
let mergedItem = mergedItems.reduce((final, itemBefore) => {
|
||
|
let oups = conflictingTags(itemBefore.tags, item.tags)
|
||
|
//console.log('conflicts for ', itemBefore.tags, item.tags)
|
||
|
return oups.length ? item : deepAssign({}, item, itemBefore)
|
||
|
},
|
||
|
item)
|
||
|
mergedItems.push(mergedItem)
|
||
|
return mergedItems
|
||
|
},
|
||
|
[items[0]])
|
||
|
return [...variableList, ...list]
|
||
|
}, [])
|
||
|
console.log('groupedMergedVariables', groupedMergedVariables)
|
||
|
|
||
|
|
||
|
|
||
|
export default class Explorer extends React.Component {
|
||
|
render() {
|
||
|
return <div>{JSON.stringify(groupedMergedVariables['agff'], null, 2)}</div>
|
||
|
}
|
||
|
}
|