-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from HarvestProfit/cleaning-up-definitions-code
Cleans up the code a bit and adds a bunch of docs
- Loading branch information
Showing
11 changed files
with
198 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import liquidDefinitions from './liquid'; | ||
import solidDefinitions from './solid'; | ||
import seedDefinitions from './seed'; | ||
import yieldDefinitions from './yield'; | ||
import { UnitRedefinitionError, UndefinedUnitError } from '../errors'; | ||
|
||
export const inflatedUnits = {}; | ||
export const selectableUnitsByGroup = {}; | ||
|
||
function addUnitDefinitionWithoutError(group, name, value, primaryName, fullName) { | ||
if (!inflatedUnits[name]) { | ||
inflatedUnits[name] = { | ||
group, | ||
value, | ||
primaryName, | ||
fullName | ||
}; | ||
} | ||
} | ||
|
||
function addUnitDefinition(group, name, value, primaryName, fullName) { | ||
if (inflatedUnits[name]) { | ||
throw new UnitRedefinitionError(`${name} is already a defined unit. Do not redefine units`); | ||
} | ||
|
||
addUnitDefinitionWithoutError(group, name, value, primaryName, fullName); | ||
} | ||
|
||
// Used to expand the definitions provided into a more robust object with multiple lookup keys. | ||
// allows looking up units by their short name, plural name, or any alias. | ||
// Additionally, it builds a list of common selectable units. | ||
function inflateUnits(compatibilityGroup, definitions) { | ||
Object.keys(definitions).forEach((unitName) => { | ||
const definition = definitions[unitName]; | ||
addUnitDefinition(compatibilityGroup, unitName, definition.value, unitName, definition.name); | ||
addUnitDefinitionWithoutError(compatibilityGroup, definition.name, definition.value, unitName, definition.name); | ||
const plural = definition.name + (definition.name[definition.name.length - 1] === 's' ? 'es' : 's'); | ||
addUnitDefinitionWithoutError(compatibilityGroup, plural, definition.value, unitName, definition.name); | ||
|
||
|
||
(definition.aliases || []).forEach((aliasName) => { | ||
addUnitDefinition(compatibilityGroup, aliasName, definition.value, unitName, definition.name); | ||
const plural = aliasName + (aliasName[aliasName.length - 1] === 's' ? 'es' : 's'); | ||
addUnitDefinitionWithoutError(compatibilityGroup, plural, definition.value, unitName, definition.name); | ||
}); | ||
|
||
if (definition.selectableAs) { | ||
selectableUnitsByGroup[compatibilityGroup] = selectableUnitsByGroup[compatibilityGroup] || []; | ||
selectableUnitsByGroup[compatibilityGroup].push(definition.selectableAs); | ||
} | ||
}); | ||
} | ||
|
||
|
||
inflateUnits('liquid', liquidDefinitions); | ||
inflateUnits('weight', solidDefinitions); | ||
inflateUnits('seed', seedDefinitions); | ||
inflateUnits('yield', yieldDefinitions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
/* | ||
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 { | ||
l: { | ||
name: 'liter', | ||
aliases: ['litre'], | ||
value: 1, | ||
selectableAs: 'liters', | ||
}, | ||
ml: { | ||
name: 'milliliter', | ||
value: 0.001, | ||
selectableAs: 'milliliters', | ||
}, | ||
pt: { | ||
name: 'pint', | ||
value: 0.473176473, | ||
selectableAs: 'pints', | ||
}, | ||
qt: { | ||
name: 'quart', | ||
value: 0.946352946, | ||
selectableAs: 'quarts', | ||
}, | ||
gal: { | ||
name: 'gallon', | ||
value: 3.785411784, | ||
selectableAs: 'gallons', | ||
}, | ||
floz: { | ||
name: 'fluid ounce', | ||
aliases: ['fl oz'], | ||
value: 0.02957353, | ||
selectableAs: 'floz', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
/* | ||
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 { | ||
seed: { | ||
name: 'seed', | ||
value: 1, | ||
selectableAs: 'seeds' | ||
}, | ||
bag: { | ||
name: 'bag', | ||
value: 80000, | ||
selectableAs: 'bags' | ||
}, | ||
'units - 130k': { | ||
name: 'units - 130k', | ||
aliases: ['units130k'], | ||
value: 130000, | ||
selectableAs: 'units - 130k' | ||
}, | ||
'units - 140k': { | ||
name: 'units - 140k', | ||
aliases: ['units140k'], | ||
value: 140000, | ||
selectableAs: 'units - 140k', | ||
}, | ||
} |
Oops, something went wrong.