Menu

Implementing Groovy closure in JavaScript

2021-02-24
2021-03-21
  • Tim Nicholson

    Tim Nicholson - 2021-02-24

    Many FP metods, such as c.find() require a closure to filter the results. I am trying to implement something in javascript but cannot get anything to work. AFAIK the closure should return a boolean but just inserting a javasript boolean test does not work.

    For example c.find(true)produces a type error saying the parameters do not match any of the method options. I would have expected it to return the same as c.findAll().

    I can do a c.findAll() then fliter the resulting list in this case but there are other methods where I cannot find a simple solution such as node.sortChildrenBy().

    There is also the question of passing the object to be tested, in javascript it is not defined.

     
  • Alexandre

    Alexandre - 2021-03-15

    Groovy is the preferred language for Freeplane. Some things may not be as easy to do with the other languages.

     
    • Tim Nicholson

      Tim Nicholson - 2021-03-17

      I appreciate this, and accept that some things may be harder. However, using a language you are more familiar with makes other things much easier and that is generlly where I work. I don't mind the edge cases being a bit harder since having to convert a whole script is possibly harder still.

       
      • lilive

        lilive - 2021-03-17

        Hi,
        I have tried and found something.

        var condition  = new org.freeplane.api.NodeCondition() {
            check: function( node ) {
                return node.text.length()>3
            }
        };
        var nodes = c.find( condition );
        node.text = "I have found " + nodes.size() + " nodes"
        

        It works but... not so convenient !

         
        👍
        1

        Last edit: lilive 2021-03-17
  • Tim Nicholson

    Tim Nicholson - 2021-03-18

    That's great, thanks for digging it out I will take a look.

     
  • Tim Nicholson

    Tim Nicholson - 2021-03-21

    Just had a chance to give this a try and it does exactly what I was looking for so thanks very much.
    Given that a NodeCondition test needs writing anyway it only adds a couple of extra lines of code to add the class instance.