Skip to content

Commit

Permalink
Merge pull request #43 from HarvestProfit/acres-units
Browse files Browse the repository at this point in the history
adds area based units and ensures a default selectable unit
  • Loading branch information
humphreyja authored May 16, 2021
2 parents a204c84 + cf31049 commit df7053b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harvest-profit/units",
"version": "1.4.3",
"version": "1.4.4",
"description": "Units helper for Harvest Profit javascript applications",
"main": "dist/index.js",
"repository": "https://github.com/HarvestProfit/harvest-profit-units",
Expand Down
7 changes: 6 additions & 1 deletion src/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ class Units {
}

// Given a compatibility group name, it will return the common units in that group.
// group names include: weight, liquid, seed, and yield
// group names include: weight, liquid, seed, area, and yield
static selectableUnits(group) {
return selectableUnitsByGroup[group];
}

// Given a compatibility group name, it willl return the default unit
static defaultSelectableUnit(group) {
return Units.selectableUnits(group)[0];
}

// Checks if 2 units are compatible. Units may be a Units object.
static isCompatible(unit1, unit2) {
checkUnitValidity(unit1);
Expand Down
21 changes: 21 additions & 0 deletions src/definitions/area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
key = common name/short name
name = full name of unit. A plural version of this is also added
aliases = other names for unit (tonnes and metric tons for example). Plural versions of these are also added
value = conversion number. If liters have a value of 1, then milliliters have a value of 0.001 (there is 0.001 liters in 1 milliliter)
selectableAs = the name that shows up in the list of units that we want to be able to select from. Not every unit should go on here, just the common ones
*/

export default {
ac: {
name: 'acre',
value: 1,
selectableAs: 'acres',
default: true,
},
ha: {
name: 'hectare',
value: 2.4710562857,
selectableAs: 'hectare',
}
}
9 changes: 8 additions & 1 deletion src/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import liquidDefinitions from './liquid';
import solidDefinitions from './solid';
import seedDefinitions from './seed';
import yieldDefinitions from './yield';
import areaDefinitions from './area';
import { UnitRedefinitionError, UndefinedUnitError } from '../errors';

export const inflatedUnits = {};
Expand Down Expand Up @@ -46,7 +47,12 @@ function inflateUnits(compatibilityGroup, definitions) {

if (definition.selectableAs) {
selectableUnitsByGroup[compatibilityGroup] = selectableUnitsByGroup[compatibilityGroup] || [];
selectableUnitsByGroup[compatibilityGroup].push(definition.selectableAs);
if (definition.default) {
selectableUnitsByGroup[compatibilityGroup].unshift(definition.selectableAs);
} else {
selectableUnitsByGroup[compatibilityGroup].push(definition.selectableAs);
}

}
});
}
Expand All @@ -56,3 +62,4 @@ inflateUnits('liquid', liquidDefinitions);
inflateUnits('weight', solidDefinitions);
inflateUnits('seed', seedDefinitions);
inflateUnits('yield', yieldDefinitions);
inflateUnits('area', areaDefinitions);
1 change: 1 addition & 0 deletions src/definitions/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default {
name: 'gallon',
value: 3.785411784,
selectableAs: 'gallons',
default: true,
},
floz: {
name: 'fluid ounce',
Expand Down
3 changes: 2 additions & 1 deletion src/definitions/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default {
seed: {
name: 'seed',
value: 1,
selectableAs: 'seeds'
selectableAs: 'seeds',
default: true,
},
bag: {
name: 'bag',
Expand Down
1 change: 1 addition & 0 deletions src/definitions/solid.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ export default {
aliases: ['lb'],
value: 453.592375,
selectableAs: 'lbs',
default: true,
},
}
22 changes: 22 additions & 0 deletions test/UnitsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ describe('UnitsHelper', () => {
});
});

describe('default selectable unit', () => {
it('should return gallons for liquid units', () => {
expect(Units.defaultSelectableUnit('liquid')).toEqual('gallons');
});

it('should return lbs for weight units', () => {
expect(Units.defaultSelectableUnit('weight')).toEqual('lbs');
});

it('should return seeds for seed units', () => {
expect(Units.defaultSelectableUnit('seed')).toEqual('seeds');
});

it('should return bushels for yield units', () => {
expect(Units.defaultSelectableUnit('yield')).toEqual('bushels');
});

it('should return acres for area units', () => {
expect(Units.defaultSelectableUnit('area')).toEqual('acres');
});
});

describe('list available units', () => {
it('should list available solid units when unit is "lb"', () => {
const units = UnitsHelper.listAvailableUnits({ units: 'lb' });
Expand Down

0 comments on commit df7053b

Please sign in to comment.