Skip to content

Commit

Permalink
feat: implement buttons for different base unit systems
Browse files Browse the repository at this point in the history
Added to sheet configuration dialog. Added buttons for SI, mm-kg-sec, and in-lbm-sec
  • Loading branch information
mgreminger committed Dec 18, 2023
1 parent a454031 commit 765038f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 22 deletions.
18 changes: 16 additions & 2 deletions src/BaseUnitsConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script lang="ts">
import { ComboBox } from "carbon-components-svelte";
import { ComboBox, ButtonSet, Button } from "carbon-components-svelte";
import CheckmarkOutline from "carbon-icons-svelte/lib/CheckmarkOutline.svelte";
import { mathCellChanged } from "./stores";
import { type CustomBaseUnits, baseUnitChoices, getDefaultBaseUnits } from "./sheet/Sheet";
import { type CustomBaseUnits, baseUnitSystems, baseUnitChoices,
getDefaultBaseUnits, isDefaultBaseUnits } from "./sheet/Sheet";
export let baseUnits: CustomBaseUnits;
Expand All @@ -26,6 +28,18 @@
}
</style>

<ButtonSet>
{#each baseUnitSystems.keys() as name}
<Button
kind="tertiary"
on:click={() => baseUnits = getDefaultBaseUnits(name)}
icon={isDefaultBaseUnits(baseUnits, name) ? CheckmarkOutline : null}
>
{name}
</Button>
{/each}
</ButtonSet>

<div class="container">
{#each baseUnitChoices as dimension (dimension.name)}
<div class="combo-input">
Expand Down
124 changes: 104 additions & 20 deletions src/sheet/Sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,39 +155,123 @@ export type CustomBaseUnits = {
export const baseUnitChoices: {name: string, label: string, choices: string[]}[] = [
{name: 'mass', label: 'Mass', choices: ['kg', 'g', 'mg', 'tonne', 'lbm', 'ton', 'oz'] },
{name: 'length', label: 'Length', choices: ['m', 'mm', 'cm', 'km', 'um', 'nm', 'angstrom', 'in', 'feet', 'yard', 'mile']},
{name: 'time', label: 'Time', choices: ['s', 'ms', 'us', 'ns', 'min', 'hour', 'day', 'week', 'month', 'year']},
{name: 'current', label: 'Electric Current', choices: ['A', 'mA', 'uA', 'kA', 'MA']},
{name: 'temperature', label: 'Temperature', choices: ['K', 'degC', 'degF', 'degR']},
{name: 'luminous_intensity', label: 'Luminous Intensity', choices: ['cd', 'mcd', 'kcd']},
{name: 'amount_of_substance', label: 'Amount of Substance', choices: ['mol', 'kmol', 'mmol']},
{name: 'force', label: 'Force', choices: ['N', 'mN', 'kN', 'lbf', 'kip', 'dyne']},
{name: 'area', label: 'Area', choices: ['m^2', 'cm^2', 'mm^2', 'km^2', 'hectare', 'in^2', 'feet^2', 'yard^2', 'mile^2', 'acre']},
{name: 'volume', label: 'Volume', choices: ['m^3', 'cm^3', 'mm^3', 'km^3', 'liter', 'ml', 'in^3', 'feet^3', 'yard^3', 'mile^3', 'gallon', 'floz']},
{name: 'energy', label: 'Energy', choices: ['J', 'mJ', 'kJ', 'MJ', 'Wh', 'kWh', 'eV', 'BTU', 'hp*hr', 'N*m', 'in*lbf', 'foot*lbf', 'erg']},
{name: 'power', label: 'Power', choices: ['w', 'mW', 'kW', 'MW', 'hp', 'BTU/min', 'BTU/sec', 'erg/sec', 'dyne*cm/sec']},
{name: 'force', label: 'Force', choices: ['N', 'mN', 'kN', 'lbf', 'kip', 'dyne']},
{name: 'pressure', label: 'Pressure', choices: ['Pa', 'kPa', 'MPa', 'psi', 'atm', 'torr', 'bar', 'mmHg', 'mmH2O', 'cmH2O']},
{name: 'angle', label: 'Angle', choices: ['rad', 'deg', 'grad', 'cycle', 'arcsec', 'arcmin']},
{name: 'time', label: 'Time', choices: ['s', 'ms', 'us', 'ns', 'min', 'hour', 'day', 'week', 'month', 'year']},
{name: 'temperature', label: 'Temperature', choices: ['K', 'degC', 'degF', 'degR']},
{name: 'energy', label: 'Energy', choices: ['J', 'mJ', 'kJ', 'MJ', 'Wh', 'kWh', 'eV', 'BTU', 'hp*hr', 'N*m', 'in*lbf', 'foot*lbf', 'erg']},
{name: 'power', label: 'Power', choices: ['W', 'mW', 'kW', 'MW', 'hp', 'BTU/min', 'BTU/sec', 'erg/sec', 'dyne*cm/sec']},
{name: 'charge', label: 'Electric Charge', choices: ['C', 'mC', 'uC', 'nC', 'pC']},
{name: 'capacitance', label: 'Capacitance', choices: ['F', 'mF', 'uF', 'nF', 'pF']},
{name: 'electric_potential', label: 'Electric Potential', choices: ['V', 'mV', 'kV', 'uV', 'MV']},
{name: 'current', label: 'Electric Current', choices: ['A', 'mA', 'uA', 'kA', 'MA']},
{name: 'resistance', label: 'Resistance', choices: ['ohm', 'mohm', 'kohm', 'Mohm', 'Gohm']},
{name: 'capacitance', label: 'Capacitance', choices: ['F', 'mF', 'uF', 'nF', 'pF']},
{name: 'inductance', label: 'Inductance', choices: ['H', 'mH', 'uH', 'nH', 'pH']},
{name: 'conductance', label: 'Conductance', choices: ['S', 'mS', 'kS', 'uS', 'MS']},
{name: 'magnetic_flux', label: 'Magnetic Flux', choices: ['Wb', 'mWb', 'uWb', 'nWb']},
{name: 'magnetic_flux_density', label: 'Magnetic Flux Density', choices: ['T', 'mT', 'uT', 'nT','pT']},
{name: 'angle', label: 'Angle', choices: ['rad', 'deg', 'grad', 'cycle', 'arcsec', 'arcmin']},
{name: 'magnetic_flux_density', label: 'Magnetic Flux Density', choices: ['T', 'mT', 'uT', 'nT','pT']},
{name: 'luminous_intensity', label: 'Luminous Intensity', choices: ['cd', 'mcd', 'kcd']},
{name: 'amount_of_substance', label: 'Amount of Substance', choices: ['mol', 'kmol', 'mmol']},
{name: 'information', label: 'Information', choices: ['b', 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'kb', 'mb', 'Gb', 'Tb', 'Pb']},
];

export function getDefaultBaseUnits(): CustomBaseUnits {
const defaultBaseUnits = {};
export type BaseUnitSystemNames = "SI" | "mm-kg-sec" | "inch-lbm-sec";

for (const dimension of baseUnitChoices) {
defaultBaseUnits[dimension.name] = dimension.choices[0]
}
export const baseUnitSystems = new Map<BaseUnitSystemNames, CustomBaseUnits>([
[
"SI",
{
mass: "kg",
length: "m",
time: "s",
current: "A",
temperature: "K",
luminous_intensity: "cd",
amount_of_substance: "mol",
force: "N",
area: "m^2",
volume: "m^3",
energy: "J",
power: "W",
pressure: "Pa",
charge: "C",
capacitance: "F",
electric_potential: "V",
resistance: "ohm",
inductance: "H",
conductance: "S",
magnetic_flux: "Wb",
magnetic_flux_density: "T",
angle: "rad",
information: "b",
},
],
[
"mm-kg-sec",
{
mass: "kg",
length: "mm",
time: "s",
current: "A",
temperature: "K",
luminous_intensity: "cd",
amount_of_substance: "mol",
force: "N",
area: "mm^2",
volume: "mm^3",
energy: "mJ",
power: "mW",
pressure: "MPa",
charge: "C",
capacitance: "F",
electric_potential: "V",
resistance: "ohm",
inductance: "H",
conductance: "S",
magnetic_flux: "Wb",
magnetic_flux_density: "T",
angle: "rad",
information: "b",
},
],
[
"inch-lbm-sec",
{
mass: "lbm",
length: "in",
time: "s",
current: "A",
temperature: "degR",
luminous_intensity: "cd",
amount_of_substance: "mol",
force: "lbf",
area: "in^2",
volume: "in^3",
energy: "BTU",
power: "hp",
pressure: "psi",
charge: "C",
capacitance: "F",
electric_potential: "V",
resistance: "ohm",
inductance: "H",
conductance: "S",
magnetic_flux: "Wb",
magnetic_flux_density: "T",
angle: "rad",
information: "b",
},
],
]);

return defaultBaseUnits as CustomBaseUnits;
export function getDefaultBaseUnits(system: BaseUnitSystemNames = "SI"): CustomBaseUnits {
return {...baseUnitSystems.get(system)};
}

export function isDefaultBaseUnits(baseUnits: CustomBaseUnits): boolean {
return baseUnitChoices.reduce((acum, value) => acum && value.choices[0] === baseUnits[value.name], true);
}
export function isDefaultBaseUnits(baseUnits: CustomBaseUnits, system: BaseUnitSystemNames = "SI"): boolean {
const defaultBaseUnits = baseUnitSystems.get(system);
return Object.entries(defaultBaseUnits).reduce((acum, [key, value]) => acum && value === baseUnits[key], true);
}

0 comments on commit 765038f

Please sign in to comment.