Skip to content

Nested functions do not evaluate arguments #712

Open
@Jaspooky

Description

When functions are not defined at the top level but instead attached as part of an object, their arguments don't appear to actually be evaluated. For example below, reverseString works fine when declared as a top level function, but not when referenced as a property of a wrapping object.

MRE

const jsonata = require('jsonata');
 
 const lib = () => ({
   reverseString: (str) => str.split('').reverse().join(''),
 });
 
 const createJsonata = (expression) => {
   const instance = jsonata(expression);

   instance.registerFunction('lib', lib);
   instance.registerFunction('reverseString', lib().reverseString);
 
   return instance;
 };
 
 const data = { name: 'Jasper' };
 
 const works = createJsonata("( $str := $.name; $lib().reverseString($str) )")
   .evaluate(data); //?
   // 'repsaJ'
 
 const alsoWorks = createJsonata("$reverseString($.name)")
   .evaluate(data); //?
   // 'repsaJ'
 
 const fails = createJsonata("$lib().reverseString($.name)")
   .evaluate(data); //?
   // TypeError: Cannot read properties of undefined (reading 'split')

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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