Skip to content

Commit

Permalink
Remove circular imports (#28)
Browse files Browse the repository at this point in the history
* Add tslint rules

* Move unit typedef

* Explicit imports

* More stuff

* Fix coverage

* Unit tests
  • Loading branch information
jscheiny authored May 30, 2018
1 parent e6065f2 commit 820925b
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 56 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.12.0",
"tslint-config-standard": "^7.0.0",
"tslint-no-circular-imports": "^0.4.0",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "~2.8.3"
}
Expand Down
7 changes: 0 additions & 7 deletions src/exponent/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/measure/__test__/arithmeticSpec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsArithmeticError } from "../../exponent";
import { IsArithmeticError } from "../../exponent/utils";
import {
DivideUnits,
ExponentiateUnit,
Expand Down
2 changes: 1 addition & 1 deletion src/measure/__test__/mathTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { meters, seconds } from "../../unit";
import { meters, seconds } from "../../unit/base";
import * as SafeMath from "../math";
import { Measure } from "../measure";

Expand Down
3 changes: 2 additions & 1 deletion src/measure/__test__/unitTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dimension, divideUnits, exponentiateUnit, multiplyUnits, nthRootUnit, Unit } from "../units";
import { Unit } from "../types";
import { dimension, divideUnits, exponentiateUnit, multiplyUnits, nthRootUnit } from "../units";

describe("Units", () => {
const x = dimension("x");
Expand Down
4 changes: 2 additions & 2 deletions src/measure/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Exponent } from "../exponent";
import { Unit } from "./units";
import { Exponent } from "../exponent/common";
import { Unit } from "./types";

// TODO Remove cache and do this statelessly
const DimensionSymbolCache: { [dimension: string]: string } = {};
Expand Down
2 changes: 1 addition & 1 deletion src/measure/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as SafeMath from "./math";
import { Unit } from "./units";
import { Unit } from "./types";

export { SafeMath, Unit };
export * from "./measure";
6 changes: 3 additions & 3 deletions src/measure/math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Exponent } from "../exponent";
import { Exponent } from "../exponent/common";
import { Measure } from "./measure";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit } from "./types";
import { nthRootUnit, Unit } from "./units";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit, Unit } from "./types";
import { nthRootUnit } from "./units";

export const abs = wrapUnary(Math.abs);
export const ceil = wrapUnary(Math.ceil);
Expand Down
11 changes: 5 additions & 6 deletions src/measure/measure.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Exponent } from "../exponent";
import { Dimensionless } from "../quantity";
import { Exponent } from "../exponent/common";
import { formatUnit, setDimensionSymbol } from "./format";
import { DivideUnits, ExponentiateUnit, MultiplyUnits } from "./types";
import { dimension, divideUnits, exponentiateUnit, multiplyUnits, Unit } from "./units";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, Unit } from "./types";
import { dimension, divideUnits, exponentiateUnit, multiplyUnits } from "./units";

export class Measure<U extends Unit> {
// Construction functions
Expand All @@ -11,7 +10,7 @@ export class Measure<U extends Unit> {
if (symbol) {
setDimensionSymbol(dim, symbol);
}
return new Measure(1, dimension(dim));
return new Measure(1, dimension(dim), symbol);
}

public static dimensionless(value: number): Measure<{}> {
Expand Down Expand Up @@ -64,7 +63,7 @@ export class Measure<U extends Unit> {
return new Measure(-this.value, this.unit);
}

public scale(value: number | Dimensionless): Measure<U> {
public scale(value: number | Measure<{}>): Measure<U> {
const numericValue = typeof value === "number" ? value : value.value;
return new Measure(numericValue * this.value, this.unit);
}
Expand Down
17 changes: 7 additions & 10 deletions src/measure/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
AddExponents,
ArithmeticError,
DivideExponents,
Exponent,
IsArithmeticError,
MultiplesOf,
MultiplyExponents,
} from "../exponent";
import { Unit } from "./units";
import { AddExponents } from "../exponent/addition";
import { ArithmeticError, Exponent } from "../exponent/common";
import { DivideExponents } from "../exponent/division";
import { MultiplyExponents } from "../exponent/multiplication";
import { IsArithmeticError, MultiplesOf } from "../exponent/utils";

export type Unit = Partial<{ [dimension: string]: Exponent }>;

// Arithmetic

Expand Down
6 changes: 2 additions & 4 deletions src/measure/units.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ArithmeticError, Exponent, MaxExponent, MinExponent } from "../exponent";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit } from "./types";

export type Unit = Partial<{ [dimension: string]: Exponent }>;
import { ArithmeticError, Exponent, MaxExponent, MinExponent } from "../exponent/common";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit, Unit } from "./types";

export function dimension<D extends string>(dim: D): { [K in D]: 1 } {
// TODO Remove cast to any somehow
Expand Down
8 changes: 4 additions & 4 deletions src/quantity/__test__/quantityTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Measure } from "../../measure";
import { Measure } from "../../measure/measure";
import * as Quantity from "../quantities";

describe("Quantity", () => {
describe("Quantities", () => {
const QuantityNames = Object.keys(Quantity);

function forEachQuantity(fn: (quantity: Measure<any>, name: string) => void) {
Expand All @@ -10,7 +10,7 @@ describe("Quantity", () => {
});
}

it("No two quantities should be the same", () => {
it("no two quantities should have the same dimensions", () => {
forEachQuantity((a, aName) => {
forEachQuantity((b, bName) => {
if (aName === bName) {
Expand All @@ -26,7 +26,7 @@ describe("Quantity", () => {
});
});

it("All quantities should be normalized", () => {
it("all quantities should be normalized", () => {
forEachQuantity(quantity => {
expect(quantity.value).toBe(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/quantity/quantities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Measure } from "../measure";
import { Measure } from "../measure/measure";
import * as Base from "../unit/base";

// Dimensionless
Expand Down
54 changes: 54 additions & 0 deletions src/unit/__test__/unitTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Measure } from "../../measure/measure";
import * as Units from "../../unit";
import { meters } from "../base";
import { kilo, micro } from "../metric";

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

function forEachUnit(fn: (unit: Measure<any>, name: string) => void) {
UnitNames.forEach(name => {
const value = (Units as any)[name];
if (value instanceof Measure) {
fn(value, name);
} else {
for (const subName in value) {
if (value.hasOwnProperty(subName)) {
const unit = value[subName];
fn(unit, subName);
}
}
}
});
}

it("all units should have a symbol", () => {
forEachUnit((unit, name) => {
try {
expect(unit.getSymbol()).not.toBeUndefined();
} catch (e) {
console.log(`Unit ${name} has no symbol defined.`);
throw e;
}
});
});

describe("prefixes", () => {
it("should scale the base unit", () => {
const km = kilo(meters);
expect(km.getUnit()).toEqual(meters.getUnit());
expect(km.value).toBe(1000);
});

it("should apply a prefix when a symbol is present on the base unit", () => {
expect(kilo(meters).getSymbol()).toBe("km");
});

it("should not apply a prefix when a symbo is not present on the base unit", () => {
const blargs = Measure.of(1_000_000, meters);
const microblargs = micro(blargs);
expect(microblargs.getSymbol()).toBeUndefined();
expect(microblargs.value).toBe(1);
});
});
});
2 changes: 1 addition & 1 deletion src/unit/angle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Measure } from "../measure";
import { Measure } from "../measure/measure";
import { radians } from "./base";

export const pi = Measure.of(Math.PI, radians, "pi");
Expand Down
2 changes: 1 addition & 1 deletion src/unit/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Measure } from "../measure";
import { Measure } from "../measure/measure";

export const meters = Measure.dimension("length", "m");
export const kilograms = Measure.dimension("mass", "kg");
Expand Down
4 changes: 2 additions & 2 deletions src/unit/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Measure } from "../measure";
import { Quantity } from "../quantity";
import { Measure } from "../measure/measure";
import * as Quantity from "../quantity/quantities";
import { grams, meters, seconds } from "./base";

// Time
Expand Down
4 changes: 2 additions & 2 deletions src/unit/imperial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cubic, Measure } from "../measure";
import { Quantity } from "../quantity";
import { cubic, Measure } from "../measure/measure";
import * as Quantity from "../quantity/quantities";
import { grains, inches, pounds } from "./common";

// Liquid Volume
Expand Down
9 changes: 5 additions & 4 deletions src/unit/metric.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Measure, Unit } from "../measure";
import { Quantity } from "../quantity";
import { Measure } from "../measure/measure";
import { Unit } from "../measure/types";
import * as Quantity from "../quantity/quantities";
import { amperes, candela, kilograms, meters, moles, seconds, steradians } from "./base";

export const hertz: Quantity.Frequency = seconds.inverse().withSymbol("Hz");
Expand All @@ -17,8 +18,8 @@ export const webers: Quantity.MagneticFlux = joules.per(amperes).withSymbol("Wb"
export const teslas: Quantity.MagneticFluxDensity = volts.times(seconds.per(meters.squared())).withSymbol("T");
export const sieverts: Quantity.RadiationDose = joules.per(kilograms).withSymbol("Sv");
export const katals: Quantity.CatalyticActivity = moles.per(seconds).withSymbol("kat");
export const lumens: Quantity.LuminousFlux = candela.times(steradians);
export const luxes: Quantity.Illuminance = lumens.per(meters.squared());
export const lumens: Quantity.LuminousFlux = candela.times(steradians).withSymbol("lm");
export const luxes: Quantity.Illuminance = lumens.per(meters.squared()).withSymbol("lx");

// Prefixes

Expand Down
4 changes: 2 additions & 2 deletions src/unit/other.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cubic, Measure, square } from "../measure";
import { Quantity } from "../quantity";
import { cubic, Measure, square } from "../measure/measure";
import * as Quantity from "../quantity/quantities";
import { grams, meters, seconds } from "./base";
import { milli, nano, pascals } from "./metric";

Expand Down
4 changes: 2 additions & 2 deletions src/unit/uscu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cubic, Measure } from "../measure";
import { Quantity } from "../quantity";
import { cubic, Measure } from "../measure/measure";
import * as Quantity from "../quantity/quantities";
import { grams, meters } from "./base";
import { grains, inches, pounds } from "./common";
import { micro } from "./metric";
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": [
"tslint-config-standard",
"tslint-config-prettier",
"tslint-plugin-prettier"
"tslint-plugin-prettier",
"tslint-no-circular-imports"
],
"rules": {
"prettier": [
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3315,6 +3315,10 @@ tslint-eslint-rules@^4.1.1:
tslib "^1.0.0"
tsutils "^1.4.0"

tslint-no-circular-imports@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/tslint-no-circular-imports/-/tslint-no-circular-imports-0.4.0.tgz#da22d442376f0eb5b03738b0b91a3d8949f6b8f3"

tslint-plugin-prettier@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/tslint-plugin-prettier/-/tslint-plugin-prettier-1.3.0.tgz#7eb65d19ea786a859501a42491b78c5de2031a3f"
Expand Down

0 comments on commit 820925b

Please sign in to comment.