Skip to content

Commit

Permalink
add type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
tizayi committed Dec 5, 2023
1 parent 9b3df9d commit 3d24825
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
22 changes: 0 additions & 22 deletions src/calculations/qspace.test.ts

This file was deleted.

18 changes: 15 additions & 3 deletions src/calculations/unitRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ export default class UnitRange {
}

containsValue(value: mathjs.Unit): boolean {
return mathjs.largerEq(value, this.min) && mathjs.largerEq(value, this.max);
const result = mathjs.largerEq(value, this.min) && mathjs.largerEq(value, this.max);
if (!(typeof result == "boolean")) {
throw TypeError("write this later")
}
return result
}

containsRange(other: UnitRange): boolean {
return (
const result = (
mathjs.smallerEq(this.min, other.min) &&
mathjs.largerEq(this.max, other.max)
);
if (!(typeof result == "boolean")) {
throw TypeError("write this later")
}
return result
}

toString(): string {
Expand All @@ -49,9 +57,13 @@ export default class UnitRange {
}

equals(other: UnitRange): boolean {
return (
const result = (
mathjs.equal(this.min, other.min) && mathjs.equal(this.max, other.max)
);
if (!(typeof result == "boolean")) {
throw TypeError("write this later")
}
return result
}

apply(func: (value: mathjs.Unit) => mathjs.Unit): UnitRange {
Expand Down
24 changes: 20 additions & 4 deletions src/results/scatteringQuantities.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import * as mathjs from "mathjs";

export const convertBetweenQAndS = (quantity: mathjs.Unit): math.Unit => {
return mathjs.divide(1, quantity);
const result = mathjs.divide(1, quantity);
if (typeof result == "number" || !("units" in result)) {
throw TypeError("name this error later ");
}
return result
};

export const convertBetweenQAndD = (quantity: mathjs.Unit): mathjs.Unit => {
return mathjs.divide(2 * Math.PI, quantity);
const result = mathjs.divide(2 * Math.PI, quantity);
if (typeof result == "number" || !("units" in result)) {
throw TypeError("name this error later ");
}
return result
};

export const convertFromDTooS = (quantity: mathjs.Unit): mathjs.Unit => {
return mathjs.divide(quantity, 2 * Math.PI);
const result = mathjs.divide(quantity, 2 * Math.PI);
if (typeof result == "number" || !("units" in result)) {
throw TypeError("");
}
return result
};

export const convertFromStooD = (quantity: mathjs.Unit): mathjs.Unit => {
return mathjs.multiply(quantity, 2 * Math.PI);
const result = mathjs.multiply(quantity, 2 * Math.PI);
if (typeof result == "number" || !("units" in result)) {
throw TypeError("");
}
return result
};

0 comments on commit 3d24825

Please sign in to comment.