@@ -16,8 +16,8 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
16
16
return indexedData [ entityName ] ;
17
17
}
18
18
19
- function getEntityIndicator ( entityName , indicatorID ) {
20
- if ( indexedData [ entityName ] . user && indexedData [ entityName ] . user [ indicatorID ] ) {
19
+ function getEntityIndicator ( entityName , indicatorID ) {
20
+ if ( indexedData [ entityName ] . user && indexedData [ entityName ] . user [ indicatorID ] ) {
21
21
return indexedData [ entityName ] . user [ indicatorID ] ;
22
22
}
23
23
return indexedData [ entityName ] [ indicatorID ] ;
@@ -31,7 +31,7 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
31
31
return indicatorLookup [ id ] ;
32
32
}
33
33
34
- function getIndicatorLookup ( ) {
34
+ function getIndicatorLookup ( ) {
35
35
return indicatorLookup ;
36
36
}
37
37
@@ -73,38 +73,37 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
73
73
indicator . max ? Number ( indicator . max ) : max ,
74
74
] ;
75
75
76
- if ( diverging ) {
77
- let centerpoint = 0 ; // currently no way to set this diffeently included here as a signpost for the future
76
+ if ( diverging ) {
77
+ // currently no way to set this diffeently included here as a signpost for the future
78
+ const centerpoint = 0 ;
78
79
79
- if ( indicator . max ) {
80
+ if ( indicator . max ) {
80
81
range = [ 0 , indicator . max ] ;
81
- if ( indicator . min ) {
82
- range = [ 0 , Math . max ( Math . abs ( indicator . min ) , indicator . max ) ] ;
82
+ if ( indicator . min ) {
83
+ range = [ 0 , Math . max ( Math . abs ( indicator . min ) , indicator . max ) ] ;
83
84
}
84
- } else {
85
+ } else {
85
86
range = [ 0 , max ] ;
86
87
}
87
88
value = Math . abs ( value - centerpoint ) ;
88
89
}
89
90
90
-
91
91
return {
92
92
id : indicator . id ,
93
93
value,
94
94
weight : indicator . userWeighting
95
95
? Number ( indicator . userWeighting )
96
96
: Number ( indicator . weighting ) ,
97
- invert : ! ! indicator . invert ,
98
- range
97
+ invert : indicator . invert === true || indicator . invert . toLowerCase ( ) === 'true' ,
98
+ range,
99
99
} ;
100
100
}
101
101
102
102
function indexEntity ( entity , calculationList , overwrite = allowOverwrite ) {
103
103
const newEntity = clone ( entity ) ;
104
104
calculationList . forEach ( ( indicatorID ) => {
105
- if ( newEntity [ indicatorID ] && overwrite === true
106
- || ! newEntity [ indicatorID ] )
107
- {
105
+ if ( ( newEntity [ indicatorID ] && overwrite === true )
106
+ || ! newEntity [ indicatorID ] ) {
108
107
// get the required component indicators to calculate the parent value
109
108
// this is a bit brittle maybe?
110
109
const componentIndicators = indicatorsData
@@ -115,8 +114,8 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
115
114
// calculate the weighted mean of the component indicators on the newEntity
116
115
// assign that value to the newEntity
117
116
newEntity [ indicatorID ] = calculateWeightedMean ( componentIndicators , indexMax ) ;
118
- } else {
119
- console . log ( `retaining existing value for ${ newEntity . name } - ${ indicatorID } : ${ Number ( entity [ indicatorID ] ) } ` )
117
+ } else {
118
+ console . warn ( `retaining existing value for ${ newEntity . name } - ${ indicatorID } : ${ Number ( entity [ indicatorID ] ) } ` ) ;
120
119
newEntity [ indicatorID ] = Number ( entity [ indicatorID ] ) ;
121
120
}
122
121
} ) ;
@@ -126,39 +125,53 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
126
125
. map ( ( indicator ) => formatIndicator ( indicator , newEntity , indexMax ) ) ;
127
126
128
127
newEntity . value = calculateWeightedMean ( pillarIndicators , indexMax ) ;
129
- if ( ! newEntity . user ) {
128
+ if ( ! newEntity . user ) {
130
129
newEntity . user = { } ;
131
130
}
132
131
return newEntity ;
133
132
}
134
133
134
+ function getIndexableIndicators ( ) {
135
+ return indicatorsData
136
+ . filter ( ( i ) => {
137
+ const isIndicator = String ( i . id ) . match ( indicatorIdTest ) ;
138
+ const isExcluded = excludeIndicator ( i ) ;
139
+ return isIndicator && ! isExcluded ;
140
+ } ) ;
141
+ }
142
+
143
+ function getCalculationList ( indicators ) {
144
+ return indicators
145
+ . filter ( ( i ) => ( i . type === 'calculated' && ! excludeIndicator ( i ) ) )
146
+ . map ( ( i ) => i . id )
147
+ . sort ( ( i1 , i2 ) => ( i2 . split ( '.' ) . length - i1 . split ( '.' ) . length ) ) ;
148
+ }
135
149
136
150
function adjustValue ( entityName , indicatorID , value ) {
137
151
const e = getEntity ( entityName ) ;
138
152
139
- if ( ! indicatorID && ! value || ! e . user ) {
140
- e . user = { } ; // no value or indicator specified, reset
141
- } else if ( ! value && e . user ) {
153
+ if ( ( ! indicatorID && ! value ) || ! e . user ) {
154
+ e . user = { } ; // no value or indicator specified, reset
155
+ } else if ( ! value && e . user ) {
142
156
delete e . user [ indicatorID ] ; // no value specified, reset the indicator
143
157
}
144
-
145
158
146
159
if ( indicatorLookup [ indicatorID ] && indicatorLookup [ indicatorID ] . type === 'calculated' ) {
147
160
console . warn ( `${ indicatorID } is a calculated value and can not be adjusted directly, perhaps you meant to adjust the weighting?` ) ;
148
161
return clone ( e ) ;
149
162
}
150
- if ( indicatorID !== undefined && value !== undefined ) {
163
+ if ( indicatorID !== undefined && value !== undefined ) {
151
164
e . user [ indicatorID ] = value ;
152
165
}
153
166
154
167
const onlyIdIndicators = getIndexableIndicators ( indicatorsData ) ;
155
168
const calculationList = getCalculationList ( onlyIdIndicators ) ;
156
-
169
+
157
170
indexedData [ e . name ] = indexEntity ( e , calculationList , true ) ;
158
171
// console.log(indexedData[e.name])
159
- const adjustedEntity = Object . assign ( clone ( indexedData [ e . name ] ) , indexedData [ e . name ] . user )
160
- delete adjustedEntity . user ;
161
- delete adjustedEntity . data ;
172
+ const adjustedEntity = Object . assign ( clone ( indexedData [ e . name ] ) , indexedData [ e . name ] . user ) ;
173
+ delete adjustedEntity . user ;
174
+ delete adjustedEntity . data ;
162
175
return adjustedEntity ;
163
176
}
164
177
@@ -189,22 +202,6 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
189
202
return tree ;
190
203
}
191
204
192
- function getCalculationList ( indicators ) {
193
- return indicators
194
- . filter ( ( i ) => ( i . type === 'calculated' && ! excludeIndicator ( i ) ) )
195
- . map ( ( i ) => i . id )
196
- . sort ( ( i1 , i2 ) => ( i2 . split ( '.' ) . length - i1 . split ( '.' ) . length ) ) ;
197
- }
198
-
199
- function getIndexableIndicators ( indicators ) {
200
- return indicatorsData
201
- . filter ( ( i ) => {
202
- const isIndicator = String ( i . id ) . match ( indicatorIdTest )
203
- const isExcluded = excludeIndicator ( i ) ;
204
- return isIndicator && ! isExcluded ;
205
- } ) ;
206
- }
207
-
208
205
function calculateIndex ( overwrite = allowOverwrite ) {
209
206
// get a list of the values we need to calculate
210
207
// in order of deepest in the heirachy to the shallowist
@@ -227,9 +224,9 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
227
224
calculateIndex ( true ) ;
228
225
}
229
226
230
- function filterIndicators ( exclude = ( ) => false , overwrite = allowOverwrite ) {
227
+ function filterIndicators ( exclude = ( ) => false , overwrite = allowOverwrite ) {
231
228
excludeIndicator = exclude ;
232
- calculateIndex ( overwrite ) ;
229
+ calculateIndex ( overwrite ) ;
233
230
}
234
231
235
232
calculateIndex ( allowOverwrite ) ;
0 commit comments