Skip to content

Commit

Permalink
Adjusted value return (#19)
Browse files Browse the repository at this point in the history
* improve return object from adjustValue
  • Loading branch information
Tom Pearson authored Mar 9, 2022
1 parent 43977e8 commit 2353b74
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ __allowOverwrite__, optional. By default this is set to true ensuring that all c


### indexCore.__adjustValue(_entityName:String_, _indicatorID:String_, _value:Number_)__
A function that allows the user to adjust an entity's (_entityName_) indicator (_indicatorID_) score to a specified _value_. Calculated values for that entity are re-calculated using the new value. If the function is called without _value_ the indicator is reset to it's inital value. If the function is called without _indicatorId_ or _value_ all indicators on the entity are reset to their initial values.
A function that allows the user to adjust an entity's (_entityName_) indicator (_indicatorID_) score to a specified _value_. Calculated values for that entity are re-calculated using the new value. If the function is called without _value_ the indicator is reset to it's inital value. If the function is called without _indicatorId_ or _value_ all indicators on the entity are reset to their initial values. As a convenience the function returns an object with all the recalculated values.

_NOTE: whislt the old value is retained there's currently no way to reset it._

Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ const inclusiveInternetEntities = csvParse(fs.readFileSync(`${inclusiveIternetRo
const inclusiveInternetIndex = indexCore(inclusiveInternetIndicators, inclusiveInternetEntities);


console.log('value', inclusiveInternetIndex.indexedData['Singapore']['value'])
console.log('1', inclusiveInternetIndex.indexedData['Singapore']['1'])
console.log('1.2', inclusiveInternetIndex.indexedData['Singapore']['1.2'])
console.log('1.2.1', inclusiveInternetIndex.getEntityIndicator('Singapore','1.2.1'))

inclusiveInternetIndex.adjustValue('Singapore','1.2.1',50);

console.log('adjusted')
console.log('value', inclusiveInternetIndex.indexedData['Singapore']['value'])
console.log('1', inclusiveInternetIndex.indexedData['Singapore']['1'])
console.log('1.2', inclusiveInternetIndex.indexedData['Singapore']['1.2'])
console.log('1.2.1', inclusiveInternetIndex.getEntityIndicator('Singapore','1.2.1'))
// console.log('value', inclusiveInternetIndex.indexedData['Singapore']['value'])
// console.log('1', inclusiveInternetIndex.indexedData['Singapore']['1'])
// console.log('1.2', inclusiveInternetIndex.indexedData['Singapore']['1.2'])
// console.log('1.2.1', inclusiveInternetIndex.getEntityIndicator('Singapore','1.2.1'))

console.log(inclusiveInternetIndex.adjustValue('Singapore','1.2.1',50));

// console.log('adjusted')
// console.log('value', inclusiveInternetIndex.indexedData['Singapore']['value'])
// console.log('1', inclusiveInternetIndex.indexedData['Singapore']['1'])
// console.log('1.2', inclusiveInternetIndex.indexedData['Singapore']['1.2'])
// console.log('1.2.1', inclusiveInternetIndex.getEntityIndicator('Singapore','1.2.1'))

// const simpleRootDir = 'data/simple-index-set';

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@economist/index-core",
"version": "1.5.0",
"version": "1.5.2",
"description": "",
"main": "src/index-core.js",
"type": "module",
Expand Down
6 changes: 5 additions & 1 deletion src/index-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ function indexCore(indicatorsData = [], entitiesData = [], indexMax = 100, allow
const calculationList = getCalculationList(onlyIdIndicators);

indexedData[e.name] = indexEntity(e, calculationList, true);
return indexedData[e.name];
// console.log(indexedData[e.name])
const adjustedEntity = Object.assign(clone(indexedData[e.name]),indexedData[e.name].user)
delete adjustedEntity.user;
delete adjustedEntity.data;
return adjustedEntity;
}

function createStructure(indicatorIds) {
Expand Down
8 changes: 8 additions & 0 deletions test/index-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ test('get the user set value for an indicator', ()=>{
const simpleIndex = indexCore(simpleIndicators, simpleEntities);
simpleIndex.adjustValue('Monopoly', '1.2', 3.142);
expect(simpleIndex.getEntityIndicator('Monopoly','1.2')).toBe(3.142);
})

test('check the return value from adjustValue', ()=>{
const simpleIndex = indexCore(simpleIndicators, simpleEntities);
const adjustedObject = simpleIndex.adjustValue('Monopoly', '1.2', 3.142);
expect(adjustedObject['1.2']).toBe(3.142);
expect(adjustedObject['user']).toBe(undefined);
expect(adjustedObject['data']).toBe(undefined);
})

0 comments on commit 2353b74

Please sign in to comment.