Skip to content

Commit

Permalink
feat: for missing projections fallback to unknown secondary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Oct 8, 2020
1 parent f5a78cc commit 2cc07af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function projectionReducer(location, targetAltitudePointer, result) {
.sort((a, b) => a.altitudePointer - b.altitudePointer)
.reverse()
.find(({ altitudePointer }) => altitudePointer <= targetAltitudePointer);

if (projection && typeof projection.forestType === 'string') {
const { altitudinalZone, forestType } = projection;
result.projections.push({ altitudinalZone, forestType });
Expand Down Expand Up @@ -101,6 +102,16 @@ function project(location = {}, targetAltitude, previousResult) {
if (previous && previous.altitudinalZone === last.altitudinalZone) {
// Could not find projection to targetAltitude
result.projections = [];

// Try to find projection using "unknown" for secondary fields.
for (let i = 0; i < secondaryFields.length; i += 1) {
const secondaryField = secondaryFields[i];
if (location[secondaryField] !== 'unknown') {
location[secondaryField] = 'unknown';
result = project(location, targetAltitude, result);
break;
}
}
} else if (lastAltitudeIdx < targetAltitudeIdx) {
result = project({ ...location, ...last }, targetAltitude, result);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/test/project.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ describe('valid projections', () => {
).toStrictEqual([{ altitudinalZone: '80', forestType: '50*' }]);
});

test('projection with fallback to unknown secondary field (silverFirArea)', () => {
expect(
project(
{
forestEcoregion: '2a',
altitudinalZone: '80',
forestType: '50*',
silverFirArea: '2',
},
'50',
).projections.slice(-1)[0],
).toStrictEqual({ altitudinalZone: '50', forestType: '12a' });
});

test('projection with valid transitionForestType', () => {
expect(
project(
Expand Down

0 comments on commit 2cc07af

Please sign in to comment.