Skip to content

Commit

Permalink
Merge pull request #38 from jy95/quantityImprovements
Browse files Browse the repository at this point in the history
feat: improving rendering of quantity objects
  • Loading branch information
jy95 authored Apr 23, 2024
2 parents 389327f + 0b676cf commit 8d226a1
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 53 deletions.
2 changes: 1 addition & 1 deletion __tests__/corner_cases.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Corner cases", () => {
};

let result = dosageUtils.fromDosageToText(dosage);
expect(result).toBe("42 ");
expect(result).toBe("42");
});

test("rateRatio - no denominator", () => {
Expand Down
13 changes: 8 additions & 5 deletions src/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
"denominator_other": ":{{count}}"
}
},
"quantity": "{{quantity}} {{unit}}"
"quantity": {
"withUnit": "{{quantity}} {{unit}}",
"withoutUnit": "{{quantity}}"
}
},
"fields": {
"doseQuantity": "{{quantity}} {{unit}}",
"doseQuantity": "{{quantityText}}",
"doseRange": "{{rangeText}}",
"rateQuantity": "mit einem Verhältnis von {{quantity}} {{unit}}",
"rateQuantity": "mit einem Verhältnis von {{quantityText}}",
"rateRange": "mit einem Verhältnis von {{rangeText}}",
"rateRatio": "mit einem Verhältnis von {{ratioText}}",
"duration": "für {{durationText}}",
Expand Down Expand Up @@ -83,8 +86,8 @@
"event_one": "{{event}}",
"event_other": "{{event}}"
},
"maxDosePerLifetime": "bis zu einer maximalen Menge von {{count}} {{unit}} über die Lebenszeit des Patienten",
"maxDosePerAdministration": "bis zu einer maximalen Menge von {{count}} {{unit}} pro Dosis",
"maxDosePerLifetime": "bis zu einer maximalen Menge von {{quantityText}} über die Lebenszeit des Patienten",
"maxDosePerAdministration": "bis zu einer maximalen Menge von {{quantityText}} pro Dosis",
"maxDosePerPeriod": "bis zu einer maximalen Menge von {{maxDosePerPeriodText}}"
}
}
13 changes: 8 additions & 5 deletions src/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
"denominator_other": ":{{count}}"
}
},
"quantity": "{{quantity}} {{unit}}"
"quantity": {
"withUnit": "{{quantity}} {{unit}}",
"withoutUnit": "{{quantity}}"
}
},
"fields": {
"doseQuantity": "{{quantity}} {{unit}}",
"doseQuantity": "{{quantityText}}",
"doseRange": "{{rangeText}}",
"rateQuantity": "at a rate of {{quantity}} {{unit}}",
"rateQuantity": "at a rate of {{quantityText}}",
"rateRange": "at a rate of {{rangeText}}",
"rateRatio": "at a rate of {{ratioText}}",
"duration": "over {{durationText}}",
Expand Down Expand Up @@ -83,8 +86,8 @@
"event_one": "on {{event}}",
"event_other": "on {{event}}"
},
"maxDosePerLifetime": "up to a maximum of {{count}} {{unit}} for the lifetime of patient",
"maxDosePerAdministration": "up to a maximum of {{count}} {{unit}} per dose",
"maxDosePerLifetime": "up to a maximum of {{quantityText}} for the lifetime of patient",
"maxDosePerAdministration": "up to a maximum of {{quantityText}} per dose",
"maxDosePerPeriod": "up to a maximum of {{maxDosePerPeriodText}}"
}
}
13 changes: 8 additions & 5 deletions src/locales/fr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
"denominator_other": ":{{count}}"
}
},
"quantity": "{{quantity}} {{unit}}"
"quantity": {
"withUnit": "{{quantity}} {{unit}}",
"withoutUnit": "{{quantity}}"
}
},
"fields": {
"doseQuantity": "{{quantity}} {{unit}}",
"doseQuantity": "{{quantityText}}",
"doseRange": "{{rangeText}}",
"rateQuantity": "au taux de {{quantity}} {{unit}}",
"rateQuantity": "au taux de {{quantityText}}",
"rateRange": "au taux de {{rangeText}}",
"rateRatio": "au taux de {{ratioText}}",
"duration": "durant {{durationText}}",
Expand Down Expand Up @@ -83,8 +86,8 @@
"event_one": "{{event}}",
"event_other": "{{event}}"
},
"maxDosePerLifetime": "jusqu'à un maximum de {{count}} {{unit}} pour la durée de vie du patient",
"maxDosePerAdministration": "jusqu'à un maximum de {{count}} {{unit}} par dose",
"maxDosePerLifetime": "jusqu'à un maximum de {{quantityText}} pour la durée de vie du patient",
"maxDosePerAdministration": "jusqu'à un maximum de {{quantityText}} par dose",
"maxDosePerPeriod": "jusqu'à un maximum de {{maxDosePerPeriodText}}"
}
}
13 changes: 8 additions & 5 deletions src/locales/nl/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
"denominator_other": ":{{count}}"
}
},
"quantity": "{{quantity}} {{unit}}"
"quantity": {
"withUnit": "{{quantity}} {{unit}}",
"withoutUnit": "{{quantity}}"
}
},
"fields": {
"doseQuantity": "{{quantity}} {{unit}}",
"doseQuantity": "{{quantityText}}",
"doseRange": "{{rangeText}}",
"rateQuantity": "met een verhouding van {{quantity}} {{unit}}",
"rateQuantity": "met een verhouding van {{quantityText}}",
"rateRange": "met een verhouding van {{rangeText}}",
"rateRatio": "met een verhouding van {{ratioText}}",
"duration": "gedurende {{durationText}}",
Expand Down Expand Up @@ -83,8 +86,8 @@
"event_one": "{{event}}",
"event_other": "{{event}}"
},
"maxDosePerLifetime": "tot een maximum van {{count}} {{unit}} gedurende de levensduur van de patiënt",
"maxDosePerAdministration": "tot een maximum van {{count}} {{unit}} per dosis",
"maxDosePerLifetime": "tot een maximum van {{quantityText}} gedurende de levensduur van de patiënt",
"maxDosePerAdministration": "tot een maximum van {{quantityText}} per dosis",
"maxDosePerPeriod": "tot een maximum van {{maxDosePerPeriodText}}"
}
}
16 changes: 8 additions & 8 deletions src/translators/doseQuantity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Functions
import { extractMatchingDoseAndRateFirstEntry } from "../internal/extractMatchingDoseAndRateFirstEntry";
import { fromQuantityToString } from "../utils/fromQuantityToString";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -15,14 +17,12 @@ export function transformDoseQuantityToText({
return undefined;
}

let quantity = doseQuantity.value || 1;
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: doseQuantity,
});

// Otherwise rendering it
return i18next.t("fields.doseQuantity", {
quantity: quantity,
unit: unit,
quantityText: fromQuantityToString({
quantity: doseQuantity,
config,
i18next,
}),
});
}
14 changes: 8 additions & 6 deletions src/translators/maxDosePerAdministration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Functions
import { fromQuantityToString } from "../utils/fromQuantityToString";

// types
import type { DisplayOrderParams } from "../types";

Expand All @@ -12,13 +15,12 @@ export function transformMaxDosePerAdministrationToText({
}

const maxDosePerAdministration = dos.maxDosePerAdministration;
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: maxDosePerAdministration,
});

return i18next.t("fields.maxDosePerAdministration", {
count: maxDosePerAdministration.value || 1,
unit: unit,
quantityText: fromQuantityToString({
quantity: maxDosePerAdministration,
config,
i18next,
}),
});
}
14 changes: 8 additions & 6 deletions src/translators/maxDosePerLifetime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Functions
import { fromQuantityToString } from "../utils/fromQuantityToString";

// types
import type { DisplayOrderParams } from "../types";

Expand All @@ -12,13 +15,12 @@ export function transformMaxDosePerLifetimeToText({
}

const maxDosePerLifetime = dos.maxDosePerLifetime;
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: maxDosePerLifetime,
});

return i18next.t("fields.maxDosePerLifetime", {
count: maxDosePerLifetime.value || 1,
unit: unit,
quantityText: fromQuantityToString({
quantity: maxDosePerLifetime,
config,
i18next,
}),
});
}
18 changes: 7 additions & 11 deletions src/translators/rateQuantity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Functions
import { extractMatchingDoseAndRateFirstEntry } from "../internal/extractMatchingDoseAndRateFirstEntry";
import { fromQuantityToString } from "../utils/fromQuantityToString";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -15,17 +17,11 @@ export function transformRateQuantityToText({
return undefined;
}

// information from rate
let value = rateQuantity.value || 1;

// quantity unit
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: rateQuantity,
});

return i18next.t("fields.rateQuantity", {
quantity: value,
unit: unit,
quantityText: fromQuantityToString({
quantity: rateQuantity,
config,
i18next,
}),
});
}
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export type { I18N, InitOptions };
*/
export type I18InitOptions = Exclude<InitOptions, "lng" | "ns" | "defaultNS">;

// Types for amount functions
// Types for translation functions
export type RangeParams = {
range: Range;
config: Config;
Expand All @@ -270,3 +270,9 @@ export type RatioParams = {
config: Config;
i18next: I18N;
};

export type QuantityParams = {
quantity: Quantity;
config: Config;
i18next: I18N;
};
31 changes: 31 additions & 0 deletions src/utils/fromQuantityToString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Function

// Type
import type { QuantityParams } from "../types";

// To cover all nasty cases of Quantity, only once
// https://build.fhir.org/datatypes.html#Quantity
export function fromQuantityToString({
quantity,
config,
i18next,
}: QuantityParams): string | undefined {
// extract function for the unit display from config
const { fromFHIRQuantityUnitToString, language } = config;

// Compute the result
let unit = fromFHIRQuantityUnitToString({ language, quantity });
let value = quantity.value || 1;

// If no unit is present (in other words ""), we don't put it
if (unit.length === 0) {
return i18next.t("amount.quantity.withoutUnit", {
quantity: value,
});
} else {
return i18next.t("amount.quantity.withUnit", {
quantity: value,
unit: unit,
});
}
}

0 comments on commit 8d226a1

Please sign in to comment.