Skip to content

Commit 7bf0178

Browse files
author
Tom Pearson
authored
Fix boolean coercion bug (#20)
* fix that bug! * fix linting and add it to the CI process * bump version
1 parent 2353b74 commit 7bf0178

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ jobs:
2828
cache: 'npm'
2929
- run: npm ci
3030
- run: npm test
31+
- run: npm run lint

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@economist/index-core",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"description": "",
55
"main": "src/index-core.js",
66
"type": "module",

src/index-core.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
1616
return indexedData[entityName];
1717
}
1818

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]) {
2121
return indexedData[entityName].user[indicatorID];
2222
}
2323
return indexedData[entityName][indicatorID];
@@ -31,7 +31,7 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
3131
return indicatorLookup[id];
3232
}
3333

34-
function getIndicatorLookup(){
34+
function getIndicatorLookup() {
3535
return indicatorLookup;
3636
}
3737

@@ -73,38 +73,37 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
7373
indicator.max ? Number(indicator.max) : max,
7474
];
7575

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;
7879

79-
if (indicator.max){
80+
if (indicator.max) {
8081
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)];
8384
}
84-
}else{
85+
} else {
8586
range = [0, max];
8687
}
8788
value = Math.abs(value - centerpoint);
8889
}
8990

90-
9191
return {
9292
id: indicator.id,
9393
value,
9494
weight: indicator.userWeighting
9595
? Number(indicator.userWeighting)
9696
: Number(indicator.weighting),
97-
invert: !!indicator.invert,
98-
range
97+
invert: indicator.invert === true || indicator.invert.toLowerCase() === 'true',
98+
range,
9999
};
100100
}
101101

102102
function indexEntity(entity, calculationList, overwrite = allowOverwrite) {
103103
const newEntity = clone(entity);
104104
calculationList.forEach((indicatorID) => {
105-
if (newEntity[indicatorID] && overwrite === true
106-
|| !newEntity[indicatorID])
107-
{
105+
if ((newEntity[indicatorID] && overwrite === true)
106+
|| !newEntity[indicatorID]) {
108107
// get the required component indicators to calculate the parent value
109108
// this is a bit brittle maybe?
110109
const componentIndicators = indicatorsData
@@ -115,8 +114,8 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
115114
// calculate the weighted mean of the component indicators on the newEntity
116115
// assign that value to the newEntity
117116
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])}`);
120119
newEntity[indicatorID] = Number(entity[indicatorID]);
121120
}
122121
});
@@ -126,39 +125,53 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
126125
.map((indicator) => formatIndicator(indicator, newEntity, indexMax));
127126

128127
newEntity.value = calculateWeightedMean(pillarIndicators, indexMax);
129-
if(!newEntity.user){
128+
if (!newEntity.user) {
130129
newEntity.user = {};
131130
}
132131
return newEntity;
133132
}
134133

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+
}
135149

136150
function adjustValue(entityName, indicatorID, value) {
137151
const e = getEntity(entityName);
138152

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) {
142156
delete e.user[indicatorID]; // no value specified, reset the indicator
143157
}
144-
145158

146159
if (indicatorLookup[indicatorID] && indicatorLookup[indicatorID].type === 'calculated') {
147160
console.warn(`${indicatorID} is a calculated value and can not be adjusted directly, perhaps you meant to adjust the weighting?`);
148161
return clone(e);
149162
}
150-
if(indicatorID !== undefined && value !== undefined){
163+
if (indicatorID !== undefined && value !== undefined) {
151164
e.user[indicatorID] = value;
152165
}
153166

154167
const onlyIdIndicators = getIndexableIndicators(indicatorsData);
155168
const calculationList = getCalculationList(onlyIdIndicators);
156-
169+
157170
indexedData[e.name] = indexEntity(e, calculationList, true);
158171
// 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;
162175
return adjustedEntity;
163176
}
164177

@@ -189,22 +202,6 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
189202
return tree;
190203
}
191204

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-
208205
function calculateIndex(overwrite = allowOverwrite) {
209206
// get a list of the values we need to calculate
210207
// in order of deepest in the heirachy to the shallowist
@@ -227,9 +224,9 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
227224
calculateIndex(true);
228225
}
229226

230-
function filterIndicators(exclude = ()=>false, overwrite=allowOverwrite){
227+
function filterIndicators(exclude = () => false, overwrite = allowOverwrite) {
231228
excludeIndicator = exclude;
232-
calculateIndex(overwrite);
229+
calculateIndex(overwrite);
233230
}
234231

235232
calculateIndex(allowOverwrite);

0 commit comments

Comments
 (0)