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

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class InvalidArgumentError extends Error {
}
}

export type ParameterArityType = 'optional' | 'required' | 'variadic';
export type ParameterArityType = 'optional' | 'required' | 'variadic' | 'variadic+';

export type ParameterTypeHint =
// | 'lazy' // TODO: it might be good to *explicitly* mark certain parameters
Expand Down Expand Up @@ -127,6 +127,12 @@ export class FunctionImplementation<Length extends number> {
max: Infinity,
};

case 'variadic+':
brontolosone marked this conversation as resolved.
Show resolved Hide resolved
return {
min: acc.min + 1,
max: Infinity,
};

default:
throw new UnreachableError(arityType);
}
Expand Down
16 changes: 9 additions & 7 deletions packages/xpath/src/functions/xforms/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ const INVALID_LINE: Line = {
end: INVALID_POINT,
};

const evaluateLines = (context: EvaluationContext, expression: EvaluableArgument): Line[] => {
const points = evaluatePoints(context, expression);

const evaluateLines = (
context: EvaluationContext,
expression: readonly EvaluableArgument[]
): Line[] => {
const points = expression.flatMap((el) => evaluatePoints(context, el));
if (points.length < 2) {
return [INVALID_LINE];
}
Expand Down Expand Up @@ -165,7 +167,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;
Expand Down Expand Up @@ -202,9 +204,9 @@ const sum = (values: readonly number[]) => {

export const distance = new NumberFunction(
'distance',
[{ arityType: 'required' }],
(context, [expression]) => {
const lines = evaluateLines(context, expression!);
[{ 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