Skip to content

Commit

Permalink
fix: type issues found from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jul 8, 2023
1 parent 26f3df5 commit 6f15ac9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/base/exponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type NegativeExponent<T extends Exponent> = T extends -6
: T extends -2
? 2
: T extends -1
? 0
: T extends 0
? 1
: T extends 0
? 0
: T extends 1
? -1
: T extends 2
Expand Down
1 change: 1 addition & 0 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type {
MultiplyExponents,
DivideExponents,
} from "./exponents";
export { MultiplyUnitExponents, DivideUnitExponents } from "./unit-exponents";
export type { Multiply, Divide, Inverse } from "./units";
55 changes: 55 additions & 0 deletions src/base/unit-exponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { type Unit, type UnitValue, type UnknownUnit } from "./core";
import {
type DivideExponents,
type Exponent,
type MultiplyExponents,
} from "./exponents";
import {
type FlatternAlias,
type GetUnitConfig,
type GetUnitValues,
} from "./utils";

export type MultiplyUnitExponents<
A extends number,
B extends 2,
> = A extends UnknownUnit
? MultiplyUnitExponentsCore<A, B> extends Record<string, UnitValue>
? Unit<FlatternAlias<MultiplyUnitExponentsCore<A, B>>>
: never
: number;

type MultiplyUnitExponentsCore<A extends UnknownUnit, B extends 2> = {
[U in keyof GetUnitConfig<A>]: {
[E in keyof GetUnitConfig<A>[U]]: MultiplyExponents<
E extends keyof GetUnitValues<GetUnitConfig<A>, U>
? GetUnitValues<GetUnitConfig<A>, U>[E] extends Exponent
? GetUnitValues<GetUnitConfig<A>, U>[E]
: 0
: 0,
B
>;
};
};

export type DivideUnitExponents<
A extends number,
B extends 2,
> = A extends UnknownUnit
? DivideUnitExponentsCore<A, B> extends Record<string, UnitValue>
? Unit<FlatternAlias<DivideUnitExponentsCore<A, B>>>
: never
: number;

type DivideUnitExponentsCore<A extends UnknownUnit, B extends 2> = {
[U in keyof GetUnitConfig<A>]: {
[E in keyof GetUnitConfig<A>[U]]: DivideExponents<
E extends keyof GetUnitValues<GetUnitConfig<A>, U>
? GetUnitValues<GetUnitConfig<A>, U>[E] extends Exponent
? GetUnitValues<GetUnitConfig<A>, U>[E]
: 0
: 0,
B
>;
};
};
16 changes: 9 additions & 7 deletions src/functions-ho/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type DivideUnitExponents,
type Divide,
type Multiply,
type Inverse,
Expand Down Expand Up @@ -64,12 +65,14 @@ export function modSafe<T extends number>(
return (b) => (((b % a) + a) % a) as OperationIO<T>;
}

type PowFunction<E extends number, B extends number> = E extends -1
type PowFunction<E extends number, B extends number> = E extends UnknownUnit
? never
: E extends -1
? (b: OperationIO<B>) => OperationIO<Inverse<B>>
: E extends 0
? (b: OperationIO<B>) => Decimal | 1
? (b: OperationIO<B>) => OperationIO<B> extends UnknownUnit ? Decimal : 1
: E extends 0.5
? (b: OperationIO<B>) => OperationIO<Divide<B, 2>>
? (b: OperationIO<B>) => OperationIO<DivideUnitExponents<B, 2>>
: E extends 1
? (b: OperationIO<B>) => OperationIO<B>
: E extends 2
Expand All @@ -83,10 +86,9 @@ type PowFunction<E extends number, B extends number> = E extends -1
/**
* Put a number to the power of the given value.
*/
export function pow<E extends number, B extends number>(
exponent: E extends UnknownUnit ? never : E,
): PowFunction<E, B> {
return ((base: OperationIO<B>) => base ** exponent) as PowFunction<E, B>;
export function pow<E extends number>(exponent: E) {
return <B extends number>(base: Parameters<PowFunction<E, B>>[0]) =>
(base ** exponent) as ReturnType<PowFunction<E, B>>;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type DivideUnitExponents,
type Divide,
type Multiply,
type Inverse,
Expand Down Expand Up @@ -98,7 +99,7 @@ export function pow<B extends number>(
export function pow<B extends number>(
base: OperationIO<B>,
exponent: 0.5,
): OperationIO<Divide<B, 2>>;
): OperationIO<DivideUnitExponents<B, 2>>;

/**
* Put a number to the power of 1.
Expand Down Expand Up @@ -146,7 +147,7 @@ export function pow(base: number, exponent: number): number {
*/
export function sqrt<T extends number>(
value: OperationIO<T>,
): OperationIO<Divide<T, 2>> {
): OperationIO<DivideUnitExponents<T, 2>> {
return pow(value, 0.5);
}

Expand Down Expand Up @@ -219,15 +220,6 @@ export function sum<T extends number>(
return values.reduce(add<number>, 0) as OperationIO<T>;
}

/**
* Takes the product of all the values in the given collection.
*/
export function product<T extends number>(
values: ReadonlyArray<T>,
): OperationIO<T> {
return values.reduce(mul<number, number>, 1) as OperationIO<T>;
}

/**
* Equal: Compare if two values are equal.
*/
Expand Down

0 comments on commit 6f15ac9

Please sign in to comment.