Skip to content

incorrect 'each' function signature #767

@sergeevik

Description

@sergeevik

from docs:
https://docs.jsonata.org/programming

Invocation chaining
value ~> $funcA ~> $funcB
is equivalent to
$funcB($funcA(value))

Examples

Customer.Email ~> $substringAfter("@") ~> $substringBefore(".") ~> $uppercase()

in jsonata exerciser:
$each([123, 321], function($v) {$v}) return Argument 1 of function "each" does not match function signature
Image

and
[123, 321] ~> $each(?, function($v) {$v}) return [123,321]

Image

by the docs
value ~> $funcA is equivalent to $funcA(value) but for each function it's not

research this if find some strage behavior:
when each recieve array as param - it's have signature and check it. When use chaining jsonata create new runtime 'each' function without signature and not check it

in code of 'each' jsonata just iterate on key of first argument. and in js array have keys (Object.keys(arr)). and it's work

    /**
     *
     * @param {*} obj - the input object to iterate over
     * @param {*} func - the function to apply to each key/value pair
     * @returns {Array} - the resultant array
     */
    async function each(obj, func) {
        var result = createSequence();

        for (var key in obj) {  <-- if obj is array code work well, but signature broken (btw not checked)
            var func_args = hofFuncArgs(func, obj[key], key, obj);
            // invoke func
            var val = await func.apply(this, func_args);
            if(typeof val !== 'undefined') {
                result.push(val);
            }
        }

        return result;
    }

found it's behavior when migrate my code from js to java and use https://github.com/dashjoin/jsonata-java
open issue there dashjoin/jsonata-java#79 but it froze.

Anyway. I suggest change 'each' signature to <(oa)-f:a> which allow array or object for processing. This is a more correct solution in my opinion (to avoid a kludge), and it brings the signature into line with actual functionality.

I hope you'll take a look and give me feedback, as we need to decide whether migration is possible without any kludge.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions