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

Nested functions do not evaluate arguments #712

Open
Jaspooky opened this issue Sep 18, 2024 · 0 comments
Open

Nested functions do not evaluate arguments #712

Jaspooky opened this issue Sep 18, 2024 · 0 comments

Comments

@Jaspooky
Copy link

Jaspooky commented Sep 18, 2024

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant