From 3d2482573a39b38fea031ad18ecb5b8d2a50aabe Mon Sep 17 00:00:00 2001 From: tizayi Date: Tue, 5 Dec 2023 16:14:49 +0000 Subject: [PATCH] add type guards --- src/calculations/qspace.test.ts | 22 ---------------------- src/calculations/unitRange.ts | 18 +++++++++++++++--- src/results/scatteringQuantities.ts | 24 ++++++++++++++++++++---- 3 files changed, 35 insertions(+), 29 deletions(-) delete mode 100644 src/calculations/qspace.test.ts diff --git a/src/calculations/qspace.test.ts b/src/calculations/qspace.test.ts deleted file mode 100644 index 363da74..0000000 --- a/src/calculations/qspace.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { expect, test } from "vitest"; -import { Vector2, Vector3 } from "three"; -import QSpace, { DetectorProperties } from "./qspace"; - -test("Test getting q from pixel position ", () => { - const detProps: DetectorProperties = { - resolution: { - height: 1, - width: 1, - }, - pixelSize: { - height: 1, - width: 1, - }, - origin: new Vector3(0, 0, 0), - beamVector: new Vector3(1, 1, 0), - }; - - const qspace = new QSpace(detProps, 1, 1); - const result = qspace.qFromPixelPosition(new Vector2(1, 1)); - expect(result).toBeInstanceOf(Vector3); -}); diff --git a/src/calculations/unitRange.ts b/src/calculations/unitRange.ts index 00263f8..15b9537 100644 --- a/src/calculations/unitRange.ts +++ b/src/calculations/unitRange.ts @@ -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 { @@ -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 { diff --git a/src/results/scatteringQuantities.ts b/src/results/scatteringQuantities.ts index 6d4c5c4..d95fd5a 100644 --- a/src/results/scatteringQuantities.ts +++ b/src/results/scatteringQuantities.ts @@ -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 };