Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance function: multiple arguments #244

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .changeset/funny-items-know.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 5 additions & 6 deletions packages/xpath/src/functions/xforms/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@
context: EvaluationContext<T>,
expression: EvaluableArgument
): Line[] => {
const points = evaluatePoints(context, expression);

const points = expression.flatMap((el) => evaluatePoints(context, el));

Check failure on line 89 in packages/xpath/src/functions/xforms/geo.ts

View workflow job for this annotation

GitHub Actions / Lint (global) (22.9.0)

Unsafe assignment of an error typed value

Check failure on line 89 in packages/xpath/src/functions/xforms/geo.ts

View workflow job for this annotation

GitHub Actions / Lint (global) (22.9.0)

Unsafe call of a(n) `error` type typed value

Check failure on line 89 in packages/xpath/src/functions/xforms/geo.ts

View workflow job for this annotation

GitHub Actions / Lint (global) (22.9.0)

Unsafe argument of type `any` assigned to a parameter of type `EvaluableArgument`
if (points.length < 2) {

Check failure on line 90 in packages/xpath/src/functions/xforms/geo.ts

View workflow job for this annotation

GitHub Actions / Lint (global) (22.9.0)

Unsafe member access .length on an `error` typed value
return [INVALID_LINE];
}

return Array.from(pairwise(points)).map((line) => {

Check failure on line 94 in packages/xpath/src/functions/xforms/geo.ts

View workflow job for this annotation

GitHub Actions / Lint (global) (22.9.0)

Unsafe argument of type error typed assigned to a parameter of type `Iterable<unknown> | Iterator<unknown, any, any>`
const [start, end] = line;

if (start === INVALID_POINT || end === INVALID_POINT) {
Expand Down Expand Up @@ -172,7 +171,7 @@
'area',
[{ arityType: 'required' }],
(context, [expression]) => {
const lines = evaluateLines(context, expression!);
const lines = evaluateLines(context, [expression!]);

if (lines.some(isInvalidLine)) {
return NaN;
Expand Down Expand Up @@ -209,9 +208,9 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('distance() and area() functions', () => {
});
});

describe('area with nodes', () => {
describe('area and distance with nodes', () => {
beforeEach(() => {
testContext = createXFormsTestContext(`
<root>
Expand Down
Loading