Skip to content

Commit

Permalink
Cleanup some unit types (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheiny authored Nov 16, 2018
1 parent 3f6df97 commit eecac11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/measure/unitTypeArithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
ProductOf,
} from "../exponent";

export type UnitWithSymbols<U extends Unit = Unit> = { [D in keyof U]+?: [string, NonNullable<U[D]>] };
export type SymbolAndExponent = [string, Exponent];

export interface Unit {
[dimension: string]: Exponent | undefined;
}

export type UnitWithSymbols<U extends Unit = Unit> = { [D in keyof U]+?: [string, NonNullable<U[D]>] };
export type SymbolAndExponent = [string, Exponent];

// Multiplication

/** Returns the product of two units. This is the sum of two dimension vectors. */
Expand All @@ -38,9 +38,9 @@ export type DivisorUnit<U extends Unit> = MultiplicandUnit<ExponentiateUnit<U, -
// Exponentiation

/** Returns the unit raised to a power. This is the scalar multiple of the dimension vector. */
export type ExponentiateUnit<U extends Unit, N extends Exponent> = StripZeroes<
{ [Dim in keyof U]: MultiplyExponents<GetExponent<U, Dim>, N> }
>;
export type ExponentiateUnit<U extends Unit, N extends Exponent> = 0 extends N
? {}
: 1 extends N ? U : { [Dim in keyof U]: MultiplyExponents<GetExponent<U, Dim>, N> };

/** A type that is assignable from all units that can be raised to the N without producing an error. */
export interface BaseUnit<N extends Exponent> {
Expand All @@ -62,8 +62,9 @@ export interface RadicandUnit<N extends Exponent> {
// Utility types

/** Removes all zero exponent dimensions from a dimension vector */
type StripZeroes<U extends Unit> = Pick<U, NonZeroKeys<U>>;
type StripZeroes<U extends Unit> = { [Dim in NonZeroKeys<U>]: U[Dim] };

/** Gets the union of all dimensions of a unit with non zero or null exponents */
type NonZeroKeys<U extends Unit> = { [Dim in keyof U]: NonNullable<U[Dim]> extends 0 ? never : Dim }[keyof U];

/** Get the exponent at a given dimension of a unit, or 0 if that dimension is undefined */
Expand Down
6 changes: 3 additions & 3 deletions src/quantity/quantities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export const Frequency = Time.inverse();
export type Frequency = typeof Frequency;

/** 1 / s^2 */
export const FrequencyDrift = Time.inverse().squared();
export const FrequencyDrift = Time.toThe(-2);
export type FrequencyDrift = typeof FrequencyDrift;

/** 1 / m^2 */
export const FuelEfficiency = Length.inverse().squared();
export const FuelEfficiency = Length.toThe(-2);
export type FuelEfficiency = typeof FuelEfficiency;

/** 1 / m */
Expand Down Expand Up @@ -228,7 +228,7 @@ export const MagneticFlux = Energy.over(ElectricCurrent);
export type MagneticFlux = typeof MagneticFlux;

/** kg / (s^2 ⋅ A) */
export const MagneticFluxDensity = Voltage.times(Time).over(Length.squared());
export const MagneticFluxDensity = Voltage.times(Time).over(Area);
export type MagneticFluxDensity = typeof MagneticFluxDensity;

/** kg ⋅ m / (s^2 ⋅ A^2) */
Expand Down

0 comments on commit eecac11

Please sign in to comment.