fontoxpath icon indicating copy to clipboard operation
fontoxpath copied to clipboard

how do i specify a union return type in `registerCustomXPathFunction`, or an "any" type?

Open DetachHead opened this issue 1 year ago • 3 comments

i'm trying to register a function where the return type is not known (i'm, pretty sure the valid types are boolean, number, string and Array but i'm not 100% sure)

registerCustomXPathFunction(
    { namespaceURI: 'foo', localName: 'bar' },
    [],
    'xs:???', // what do i put here?
    () => getSomeUnknownValue(),
)

i tried xs:anySimpleType and xs:anyAtomicType but they didn't work:

Error: Values of the type "xs:anySimpleType" can not be adapted from JavaScript to equivalent XPath values.

is there a way i can specify a union type, or disable the type checking with just an "any" type instead?

DetachHead avatar Apr 26 '24 05:04 DetachHead

Hey there,

item()* is your answer. This includes nodes, arrays, maps, etcetera. Would be cool to extend the typing system here to include even more types, but this is where we are right now.

Kind regards,

Martin

DrRataplan avatar May 01 '24 07:05 DrRataplan

thanks! another question: is there any equivalent type for null or undefined in (fonto)xpath? currently get this error when the function returns null or undefined:

Error: The JavaScript value null should be an array if it is to be converted to item()*.

DetachHead avatar May 01 '24 08:05 DetachHead

Hey,

If its item()*, its just an empty array. Anything with * is seen as returning an array, which is converted into a sequence. An empty sequence is analogous to null in usage, so an empty array works!

Kr,

Martin

On Wed, 1 May 2024, 10:07 DetachHead, @.***> wrote:

thanks! another question: is there any equivalent type for null or undefined in (fonto)xpath? currently get this error when the function returns null or undefined:

Error: The JavaScript value null should be an array if it is to be converted to item()*.

— Reply to this email directly, view it on GitHub https://github.com/FontoXML/fontoxpath/issues/636#issuecomment-2088129426, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGKEJHDM2HPVHMNIART5YLZACPF3AVCNFSM6AAAAABG2DWLFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBYGEZDSNBSGY . You are receiving this because you commented.Message ID: @.***>

DrRataplan avatar May 01 '24 08:05 DrRataplan

awesome, thanks!

DetachHead avatar May 02 '24 03:05 DetachHead