Skip to content

Commit

Permalink
Nit: restore original signature of evaluateLines, map args in `dist…
Browse files Browse the repository at this point in the history
…ance` impl

This is sort of subtle (and maybe a bit subjective). Hopefully the intent is clearer with the added JSDoc and updated argument names.
  • Loading branch information
eyelidlessness committed Jan 28, 2025
1 parent 8af45cd commit 8b66b2a
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/xpath/src/functions/xforms/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,22 @@ const INVALID_LINE: Line = {
end: INVALID_POINT,
};

/**
* Takes an {@link pointsArg} representing a serialized sequence of geopoints,
* producing an array of {@link Line}s.
*
* Corresponds to the semantics of _a single argument_ from either of the
* following functions, specified by ODK XForms:
*
* - {@link https://getodk.github.io/xforms-spec/#fn:area | area}
* - {@link https://getodk.github.io/xforms-spec/#fn:distance | distance}
*/
const evaluateLines = <T extends XPathNode>(
context: EvaluationContext<T>,
expression: readonly EvaluableArgument[]
pointsArg: EvaluableArgument
): Line[] => {
const points = expression.flatMap((el) => evaluatePoints(context, el));
const points = evaluatePoints(context, pointsArg);

if (points.length < 2) {
return [INVALID_LINE];
}
Expand Down Expand Up @@ -170,8 +181,8 @@ const geodesicArea = (lines: readonly Line[]): number => {
export const area = new NumberFunction(
'area',
[{ arityType: 'required' }],
(context, [expression]) => {
const lines = evaluateLines(context, [expression!]);
(context, [pointsArg]) => {
const lines = evaluateLines(context, pointsArg!);

if (lines.some(isInvalidLine)) {
return NaN;
Expand Down Expand Up @@ -209,8 +220,10 @@ const sum = (values: readonly number[]) => {
export const distance = new NumberFunction(
'distance',
[{ arityType: 'required' }, { arityType: 'variadic' }],
(context, args) => {
const lines = evaluateLines(context, args);
(context, pointsArgs) => {
const lines = pointsArgs.flatMap((pointsArg) => {
return evaluateLines(context, pointsArg);
});

if (lines.some(isInvalidLine)) {
return NaN;
Expand Down

0 comments on commit 8b66b2a

Please sign in to comment.