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

Explore elm types #267

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
a few more types
  • Loading branch information
csenn committed Mar 14, 2022
commit d14d548d7220110d19afa18619545ecc58cdd207
35 changes: 21 additions & 14 deletions src/datatypes/quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export class Quantity {
return true;
}

clone() {
clone(): Quantity {
return new Quantity(this.value, this.unit);
}

toString() {
toString(): string {
return `${this.value} '${this.unit}'`;
}

sameOrBefore(other: any) {
sameOrBefore(other: any): null | boolean {
if (other != null && other.isQuantity) {
const otherVal = convertUnit(other.value, other.unit, this.unit);
if (otherVal == null) {
Expand All @@ -46,9 +46,10 @@ export class Quantity {
return this.value <= otherVal;
}
}
return null
}

sameOrAfter(other: any) {
sameOrAfter(other: any): null | boolean {
if (other != null && other.isQuantity) {
const otherVal = convertUnit(other.value, other.unit, this.unit);
if (otherVal == null) {
Expand All @@ -57,9 +58,10 @@ export class Quantity {
return this.value >= otherVal;
}
}
return null
}

after(other: any) {
after(other: any): null | boolean {
if (other != null && other.isQuantity) {
const otherVal = convertUnit(other.value, other.unit, this.unit);
if (otherVal == null) {
Expand All @@ -68,9 +70,10 @@ export class Quantity {
return this.value > otherVal;
}
}
return null
}

before(other: any) {
before(other: any): null | boolean {
if (other != null && other.isQuantity) {
const otherVal = convertUnit(other.value, other.unit, this.unit);
if (otherVal == null) {
Expand All @@ -79,9 +82,10 @@ export class Quantity {
return this.value < otherVal;
}
}
return null
}

equals(other: any) {
equals(other: any): null | boolean {
if (other != null && other.isQuantity) {
if ((!this.unit && other.unit) || (this.unit && !other.unit)) {
return false;
Expand All @@ -96,15 +100,16 @@ export class Quantity {
}
}
}
return null
}

convertUnit(toUnit: any) {
convertUnit(toUnit: any): Quantity {
const value = convertUnit(this.value, this.unit, toUnit);
// Need to pass through constructor again to catch invalid units
return new Quantity(value, toUnit);
}

dividedBy(other: any) {
dividedBy(other: any): Quantity | null {
if (other == null || other === 0 || other.value === 0) {
return null;
} else if (!other.isQuantity) {
Expand All @@ -128,7 +133,7 @@ export class Quantity {
return new Quantity(decimalAdjust('round', resultValue, -8), resultUnit);
}

multiplyBy(other: any) {
multiplyBy(other: any): Quantity | null {
if (other == null) {
return null;
} else if (!other.isQuantity) {
Expand All @@ -153,7 +158,7 @@ export class Quantity {
}
}

export function parseQuantity(str: string) {
export function parseQuantity(str: string): Quantity | null {
const components = /([+|-]?\d+\.?\d*)\s*('(.+)')?/.exec(str);
if (components != null && components[1] != null) {
const value = parseFloat(components[1]);
Expand Down Expand Up @@ -206,16 +211,18 @@ export function doSubtraction(a: any, b: any) {
return doScaledAddition(a, b, -1);
}

export function doDivision(a: any, b: any) {
export function doDivision(a: Quantity | null, b: Quantity | null): Quantity | null {
if (a != null && a.isQuantity) {
return a.dividedBy(b);
}
return null
}

export function doMultiplication(a: any, b: any) {
export function doMultiplication(a: Quantity | null, b: Quantity | null): Quantity | null {
if (a != null && a.isQuantity) {
return a.multiplyBy(b);
} else {
} else if (b!= null && b.isQuantity) {
return b.multiplyBy(a);
}
return null
}
4 changes: 2 additions & 2 deletions src/util/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function normalizeUnitsWhenPossible(val1: any, unit1: any, val2: any, uni
return [newVal1, resultConverter(newUnit1), val2, resultConverter(unit2)];
}

export function convertToCQLDateUnit(unit: any) {
export function convertToCQLDateUnit(unit: any): string | undefined {
let dateUnit;
if (unit in CQL_TO_UCUM_DATE_UNITS) {
// it's already a CQL unit, so return it as-is, removing trailing 's' if necessary (e.g., years -> year)
Expand All @@ -118,7 +118,7 @@ export function convertToCQLDateUnit(unit: any) {
return dateUnit;
}

export function compareUnits(unit1: any, unit2: any) {
export function compareUnits(unit1: any, unit2: any): null | number {
try {
const c = convertUnit(1, unit1, unit2);
if (c && c > 1) {
Expand Down