Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Sergii Stotskyi
    @stalniy
    Welcome :)
    Jems
    @J3m5
    Hello there !
    Sergii Stotskyi
    @stalniy
    Hello @J3m5 ;)
    Jems
    @J3m5
    I've read the issues on the sift repo about ucast and now I'm trying mongo2js.
    I noticed that unlike sift it doesn't accept a simple function instead of a mongo query and only literal object as a value, is it planned for mongo2js to have the same behavior as sift ?
    Sergii Stotskyi
    @stalniy
    Could you please show an wxample because im not sure that understand you correctly?
    Jems
    @J3m5
    const sift = require("sift");
    const { filter } = require("@ucast/mongo2js");
    
    const siftChecker = sift(Boolean)
    const ucastChecker = filter(Boolean)
    
    console.log(siftChecker(false));
    //false
    console.log(ucastChecker(false));
    //true
    With objects too
    const data = {
      num: 3
    }
    
    const siftChecker = sift(data => data.num === 2)
    const ucastChecker = filter(data => data.num === 2)
    
    console.log(siftChecker(data));
    //false
    console.log(ucastChecker(data));
    //true
    Jems
    @J3m5
    const data = {
      num: 3
    }
    
    const query = {
      num: val => val > 2
    }
    
    const siftChecker = sift(query)
    const ucastChecker = filter(query)
    
    console.log(siftChecker(data));
    //true
    console.log(ucastChecker(data));
    //false
    Jems
    @J3m5
    More importantly, using the filter array method with it make it crash
    2 replies
    const topic = [
      {
        name: "craig",
      },
      {
        name: "tim",
      },
    ];
    
    const query = {
      name: "tim",
    };
    
    const siftChecker = sift(query);
    const ucastChecker = filter(query);
    
    console.log(topic.filter(siftChecker).length);
    // 1
    console.log(topic.filter(ucastChecker).length);
    // TypeError: n is not a function
    //    at p (/home/jeremy/Projects/playground/node_modules/@ucast/js/dist/es6c/index.js:1:440)
    //    at interpret (/home/jeremy/Projects/playground/node_modules/@ucast/core/dist/es6c/index.js:1:760)
    //    at Array.filter (<anonymous>)
    //    at Object.<anonymous> (/home/jeremy/Projects/playground/sift.js:86:19)
    //    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    //    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    //    at Module.load (internal/modules/cjs/loader.js:986:32)
    //    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    //    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    //    at internal/main/run_main_module.js:17:47
    Jems
    @J3m5
    I guess this happen because og the array method passing the index and array parameter to the filter mongo2js fonction
    Either I do it wrong, or there's still gonna be a need for testing ^^
    Jems
    @J3m5
    Fixed
    const topic = [
      {
        name: "craig",
      },
      {
        name: "tim",
      },
    ];
    
    const query = {
      name: "tim",
    };
    
    const siftChecker = sift(query);
    const ucastChecker = filter(query);
    const ucastCheckerFixed = (data) => ucastChecker(data);
    
    console.log(topic.filter(siftChecker).length);
    // 1
    console.log(topic.filter(ucastCheckerFixed).length);
    // 1
    Sergii Stotskyi
    @stalniy
    @J3m5 functions will not be supported. What is the purpose to support functions if sift(data => data.num === 2) is actually the same as data => data.num === 2?
    const query = {
      num: val => val > 2
    }
    This usecase also won't be supported because it's not MongoDB compliant. MongoDB equivalent to this is
    const query = {
      $where() { 
        return this.num > 2
      }
    }

    In context of CASL functions are not very useful, because they are not serializable. If you won't to use functions on fields, why do you use sift at all? As it's much easier to write a function with all the required conditions.

    You can use functions as conditions in CASL - https://casl.js.org/v4/en/advanced/customize-ability#custom-conditions-matcher-implementation

    And thanks for the sample with crash. Indeed, this is an issue. It happens as you said because Array.prototype.filter passes additional arguments to function which is then passed down parser, which excepts an object and that's why it crashes
    I'll fix it
    Sergii Stotskyi
    @stalniy
    anyway @ucast/mongo2js will not help you to solve the usecase which you explained in sift's issue. To implement it, you need to use @ucast/mongo and @ucast/js and create a custom interpreter
    Jems
    @J3m5
    I'm mentionned all this because backward compatibility with sift was discussed here crcn/sift.js#204.
    So if replacing sift is not the goal anymore then it's not important.
    But I did some search about serializing functions, and it's technically possible.
    Functions can be stringified with the toString method and then parsed and recreated with the new Function constructor.
    Jems
    @J3m5
    const funcStrings = ["val", "return val > 2"];
    const func = new Function(...funcStrings);
    console.log(func(3))
    // true
    Sergii Stotskyi
    @stalniy
    new Function is almost the same as eval and forbidden in some CSP environments, so this cannot be used universally
    for example in chrome apps you can use new Function. You also cannot use new Function when Content-Secure-Policy doesn't allow to eval - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
    Also it's not recommended due to security concerns
    Jems
    @J3m5
    Yep, I said it's technically possible, not that it was secure ;)
    Jems
    @J3m5
    About my issue, I'm a bit lost, I'm looking around in the source code but I don't know what function I need to customize / reimplement...
    Can you give me a hint please ?
    Jems
    @J3m5
    I customized the createInterpreter function in order to get the result and the path but now I need to figure out how to accumulate the results and how to stop the operation while returning a value that is not false.
    6 replies
    The farther I get, the harder you make it, haha ^^
    Jems
    @J3m5
    Hi @stalniy , I'm trying to setup the project but when I runthe build command I get
    src/interpreter.ts:1:43 - error TS2307: Cannot find module '@ucast/core' or its corresponding type declarations.
    
    1 import { createInterpreter, ITSELF } from '@ucast/core';
    I've followed the instructions in the CONTRIBUTING.md file
    Jems
    @J3m5
    Problem solved, I had to run pnpm run build -r
    Sergii Stotskyi
    @stalniy
    yes, pnpm do link all packages as local dependencies that's why you need to build everything first.
    Jems
    @J3m5
    Just sent a PR for that.
    It ain't much but it's honest work