diff --git a/.changeset/funny-items-know.md b/.changeset/funny-items-know.md new file mode 100644 index 000000000..77eaa3058 --- /dev/null +++ b/.changeset/funny-items-know.md @@ -0,0 +1,8 @@ +--- +"@getodk/xpath": patch +--- + +Distance function can now accepts multiple arguments. +This makes it easier to compute the distance between multiple points within a form's primary instance. +Previously, to achieve this, you'd have to introduce a calculate which concatenates those points together, +and then call the distance function with a reference to that calculate as the argument. diff --git a/packages/xpath/src/functions/xforms/geo.ts b/packages/xpath/src/functions/xforms/geo.ts index c2adc63e6..b0bbe12b9 100644 --- a/packages/xpath/src/functions/xforms/geo.ts +++ b/packages/xpath/src/functions/xforms/geo.ts @@ -86,8 +86,7 @@ const evaluateLines = ( context: EvaluationContext, expression: EvaluableArgument ): Line[] => { - const points = evaluatePoints(context, expression); - + const points = expression.flatMap((el) => evaluatePoints(context, el)); if (points.length < 2) { return [INVALID_LINE]; } @@ -172,7 +171,7 @@ export const area = new NumberFunction( 'area', [{ arityType: 'required' }], (context, [expression]) => { - const lines = evaluateLines(context, expression!); + const lines = evaluateLines(context, [expression!]); if (lines.some(isInvalidLine)) { return NaN; @@ -209,9 +208,9 @@ const sum = (values: readonly number[]) => { export const distance = new NumberFunction( 'distance', - [{ arityType: 'required' }], - (context, [expression]) => { - const lines = evaluateLines(context, expression!); + [{ arityType: 'required' }, { arityType: 'variadic' }], + (context, args) => { + const lines = evaluateLines(context, args); if (lines.some(isInvalidLine)) { return NaN; diff --git a/packages/xpath/test/xforms/area.test.ts b/packages/xpath/test/xforms/geo.test.ts similarity index 99% rename from packages/xpath/test/xforms/area.test.ts rename to packages/xpath/test/xforms/geo.test.ts index 1adcf1da7..8d9f5eae8 100644 --- a/packages/xpath/test/xforms/area.test.ts +++ b/packages/xpath/test/xforms/geo.test.ts @@ -43,7 +43,7 @@ describe('distance() and area() functions', () => { }); }); - describe('area with nodes', () => { + describe('area and distance with nodes', () => { beforeEach(() => { testContext = createXFormsTestContext(`