Skip to content

Commit

Permalink
Enforce typedefs for functions and parameters (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheiny authored Aug 9, 2018
1 parent 9ffaf31 commit 7c968e2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
26 changes: 10 additions & 16 deletions src/measure/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,22 @@ export function sum<U extends Unit>(first: Measure<U>, ...rest: Array<Measure<U>
return result;
}

function wrapUnary(f: (x: number) => number) {
return <U extends Unit>(x: Measure<U>): Measure<U> => {
return Measure.of(f(x.value), x.normalized());
};
function wrapUnary(f: (x: number) => number): <U extends Unit>(x: Measure<U>) => Measure<U> {
return x => Measure.of(f(x.value), x.normalized());
}

function wrapTrig(f: (x: number) => number) {
return (angle: PlaneAngle): Dimensionless => {
return Measure.dimensionless(f(angle.value));
};
function wrapTrig(f: (x: number) => number): (angle: PlaneAngle) => Dimensionless {
return angle => Measure.dimensionless(f(angle.value));
}

function wrapInverseTrig(f: (x: number) => number) {
return (angle: Dimensionless): PlaneAngle => {
return Measure.of(f(angle.value), radians);
};
function wrapInverseTrig(f: (x: number) => number): (angle: Dimensionless) => PlaneAngle {
return angle => Measure.of(f(angle.value), radians);
}

function warpNary(f: (...x: number[]) => number) {
return <U extends Unit>(first: Measure<U>, ...rest: Array<Measure<U>>): Measure<U> => {
return Measure.of(f(...values(first, ...rest)), first.normalized());
};
function warpNary(
f: (...x: number[]) => number,
): <U extends Unit>(first: Measure<U>, ...rest: Array<Measure<U>>) => Measure<U> {
return (first, ...rest) => Measure.of(f(...values(first, ...rest)), first.normalized());
}

function values<U extends Unit>(...measures: Array<Measure<U>>): number[] {
Expand Down
2 changes: 1 addition & 1 deletion src/measure/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dimension, divideUnits, exponentiateUnit, multiplyUnits } from "./units
export class Measure<U extends Unit> {
// Construction functions

public static dimension<Dimension extends string>(dim: Dimension, symbol?: string) {
public static dimension<Dim extends string>(dim: Dim, symbol?: string): Measure<{ [D in Dim]: 1 }> {
return new Measure(1, dimension(dim, symbol), symbol);
}

Expand Down
2 changes: 1 addition & 1 deletion src/quantity/__test__/quantityTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Quantity from "../quantities";
describe("Quantities", () => {
const QuantityNames = Object.keys(Quantity);

function forEachQuantity(fn: (quantity: Measure<any>, name: string) => void) {
function forEachQuantity(fn: (quantity: Measure<any>, name: string) => void): void {
QuantityNames.forEach(name => {
fn((Quantity as any)[name], name);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as Units from "../../unit";
import { meters } from "../base";
import { kilo, micro } from "../metric";

describe("Units", () => {
describe("Predefined Units", () => {
const UnitNames = Object.keys(Units);

function forEachUnit(fn: (unit: Measure<any>, name: string) => void) {
function forEachUnit(fn: (unit: Measure<any>, name: string) => void): void {
UnitNames.forEach(name => {
const value = (Units as any)[name];
if (value instanceof Measure) {
Expand Down
4 changes: 2 additions & 2 deletions src/unit/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const luxes: Quantity.Illuminance = lumens.per(meters.squared()).withSymb

// Prefixes

function createPrefix(symbolPrefix: string, multiplier: number) {
return <U extends Unit>(unit: Measure<U>): Measure<U> => {
function createPrefix(symbolPrefix: string, multiplier: number): <U extends Unit>(unit: Measure<U>) => Measure<U> {
return unit => {
const symbol = unit.getSymbol();
const newSymbol = symbol !== undefined ? `${symbolPrefix}${symbol}` : undefined;
return Measure.of(multiplier, unit, newSymbol);
Expand Down
7 changes: 6 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"order": "statics-first"
}
],
"ordered-imports": true
"ordered-imports": true,
"typedef": [
true,
"call-signature",
"parameter"
]
}
}

0 comments on commit 7c968e2

Please sign in to comment.