Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    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
    Sergii Stotskyi
    @stalniy
    Thanks @J3m5 !
    Jems
    @J3m5
    You're welcome
    I'm looking after a bug in getValueByPath right now
    It doesn't handle deeply nested array
      it('returns deeply nested property values from array', () => {
        const object = { items: [{ specs: [{ price: 12 }] }, { specs: [{ price: 14 }] }] }
        expect(getObjectField(object, 'items.specs.price')).to.deep.equal([12, 14])
      })
     AssertionError: expected [ undefined, undefined ] to deeply equal [ 12, 14 ]
      + expected - actual
    
      [
      -  [undefined]
      -  [undefined]
      +  12
      +  14
       ]
    Jems
    @J3m5
    And this is because at some point the items will be
    [ [ { price: 12 } ], [ { price: 14 } ] ]
    Sergii Stotskyi
    @stalniy
    Thanks for testing @J3m5! I'll look into this tomorrow and think what I can do with this
    maybe instead of Array.map we can use Array.reduce + concat
    Jems
    @J3m5
    That's what I'm trying but typescript is complaining about types and I don't know it well enough to make the types right
    I got it working!
    Sergii Stotskyi
    @stalniy
    Cool!
    Jems
    @J3m5
    All current and new tests have passed!
    This change make it unnecessary to check if the value is an array or a projected field in the interpreters, and that's a good news because there were other interpreters like all, in or elemMatch that didn't works with nested arrays.
    I'll submit a PR
    Sergii Stotskyi
    @stalniy
    Awesome news! Waiting for this PR!
    Jems
    @J3m5
    Do you prefer one big commit ot mutlples detailed ones ?
    Jems
    @J3m5
    PR submited, I'll squash those commits if you want.
    Sergii Stotskyi
    @stalniy
    That’s fine. PR is not so big. Thanks for asking :) I'll squash commits during merge, so no worries
    Simon Brunel
    @simonbrunel
    Hi! Thanks again @stalniy for your work on CASL and UCAST :) I'm going to give a try to UCAST in a NestJS project, in an attempt to not use nestjs-query. I'm wondering what is the current status of this project, are you still interested in maintaining it? How do you feel it will evolve?
    Simon Brunel
    @simonbrunel

    For context, my use case is abstracting HTTP queries (at the controller level) and later interpret these queries (at the service level) either as a mongodb query or an Azure Search query, which means:

    • a custom parser to translate HTTP queries to condition AST (I can reuse part of the @ucast/mongodb parser instructions + custom)
    • and a mongodb interpreter (I was hoping this to be built-in, but just found stalniy/ucast#2)
    • an Azure Search interpreter (obviously, I need to write it from scratch, though I could contribute it back if it makes sense)

    So it looks like I would need to write quite a lot of logic, thus the initial question :)

    Sergii Stotskyi
    @stalniy

    Hi @simonbrunel

    Thanks :)

    the project is alive and I do maintain it, it is used by casl and one commercial DAL.
    However I don't have much time to push it forward for now. just busy on my current job
    Simon Brunel
    @simonbrunel
    Thanks @stalniy! I will give it a try and, if conclusive, will contribute the mongodb interpreter. Though, I'm not sure I will have time to make it "clean" enough for a good PR. Will see :)