Skip to content

Commit

Permalink
Various non-logic changes (beyondessential#3141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alastair-L authored Sep 8, 2021
1 parent 65938c7 commit e0e0422
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export class ExpressionParser {
return this.parser.evaluate(expression);
}

/**
* Evaluate an expression and cast boolean results to 1 or 0. Also validate the expression beforehand.
*
* Note this could also return a string, array, or object (potentially others too)
* @param {*} expression
*/
evaluateToNumber(expression) {
const result = this.evaluate(expression);
if (typeof result === 'boolean') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class AnalyticArithmeticBuilder extends Builder {
return this.configCache;
}

private get paramBuildersByCode() {
private get paramBuildersByCode(): Record<string, Builder> {
if (!this.paramBuildersByCodeCache) {
this.paramBuildersByCodeCache = Object.fromEntries(
this.config.parameters.map(param => [param.code, createBuilder(this.api, param)]),
Expand Down Expand Up @@ -121,12 +121,12 @@ export class AnalyticArithmeticBuilder extends Builder {
const checkClusterIncludesAllVariables = (cluster: AnalyticCluster) =>
variables.every(variable => variable in cluster.dataValues);

const replaceAnalyticValuesWithDefaults = (cluster: AnalyticCluster) => ({
const replaceAnalyticValuesWithDefaults = (cluster: AnalyticCluster): AnalyticCluster => ({
...cluster,
dataValues: replaceDataValuesWithDefaults(cluster.dataValues, defaultValues),
});

const clusters = analyticsToAnalyticClusters(analytics);
const clusters: AnalyticCluster[] = analyticsToAnalyticClusters(analytics);
// Remove clusters that do not include all specified elements
return clusters.map(replaceAnalyticValuesWithDefaults).filter(checkClusterIncludesAllVariables);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { utcMoment, PERIOD_TYPES, momentToPeriod } from '@tupaia/utils';

import { Builder } from '../Builder';
import { FetchOptions, Event, DataValues } from '../../types';
import { FetchOptions, Event } from '../../types';
import { getExpressionParserInstance } from '../../getExpressionParserInstance';
import { EventCheckConditionsConfig, DefaultValue, configValidators } from './config';
import { validateConfig, evaluateFormulaToNumber, replaceDataValuesWithDefaults } from '../helpers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const buildParams = (params: unknown): TransformParams => {
} = params;
if (typeof transformStep !== 'string' || !(transformStep in transformBuilders)) {
throw new Error(
`Expected transform to be one of ${Object.keys(transformBuilders)} but got ${transform}`,
`Expected transform to be one of ${Object.keys(transformBuilders)} but got ${transformStep}`,
);
}

Expand Down

0 comments on commit e0e0422

Please sign in to comment.