Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-scaling conversions #9

Open
wants to merge 1 commit into
base: mainline
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/interceptor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { dynamicToInterceptor, interceptor, interceptorToDynamic } from './interceptor';

describe('Unknown unit into the system', () =>
{
test('(Runtime) Celsius to (Compiletime) Fahrenheit', () =>
{
const inputValue = 1.3;
const inputUnit = 'C';
const outputFahrenheit = dynamicToInterceptor(inputValue, inputUnit, 'F');
expect(outputFahrenheit.value).toBe(34.34);
expect(outputFahrenheit.unit).toBe('F');
});

test('(Runtime) Celsius to (Compiletime) Fahrenheit as interceptor', () =>
{
const inputValue = interceptor(1.3, 'C' as string);
const outputFahrenheit = dynamicToInterceptor(inputValue, 'F');
expect(outputFahrenheit.value).toBe(34.34);
expect(outputFahrenheit.unit).toBe('F');
});

test('Invalid unit conversion', () =>
{
const inputValue = 1.3;
const inputUnit = 'C';
expect(() => dynamicToInterceptor(inputValue, inputUnit, 's'))
.toThrowError('Cannot convert incompatible measures of time and temperature');
});

test('Unknown runtime unit', () =>
{
const inputValue = 1.3;
const inputUnit = 'unknown_doesnt_exist';
expect(() => dynamicToInterceptor(inputValue, inputUnit, 's')).toThrowError();
});

test('Unknown compiletime unit', () =>
{
const inputValue = 1.3;
const inputUnit = 'C';
expect(() => dynamicToInterceptor(inputValue, inputUnit, 'unknown_doesnt_exist')).toThrowError();
});
});

describe('Unknown unit out of the system', () =>
{
test('(Compiletime) Celsius to (Runtime) Fahrenheit', () =>
{
const inputValue = interceptor(1.3, 'C');
const outputUnits = 'F';
const outputFahrenheit = interceptorToDynamic(inputValue, outputUnits);
expect(outputFahrenheit).toBe(34.34);
});

test('Invalid unit conversion', () =>
{
const inputValue = interceptor(1.3, 'C');
const outputUnits = 's';
expect(() => interceptorToDynamic(inputValue, outputUnits))
.toThrowError('Cannot convert incompatible measures of time and temperature');
});

test('Unknown runtime unit', () =>
{
const inputValue = interceptor(1.3, 'C');
const outputUnits = 'unknown_doesnt_exist';
expect(() => interceptorToDynamic(inputValue, outputUnits)).toThrowError();
});

test('Unknown compiletime unit', () =>
{
const inputValue = interceptor(1.3, 'unknown_doesnt_exist');
const outputUnits = 'F';
expect(() => interceptorToDynamic(inputValue, outputUnits)).toThrowError();
});
});
29 changes: 29 additions & 0 deletions src/interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import autoConverter from 'convert-units';
import { UnitedInterceptor } from './types/UnitedInterceptor';

export function interceptor<N extends string>(v: number, u: N): UnitedInterceptor<N>
{
return new UnitedInterceptor<N>(v, u);
}

export function dynamicToInterceptor<N extends string>(value: UnitedInterceptor<string>, outUnit: N): UnitedInterceptor<N>
export function dynamicToInterceptor<N extends string>(value: number, inUnit: string, outUnit: N): UnitedInterceptor<N>
export function dynamicToInterceptor<N extends string>(
value: number | UnitedInterceptor<string>,
inUnit: string | N,
outUnit?: N
): UnitedInterceptor<N>
{
// by providing types, providing just a string is no longer allowed- even though that's the primary use-case in Javascript
const numericValue = typeof value === 'number' ? value : value.value;
const inUnitName = typeof value === 'number' ? inUnit : value.unit;
const outUnitName = (typeof outUnit === 'string' ? outUnit : inUnit) as N;
const convertedValue = autoConverter(numericValue).from(inUnitName as any).to(outUnitName as any);
return new UnitedInterceptor<N>(convertedValue, outUnitName);
}

export function interceptorToDynamic<InT extends string>(value: UnitedInterceptor<InT>, outUnit: string): number
{
const convertedValue = autoConverter(value.value).from(value.unit as any).to(outUnit as any);
return convertedValue;
}
79 changes: 79 additions & 0 deletions src/types/UnitedInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { UnitConversion } from './UnitConversion';
import { UnitTypeError } from './UnitTypeError';
import { interceptor } from '../interceptor';
import { ratio } from '../ratio';

/**
* NOT FOR GENERAL USE -- use the `interceptor` factory method, typically.
* A `Unit`-ed interceptor value is a simple number with a specific unit attached to it.
*
* Examples: 35.6 F or 2 C.
*/
export class UnitedInterceptor<N extends string>
{
public readonly unit: N;
public readonly value: number;

/**
* The default constructor takes the value and the unit.
*/
public constructor(v: number, u: N)
{
this.value = v;
this.unit = u;
}

/**
* Return a new interceptor of the same unit with a modified value.
*/
public modify(func: (x: number) => number)
{
return new UnitedInterceptor<N>(func(this.value), this.unit);
}

/**
* Return a new interceptor of the same unit with a value that is the sum of this and the parameter.
*/
public add<T>(other: T extends N ? UnitedInterceptor<T> : UnitTypeError<'Unit mismatch', N, T>)
{
return new UnitedInterceptor<N>(this.value + other.value, this.unit);
}

/**
* Return a new interceptor of the same unit with a value that is the parameter subtracted from this.
*/
public subtract<T>(other: T extends N ? UnitedInterceptor<T> : UnitTypeError<'Unit mismatch', N, T>)
{
return new UnitedInterceptor<N>(this.value - other.value, this.unit);
}

/**
* Return a ratio that is this over the provided parameter.
*/
public divideBy<T extends string>(other: UnitedInterceptor<T>)
{
return ratio(this.value, this.unit, other.value, other.unit);
}

/**
* Return a new interceptor of the same unit with a value that has been scaled by the provided parameter.
*/
public multiply(unitlessValue: number)
{
return interceptor(this.value * unitlessValue, this.unit);
}

/**
* Convert this interceptor from one unit to another.
*/
public convert<TN extends string, TD extends string>(
converter: TN extends N ? UnitConversion<TN, TD> : UnitTypeError<'Conversion unit mismatch', N, TN>
)
{
if (!UnitConversion.isConversion<TN, TD>(converter))
{
throw new Error('Invalid conversion object');
}
return new UnitedInterceptor<TD>((this.value / converter.source.value) * converter.dest.value, converter.dest.unit);
}
}