Skip to content

Commit

Permalink
perf: undefinedChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed May 2, 2024
1 parent 645004d commit eb93157
Show file tree
Hide file tree
Showing 26 changed files with 109 additions and 66 deletions.
7 changes: 5 additions & 2 deletions src/internal/extractMatchingDoseAndRateFirstEntry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Functions
import { isNotUndefined } from "../internal/undefinedChecks";

// Types
import type { Dosage, DoseAndRate } from "../types";
type KeyType = keyof DoseAndRate;
Expand All @@ -7,15 +10,15 @@ export function extractMatchingDoseAndRateFirstEntry<T extends KeyType>(
key: T,
): DoseAndRate[T] | undefined {
// If empty, return undefined
if (dos.doseAndRate === undefined) {
if (!isNotUndefined(dos.doseAndRate)) {
return undefined;
}

// Find the first entry that match criteria
let doseAndRate = dos.doseAndRate.find((s) => s[key] !== undefined);

// If not found, skip
if (doseAndRate === undefined) {
if (!isNotUndefined(doseAndRate)) {
return undefined;
}

Expand Down
3 changes: 2 additions & 1 deletion src/internal/extractTimingRepeat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isNotUndefined } from "./undefinedChecks";
import type { Dosage } from "../types";

export function extractTimingRepeat(dos: Dosage) {
// If empty, return undefined
if (dos.timing?.repeat === undefined) {
if (!isNotUndefined(dos.timing?.repeat)) {
return undefined;
}

Expand Down
3 changes: 2 additions & 1 deletion src/internal/isEmptyArray.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNotUndefined } from "./undefinedChecks";
export function isArrayEmpty(array?: any[]): array is undefined | [] {
return array === undefined || array.length === 0;
return !isNotUndefined(array) || array.length === 0;
}
3 changes: 0 additions & 3 deletions src/internal/isNotUndefined.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/internal/undefinedChecks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function isNotUndefined<T>(val: T | undefined): val is T {
return val !== undefined;
}

export function noUndefinedInArray<T>(...args: (T | undefined)[]) {
return args.every(isNotUndefined);
}

export function allUndefinedInArray<T>(...args: (T | undefined)[]) {
return !args.some(isNotUndefined);
}
19 changes: 10 additions & 9 deletions src/translators/boundsDuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Functions
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isNotUndefined } from "../internal/undefinedChecks";

// types
import type { Config, Duration, DisplayOrderParams, I18N } from "../types";
Expand All @@ -16,14 +17,14 @@ function transformDurationToString(
if (duration.system === "http://hl7.org/fhir/ValueSet/duration-units") {
let code = duration.code! as "s" | "min" | "h" | "d" | "wk" | "mo" | "a";
return i18next.t(`unitsOfTime:withCount.${code}`, { count: quantity });
} else {
// otherwise, it is UCUM, ... so let the user do the job
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: duration,
});
return `${quantity} ${unit}`;
}

// otherwise, it is UCUM, ... so let the user do the job
let unit = config.fromFHIRQuantityUnitToString({
language: config.language,
quantity: duration,
});
return `${quantity} ${unit}`;
}

export function transformBoundsDurationToText({
Expand All @@ -34,15 +35,15 @@ export function transformBoundsDurationToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

// Pickup the repeat interesting attributes
let boundsDuration = repeat.boundsDuration;

// Do nothing if no boundsDuration, I am not a wizard
if (boundsDuration === undefined) {
if (!isNotUndefined(boundsDuration)) {
return undefined;
}

Expand Down
11 changes: 7 additions & 4 deletions src/translators/boundsPeriod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Functions
import { formatDatetime } from "../utils/formatDatetimes";
import { isNotUndefined } from "../internal/isNotUndefined";
import {
isNotUndefined,
noUndefinedInArray,
} from "../internal/undefinedChecks";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -11,7 +14,7 @@ export function transformBoundsPeriodToText({
i18next,
}: DisplayOrderParams): string | undefined {
// If empty, return undefined
if (dos.timing?.repeat?.boundsPeriod === undefined) {
if (!isNotUndefined(dos.timing?.repeat?.boundsPeriod)) {
return undefined;
}

Expand All @@ -24,15 +27,15 @@ export function transformBoundsPeriodToText({
// Three cases

// 1. Both start and end are present
if (isNotUndefined([start, end])) {
if (noUndefinedInArray(start, end)) {
return i18next.t("fields.boundsPeriod.startAndEnd", {
start: start,
end: end,
});
}

// 2. Only start is present
if (isNotUndefined([start])) {
if (isNotUndefined(start)) {
return i18next.t("fields.boundsPeriod.onlyStart", {
start: start,
});
Expand Down
7 changes: 4 additions & 3 deletions src/translators/boundsRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DisplayOrderParams } from "../types";
// Utility function
import { fromRangeToString } from "../utils/fromRangeToString";
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isNotUndefined } from "../internal/undefinedChecks";

export function transformBoundsRangeToText({
dos,
Expand All @@ -13,15 +14,15 @@ export function transformBoundsRangeToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

// Pickup the repeat interesting attributes
let boundsRange = repeat.boundsRange;

// Do nothing if no boundsRange, I am not a wizard
if (boundsRange === undefined) {
if (!isNotUndefined(boundsRange)) {
return undefined;
}

Expand All @@ -33,7 +34,7 @@ export function transformBoundsRangeToText({
});

// Reject if empty
if (rangeText === undefined) {
if (!isNotUndefined(rangeText)) {
return undefined;
}

Expand Down
11 changes: 7 additions & 4 deletions src/translators/countCountMax.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Functions
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isNotUndefined } from "../internal/isNotUndefined";
import {
isNotUndefined,
noUndefinedInArray,
} from "../internal/undefinedChecks";

// Types
import type { DisplayOrderParams } from "../types";
Expand All @@ -12,7 +15,7 @@ export function transformCountCountMaxToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

Expand All @@ -28,15 +31,15 @@ export function transformCountCountMaxToText({
// Three cases

// 1. Both count & countMax are present
if (isNotUndefined([count, countMax])) {
if (noUndefinedInArray(count, countMax)) {
return i18next.t("fields.countMax.countMax", {
count: countMax,
low: count,
});
}

// 2. Only countMax is present
if (isNotUndefined([countMax])) {
if (isNotUndefined(countMax)) {
return i18next.t("fields.count.count", { count: countMax });
}

Expand Down
3 changes: 2 additions & 1 deletion src/translators/dayOfWeek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { fromListToString } from "../utils/fromListToString";
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isArrayEmpty } from "../internal/isEmptyArray";
import { isNotUndefined } from "../internal/undefinedChecks";

// Types
import type { DisplayOrderParams } from "../types";
Expand All @@ -14,7 +15,7 @@ export function transformDayOfWeekToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

Expand Down
3 changes: 2 additions & 1 deletion src/translators/doseQuantity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Functions
import { extractMatchingDoseAndRateFirstEntry } from "../internal/extractMatchingDoseAndRateFirstEntry";
import { fromQuantityToString } from "../utils/fromQuantityToString";
import { isNotUndefined } from "../internal/undefinedChecks";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -13,7 +14,7 @@ export function transformDoseQuantityToText({
let doseQuantity = extractMatchingDoseAndRateFirstEntry(dos, "doseQuantity");

// If not found, skip
if (doseQuantity === undefined) {
if (!isNotUndefined(doseQuantity)) {
return undefined;
}

Expand Down
5 changes: 3 additions & 2 deletions src/translators/doseRange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Utility function
import { fromRangeToString } from "../utils/fromRangeToString";
import { extractMatchingDoseAndRateFirstEntry } from "../internal/extractMatchingDoseAndRateFirstEntry";
import { isNotUndefined } from "../internal/undefinedChecks";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -13,7 +14,7 @@ export function transformDoseRangeToText({
let doseRange = extractMatchingDoseAndRateFirstEntry(dos, "doseRange");

// If not found, skip
if (doseRange === undefined) {
if (!isNotUndefined(doseRange)) {
return undefined;
}

Expand All @@ -25,7 +26,7 @@ export function transformDoseRangeToText({
});

// Reject if empty
if (text === undefined) {
if (!isNotUndefined(text)) {
return undefined;
}

Expand Down
10 changes: 5 additions & 5 deletions src/translators/durationDurationMax.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Functions
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isNotUndefined } from "../internal/isNotUndefined";
import { isNotUndefined } from "../internal/undefinedChecks";

// Types
import type { DisplayOrderParams } from "../types";
Expand All @@ -12,7 +12,7 @@ export function transformDurationDurationMaxToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

Expand All @@ -22,20 +22,20 @@ export function transformDurationDurationMaxToText({
let unit = repeat.durationUnit;

// Do nothing if no unit, I am not a wizard
if (unit === undefined) {
if (!isNotUndefined(unit)) {
return undefined;
}

return [
// duration
isNotUndefined([duration]) &&
isNotUndefined(duration) &&
i18next.t("fields.duration", {
durationText: i18next.t(`unitsOfTime:withCount.${unit}`, {
count: duration,
}),
}),
// durationMax
isNotUndefined([max]) &&
isNotUndefined(max) &&
i18next.t("fields.durationMax", {
durationMaxText: i18next.t(`unitsOfTime:withCount.${unit}`, {
count: max,
Expand Down
11 changes: 7 additions & 4 deletions src/translators/frequencyFrequencyMax.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Functions
import { extractTimingRepeat } from "../internal/extractTimingRepeat";
import { isNotUndefined } from "../internal/isNotUndefined";
import {
isNotUndefined,
noUndefinedInArray,
} from "../internal/undefinedChecks";

// Types
import type { DisplayOrderParams } from "../types";
Expand All @@ -12,7 +15,7 @@ export function transformFrequencyFrequencyMaxToText({
let repeat = extractTimingRepeat(dos);

// If empty, return undefined
if (repeat === undefined) {
if (!isNotUndefined(repeat)) {
return undefined;
}

Expand All @@ -28,15 +31,15 @@ export function transformFrequencyFrequencyMaxToText({
// Three cases

// 1. Frequency and frequencyMax are present
if (isNotUndefined([frequency, max])) {
if (noUndefinedInArray(frequency, max)) {
return i18next.t("fields.frequency.withfrequencyMax", {
count: max,
frequency: frequency,
});
}

// 2. Only frequencyMax is present
if (isNotUndefined([max])) {
if (isNotUndefined(max)) {
return i18next.t("fields.frequencyMax.frequencyMax", { count: max });
}

Expand Down
3 changes: 2 additions & 1 deletion src/translators/maxDosePerAdministration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Functions
import { fromQuantityToString } from "../utils/fromQuantityToString";
import { isNotUndefined } from "../internal/undefinedChecks";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -10,7 +11,7 @@ export function transformMaxDosePerAdministrationToText({
i18next,
}: DisplayOrderParams): string | undefined {
// If empty, return undefined
if (dos.maxDosePerAdministration === undefined) {
if (!isNotUndefined(dos.maxDosePerAdministration)) {
return undefined;
}

Expand Down
3 changes: 2 additions & 1 deletion src/translators/maxDosePerLifetime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Functions
import { fromQuantityToString } from "../utils/fromQuantityToString";
import { isNotUndefined } from "../internal/undefinedChecks";

// types
import type { DisplayOrderParams } from "../types";
Expand All @@ -10,7 +11,7 @@ export function transformMaxDosePerLifetimeToText({
i18next,
}: DisplayOrderParams): string | undefined {
// If empty, return undefined
if (dos.maxDosePerLifetime === undefined) {
if (!isNotUndefined(dos.maxDosePerLifetime)) {
return undefined;
}

Expand Down
Loading

0 comments on commit eb93157

Please sign in to comment.