Skip to content

Commit

Permalink
style: update linting and move some files again
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Oct 14, 2024
1 parent 80965de commit d04623a
Show file tree
Hide file tree
Showing 216 changed files with 1,363 additions and 2,942 deletions.
162 changes: 65 additions & 97 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { assert, type Equals, type Extends } from "tsafe";
import { type Equals, type Extends, assert } from "tsafe";

import { type Exponent } from "./exponents";
import { type RemoveNeverValues, type ExcludeUnitZeroSubvalues } from "./utils";
import type { Exponent } from "./exponents";
import type { ExcludeUnitZeroSubvalues, RemoveNeverValues } from "./utils";

/**
* A unit without any meta data.
*
* @group Abstract Unit Generators
*/
export type AbstractUnit<C extends UnitSubvalues> = number & {
readonly __uom_types__unit_class: UnitClass<C>;
};
export type AbstractUnit<C extends UnitSubvalues> = number &
Readonly<{
__uom_types__unit_class: UnitClass<C>;
}>;

/**
* Create a unit without meta data from an already existing {@link UnitClass}.
*
* @group Abstract Unit Generators
*/
export type AbstractUnitFrom<C extends UnknownUnitClass> = C extends UnitClass<
infer A
>
? AbstractUnit<UnitSubvalues extends A ? {} : A>
: never;
export type AbstractUnitFrom<C extends UnknownUnitClass> =
C extends UnitClass<infer A> ? AbstractUnit<UnitSubvalues extends A ? {} : A> : never;

/**
* The core unit type.
*
* @group Unit Generators
*/
export type Unit<
C extends UnitSubvalues,
M extends UnitSubvalues = {},
> = number & {
readonly __uom_types__unit_class: UnitClass<C>;
readonly __uom_types__unit_meta: UnitMeta<M>;
};
export type Unit<C extends UnitSubvalues, M extends UnitSubvalues = {}> = number &
Readonly<{
__uom_types__unit_class: UnitClass<C>;
__uom_types__unit_meta: UnitMeta<M>;
}>;

/**
* Create a {@link Unit} from already existing {@link UnitClass} and {@link UnitMeta}.
*
* @group Unit Generators
*/
export type UnitFrom<
C extends UnknownUnitClass,
M extends UnknownUnitMeta = UnitMeta<{}>,
> = C extends UnitClass<infer A>
? M extends UnitMeta<infer B>
? Unit<A, B>
: never
: never;
export type UnitFrom<C extends UnknownUnitClass, M extends UnknownUnitMeta = UnitMeta<{}>> =
C extends UnitClass<infer A> ? (M extends UnitMeta<infer B> ? Unit<A, B> : never) : never;

/**
* A {@link Unit} without any {@link UnitClass}.
Expand All @@ -58,9 +48,10 @@ export type UnitFrom<
*
* @group Unit Generators
*/
export type UnitConversionRate<M extends UnitSubvalues> = number & {
readonly __uom_types__unit_meta: UnitMeta<M>;
};
export type UnitConversionRate<M extends UnitSubvalues> = number &
Readonly<{
__uom_types__unit_meta: UnitMeta<M>;
}>;

/**
* Create a {@link UnitConversionRate} from already existing {@link UnitMeta}.
Expand All @@ -75,76 +66,81 @@ export type UnitConversionRateFrom<M extends UnknownUnitMeta> =
*
* @group Unit Components
*/
export type UnitClass<T extends UnitSubvalues> = UnitKeyValues<T> & {
readonly __uno_types__unit_class_type: true;
};
export type UnitClass<T extends UnitSubvalues> = UnitKeyValues<T> &
Readonly<{
__uno_types__unit_class_type: true;
}>;

/**
* Used to state how units of the same {@link UnitClass} differ from one another.
* For example, by a scale factor.
*
* @group Unit Components
*/
export type UnitMeta<T extends UnitSubvalues> = UnitKeyValues<T> & {
readonly __uno_types__unit_meta_type: true;
};
export type UnitMeta<T extends UnitSubvalues> = UnitKeyValues<T> &
Readonly<{
__uno_types__unit_meta_type: true;
}>;

/**
* A {@link Unit} that we don't know anything about.
*
* @group Unknown Units
*/
export type UnknownUnit = UnknownAbstractUnit & {
readonly __uom_types__unit_meta: UnknownUnitMeta;
};
export type UnknownUnit = UnknownAbstractUnit &
Readonly<{
__uom_types__unit_meta: UnknownUnitMeta;
}>;

/**
* An {@link AbstractUnit} that we don't know anything about.
*
* @group Unknown Units
*/
export type UnknownAbstractUnit = number & {
readonly __uom_types__unit_class: UnknownUnitClass;
};
export type UnknownAbstractUnit = number &
Readonly<{
__uom_types__unit_class: UnknownUnitClass;
}>;

/**
* An {@link UnitConversionRate} that we don't know anything about.
*
* @group Unknown Units
*/
export type UnknownUnitConversionRate = number & {
readonly __uom_types__unit_meta: UnknownUnitMeta;
};
export type UnknownUnitConversionRate = number &
Readonly<{
__uom_types__unit_meta: UnknownUnitMeta;
}>;

/**
* A {@link UnitClass} that we don't know anything about.
*
* @group Unknown Units
*/
export type UnknownUnitClass = UnknownUnitKeyValues & {
readonly __uno_types__unit_class_type: true;
};
export type UnknownUnitClass = UnknownUnitKeyValues &
Readonly<{
__uno_types__unit_class_type: true;
}>;

/**
* A {@link UnitMeta} that we don't know anything about.
*
* @group Unknown Units
*/
export type UnknownUnitMeta = UnknownUnitKeyValues & {
readonly __uno_types__unit_meta_type: true;
};
export type UnknownUnitMeta = UnknownUnitKeyValues &
Readonly<{
__uno_types__unit_meta_type: true;
}>;

type UnitKeyValues<T extends UnitSubvalues> = {
readonly __uom_types__keys: keyof ExcludeUnitZeroSubvalues<T> extends string
? keyof ExcludeUnitZeroSubvalues<T>
: never;
readonly __uom_types__value: RemoveNeverValues<T>;
};
type UnitKeyValues<T extends UnitSubvalues> = Readonly<{
__uom_types__keys: keyof ExcludeUnitZeroSubvalues<T> extends string ? keyof ExcludeUnitZeroSubvalues<T> : never;
__uom_types__value: RemoveNeverValues<T>;
}>;

type UnknownUnitKeyValues = {
readonly __uom_types__keys: string;
readonly __uom_types__value: {};
};
type UnknownUnitKeyValues = Readonly<{
__uom_types__keys: string;
__uom_types__value: {};
}>;

/**
* A mapping of subvalue of a unit its magnitude.
Expand All @@ -170,45 +166,21 @@ if (import.meta.vitest !== undefined) {
assert<Extends<Unit<{}>, AbstractUnit<{}>>>();
assert<Extends<Unit<{ a: 1 }>, AbstractUnit<{ a: 1 }>>>();
assert<Extends<Unit<UnitSubvalues>, AbstractUnit<UnitSubvalues>>>();
assert<
Extends<UnitFrom<UnitClass<{}>>, AbstractUnitFrom<UnitClass<{}>>>
>();
assert<
Extends<
UnitFrom<UnitClass<{ a: 1 }>>,
AbstractUnitFrom<UnitClass<{ a: 1 }>>
>
>();
assert<
Extends<
UnitFrom<UnitClass<UnitSubvalues>>,
AbstractUnitFrom<UnitClass<UnitSubvalues>>
>
>();
assert<Extends<UnitFrom<UnitClass<{}>>, AbstractUnitFrom<UnitClass<{}>>>>();
assert<Extends<UnitFrom<UnitClass<{ a: 1 }>>, AbstractUnitFrom<UnitClass<{ a: 1 }>>>>();
assert<Extends<UnitFrom<UnitClass<UnitSubvalues>>, AbstractUnitFrom<UnitClass<UnitSubvalues>>>>();
});
});

describe("UnitConversionRate", () => {
it("a super type of Unit", () => {
assert<Extends<Unit<{}>, UnitConversionRate<{}>>>();
assert<Extends<Unit<{}, { a: 1 }>, UnitConversionRate<{ a: 1 }>>>();
assert<Extends<Unit<{}, UnitSubvalues>, UnitConversionRate<UnitSubvalues>>>();
assert<Extends<UnitFrom<UnitClass<{}>>, UnitConversionRateFrom<UnitMeta<{}>>>>();
assert<Extends<UnitFrom<UnitClass<{}>, UnitMeta<{ a: 1 }>>, UnitConversionRateFrom<UnitMeta<{ a: 1 }>>>>();
assert<
Extends<Unit<{}, UnitSubvalues>, UnitConversionRate<UnitSubvalues>>
>();
assert<
Extends<UnitFrom<UnitClass<{}>>, UnitConversionRateFrom<UnitMeta<{}>>>
>();
assert<
Extends<
UnitFrom<UnitClass<{}>, UnitMeta<{ a: 1 }>>,
UnitConversionRateFrom<UnitMeta<{ a: 1 }>>
>
>();
assert<
Extends<
UnitFrom<UnitClass<{}>, UnitMeta<UnitSubvalues>>,
UnitConversionRateFrom<UnitMeta<UnitSubvalues>>
>
Extends<UnitFrom<UnitClass<{}>, UnitMeta<UnitSubvalues>>, UnitConversionRateFrom<UnitMeta<UnitSubvalues>>>
>();
});
});
Expand All @@ -235,12 +207,8 @@ if (import.meta.vitest !== undefined) {
describe("UnitConversionRate", () => {
it(assignableToConcreteTypes, () => {
assert<Extends<UnitConversionRate<{}>, UnknownUnitConversionRate>>();
assert<
Extends<UnitConversionRate<{ a: 1 }>, UnknownUnitConversionRate>
>();
assert<
Extends<UnitConversionRate<UnitSubvalues>, UnknownUnitConversionRate>
>();
assert<Extends<UnitConversionRate<{ a: 1 }>, UnknownUnitConversionRate>>();
assert<Extends<UnitConversionRate<UnitSubvalues>, UnknownUnitConversionRate>>();
});
});

Expand Down
14 changes: 4 additions & 10 deletions src/deprecated.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { type Exponent } from "./exponents";
import { type Pow, type Root } from "./units-operations";
import type { Exponent } from "./exponents";
import type { Pow, Root } from "./units-operations";

/**
* Multiple the exponent values of a unit by a number.
*
* @deprecated Use {@link Pow} instead.
*/
export type MultiplyUnitExponents<T extends number, E extends Exponent> = Pow<
T,
E
>;
export type MultiplyUnitExponents<T extends number, E extends Exponent> = Pow<T, E>;

/**
* Divide the exponent values of a unit by a number.
*
* @deprecated Use {@link Root} instead.
*/
export type DivideUnitExponents<T extends number, E extends Exponent> = Root<
T,
E
>;
export type DivideUnitExponents<T extends number, E extends Exponent> = Root<T, E>;
Loading

0 comments on commit d04623a

Please sign in to comment.